All files validate.js

81.17% Statements 69/85
56.25% Branches 27/48
100% Functions 3/3
81.17% Lines 69/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 2371x 1x 1x 1x   1x   1x                 4x   4x       4x       4x       4x       4x       4x 4x 2x     2x       2x                       2x   2x           2x         2x 2x           2x         2x 1x           1x 1x           1x               1x   1x 1x 1x 1x       1x 1x 1x         1x 1x 1x   1x         1x 2x         2x         2x               2x       2x 2x       2x 8x 8x           8x 8x 8x 8x               8x           1x     7x       7x 7x   7x                     7x       7x   7x 7x     7x 7x     7x           1x           1x          
const fs = require('fs');
const path = require('path');
const utils = require('../lib/utils');
const { ValidationError, PropertyRequiredError } = require('./customError');
 
const defaultLiveReloadPort = 8999;
 
module.exports = function (options) {
	const {
		webappDirectory,
		commonLibraryDirectory,
		react,
		hmr,
		esbuild,
		smp,
		ba
	} = options;
 
	Iif (typeof react === 'string') {
		global.reactPageList = react.split(',');
	}
 
	Iif (hmr) {
		global.hmr = true;
	}
 
	Iif (esbuild) {
		global.esbuild = true;
	}
 
	Iif (smp) {
		global.smp = true;
	}
 
	Iif (ba) {
		global.ba = true;
	}
 
	let webappDirectoryList = []; //所有的 webapp 工程
	if (!webappDirectory) {
		throw new PropertyRequiredError('webappDirectory');
	}
 
	Iif (typeof webappDirectory !== 'string') {
		throw new Error('The webapp directory path is empty!');
	}
 
	Iif (commonLibraryDirectory) {
		if (!fs.existsSync(commonLibraryDirectory)) {
			throw new ValidationError(
				"can't find the common-library directory: " +
					commonLibraryDirectory
			);
		}
 
		global.commonLibraryDirectory = commonLibraryDirectory;
	}
 
	//模板目录与静态资源目录默认在传入的web工程路径下查找
	webappDirectoryList = webappDirectory.split(',');
 
	Iif (webappDirectoryList.length > 2) {
		throw new Error(
			'The count of webapp directory must be less than or equal 2'
		);
	}
 
	Iif (webappDirectoryList.length === 2) {
		global.multiWebappDevServerPorts = [9981, 9982];
		global.multiWebappBaPorts = [7791, 7792];
	}
 
	webappDirectoryList.forEach(webappDirectoryPath => {
		Iif (!fs.existsSync(webappDirectoryPath)) {
			throw new ValidationError(
				"can't find the webapp directory: " + webappDirectoryPath
			);
		}
 
		const templateViewSrcPagePath = path.join(
			webappDirectoryPath,
			'views/src'
		);
 
		if (!fs.existsSync(templateViewSrcPagePath)) {
			throw new ValidationError(
				"can't find the webapp template directory: " +
					templateViewSrcPagePath
			);
		}
 
		const staticFilesDirectory = path.join(webappDirectoryPath, 'static');
		Iif (!fs.existsSync(staticFilesDirectory)) {
			throw new ValidationError(
				"can't find the static files directory " + staticFilesDirectory
			);
		}
 
		Iif (!fs.existsSync(path.join(staticFilesDirectory, 'src'))) {
			throw new ValidationError(
				"can't find 'src' directory in staticDirectory " +
					staticFilesDirectory
			);
		}
	});
 
	global.webappDirectoryList = webappDirectoryList;
 
	const reactEntryMap = {};
	const ssrEntryMap = {};
	webappDirectoryList.forEach(function (webappDirectoryPath) {
		const templateViewSrcPagePath = path.join(
			webappDirectoryPath,
			'views/src'
		);
		const regexpStaticFilesPrefix = utils.getRegexpStaticFilesPrefix();
		const staticDirectory = path.join(webappDirectoryPath, 'static');
		const staticJSSrcDirectory = path.join(
			staticDirectory,
			global.srcPrefix,
			'js'
		);
		const tplKey = utils.normalizePath(templateViewSrcPagePath);
		reactEntryMap[tplKey] = {};
		ssrEntryMap[tplKey] = {};
 
		const fileList = utils.getAllFilesByDir(
			templateViewSrcPagePath,
			utils.ssrTemplateExtensionList
		);
 
		for (const tplPath of fileList) {
			Eif (
				utils.ssrTemplateExtensionList.includes(
					path.extname(tplPath.toLowerCase())
				)
			) {
				const tplPathWithoutPrefix = tplPath.replace(
					templateViewSrcPagePath,
					''
				);
				const ssrEntryKey =
					'deploy' +
					path.join(
						path.dirname(tplPathWithoutPrefix),
						path.basename(
							tplPathWithoutPrefix,
							path.extname(tplPathWithoutPrefix)
						)
					);
				ssrEntryMap[tplKey][ssrEntryKey] =
					'./src' + tplPathWithoutPrefix;
			}
 
			const tplContent = fs.readFileSync(tplPath).toString();
			const scriptElementMatches = [
				...tplContent.matchAll(utils.getRegexpScriptElements())
			];
 
			for (const scriptElementMatchesItem of scriptElementMatches) {
				const [scriptElement] = scriptElementMatchesItem;
				const scriptElementSrcMatches = [
					...scriptElement.matchAll(
						utils.getRegexpScriptElementSrcAttrValue()
					)
				];
 
				for (const scriptElementSrcMatchesItem of scriptElementSrcMatches) {
					const [, scriptElementSrc] = scriptElementSrcMatchesItem;
					const scriptElementLowerCase = scriptElement.toLowerCase();
					Iif (
						scriptElementLowerCase.indexOf('release="false"') >
							-1 &&
						scriptElementLowerCase.indexOf('bundle') === -1
					) {
						continue;
					}
 
					if (
						!global.localStaticResourcesPrefix.test(
							scriptElementSrc
						) ||
						!scriptElementSrc.includes('bundle')
					) {
						continue;
					}
 
					const jsPath = scriptElementSrc.replace(
						regexpStaticFilesPrefix,
						''
					);
					const extname = path.extname(scriptElementSrc);
					const bundleName = `main${extname}`;
 
					let jsSrcPath = utils
						.normalizePath(
							path.join(
								staticDirectory,
								path.dirname(jsPath),
								bundleName
							)
						)
						.replace(global.deployPrefix, global.srcPrefix)
						.replace(staticJSSrcDirectory, '');
 
					const jsDeployPath = utils
						.normalizePath(path.dirname(jsSrcPath))
						.replace(utils.normalizePath(staticJSSrcDirectory), '');
 
					let entryKey = path.join(jsDeployPath, 'bundle');
 
					Eif (entryKey.startsWith('/')) {
						entryKey = entryKey.replace('/', '');
					}
 
					Eif (jsSrcPath.startsWith('/')) {
						jsSrcPath = jsSrcPath.replace('/', './');
					}
 
					reactEntryMap[tplKey][entryKey] = jsSrcPath;
				}
			}
		}
	});
 
	global.livereloadPort =
		typeof options.livereloadPort !== 'undefined' &&
		utils.isInt(options.livereloadPort)
			? parseInt(options.livereloadPort)
			: defaultLiveReloadPort;
 
	return {
		reactEntryMap,
		ssrEntryMap
	};
};