diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index d39de1a..6dbb3cf 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -32,7 +32,9 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { const inputPaths: any = JSON.parse(jsonInput); for (let i = 0; i < inputPaths.length; ++i) { const input: string = inputPaths[i].path; - const singleCmd: any = `${cmd} "${input}"`; + const cacheOutputPath: string = inputPaths[i].cacheOutputPath; + const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, ".abc"); + const singleCmd: any = `${cmd} "${cacheOutputPath}" -o "${cacheAbcFilePath}" --source-file "${input}"`; logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); try { childProcess.execSync(singleCmd); @@ -40,16 +42,6 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); process.exit(FAIL); } - - const abcFile: string = input.replace(/\.js$/, '.abc'); - if (fs.existsSync(abcFile)) { - const abcFileNew: string = abcFile.replace(/_.abc$/, '.abc'); - fs.copyFileSync(abcFile, abcFileNew); - fs.unlinkSync(abcFile); - } else { - logger.error(red, `ETS:ERROR ${abcFile} is lost`, reset); - process.exit(FAIL); - } } return; @@ -59,8 +51,9 @@ function es2abcByWorkers(jsonInput: string, cmd: string): Promise { const inputPaths: any = JSON.parse(jsonInput); for (let i = 0; i < inputPaths.length; ++i) { const input: string = inputPaths[i].path; - const abcFile: string = input.replace(/_.js$/, '.abc'); - const singleCmd: any = `${cmd} "${input}" --output "${abcFile}"`; + const cacheOutputPath: string = inputPaths[i].cacheOutputPath; + const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, ".abc"); + const singleCmd: any = `${cmd} "${cacheOutputPath}" --output "${cacheAbcFilePath}" --source-file "${input}"`; logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); try { childProcess.execSync(singleCmd); diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 477ef2e..b07eec7 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -28,7 +28,9 @@ import { genAbcFileName, mkdirsSync, genSourceMapFileName, - checkNodeModulesFile + checkNodeModulesFile, + compareNodeVersion, + removeDir } from './utils'; import { projectConfig } from '../main'; import { @@ -46,7 +48,8 @@ import { FAIL, EXTNAME_JS_MAP, TS2ABC, - ES2ABC + ES2ABC, + TEMPORARY } from './pre_define'; const firstFileEXT: string = '_.js'; @@ -62,7 +65,8 @@ let nodeJs: string; let delayCount = 0; interface File { path: string, - size: number + size: number, + cacheOutputPath: string } let intermediateJsBundle: Array = []; let fileterIntermediateJsBundle: Array = []; @@ -133,6 +137,11 @@ export class GenAbcPlugin { } } + if (!checkNodeModules()) { + process.exitCode = FAIL; + return; + } + compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { if (projectConfig.compileMode === JSBUNDLE || projectConfig.compileMode === undefined) { return; @@ -158,6 +167,7 @@ export class GenAbcPlugin { if (projectConfig.compileMode === ESMODULE) { return; } + removeDir(output); Object.keys(compilation.assets).forEach(key => { // choose *.js if (output && path.extname(key) === EXTNAME_JS) { @@ -343,14 +353,25 @@ function processEntryToGenAbc(entryInfos: Map): void { } function writeFileSync(inputString: string, output: string, jsBundleFile: string): void { - const parent: string = path.join(output, '..'); + let parent: string = path.join(output, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { mkDir(parent); } - fs.writeFileSync(output, inputString); - if (fs.existsSync(output)) { - const fileSize: any = fs.statSync(output).size; - intermediateJsBundle.push({path: output, size: fileSize}); + const sufStr: string = output.replace(projectConfig.buildPath, ''); + let cacheOutputPath: string = ""; + if (process.env.cachePath) { + cacheOutputPath = path.join(process.env.cachePath, TEMPORARY, sufStr); + } else { + cacheOutputPath = output; + } + parent = path.join(cacheOutputPath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + fs.writeFileSync(cacheOutputPath, inputString); + if (fs.existsSync(cacheOutputPath)) { + const fileSize: any = fs.statSync(cacheOutputPath).size; + intermediateJsBundle.push({path: output, size: fileSize, cacheOutputPath: cacheOutputPath}); } else { logger.error(red, `ETS:ERROR Failed to convert file ${jsBundleFile} to bin. ${output} is lost`, reset); process.exitCode = FAIL; @@ -403,12 +424,8 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number): an } function invokeWorkersModuleToGenAbc(moduleInfos: Array): void { - if (fs.existsSync(buildPathInfo)) { - fs.rmdirSync(buildPathInfo, { recursive: true}); - } - if (fs.existsSync(projectConfig.nodeModulesPath)) { - fs.rmdirSync(projectConfig.nodeModulesPath, { recursive: true}); - } + removeDir(buildPathInfo); + removeDir(projectConfig.nodeModulesPath); invokeCluterModuleToAbc(); } @@ -463,8 +480,7 @@ function invokeCluterModuleToAbc(): void { const abcArgs: string[] = initAbcEnv(); const clusterNewApiVersion: number = 16; - const currentNodeVersion: number = parseInt(process.version.split('.')[0]); - const useNewApi: boolean = currentNodeVersion >= clusterNewApiVersion; + const useNewApi: boolean = compareNodeVersion(clusterNewApiVersion); if (useNewApi && cluster.isPrimary || !useNewApi && cluster.isMaster) { if (useNewApi) { @@ -767,20 +783,21 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil for (let i = 0; i < inputPaths.length; ++i) { const input: string = inputPaths[i].path; const abcPath: string = input.replace(/_.js$/, EXTNAME_ABC); - if (!fs.existsSync(input)) { - logger.error(red, `ETS:ERROR ${input} is lost`, reset); + const cacheOutputPath: string = inputPaths[i].cacheOutputPath; + const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc'); + if (!fs.existsSync(cacheOutputPath)) { + logger.error(red, `ETS:ERROR ${cacheOutputPath} is lost`, reset); process.exitCode = FAIL; break; } - if (fs.existsSync(abcPath)) { - const hashInputContentData: any = toHashData(input); - const hashAbcContentData: any = toHashData(abcPath); - if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { - updateJsonObject[input] = hashInputContentData; - updateJsonObject[abcPath] = hashAbcContentData; - if (!projectConfig.isPreview) { - fs.unlinkSync(input); - } + if (fs.existsSync(cacheAbcFilePath)) { + const hashInputContentData: any = toHashData(cacheOutputPath); + const hashAbcContentData: any = toHashData(cacheAbcFilePath); + if (jsonObject[cacheOutputPath] === hashInputContentData && jsonObject[cacheAbcFilePath] === hashAbcContentData) { + updateJsonObject[cacheOutputPath] = hashInputContentData; + updateJsonObject[cacheAbcFilePath] = hashAbcContentData; + mkdirsSync(path.dirname(abcPath)); + fs.copyFileSync(cacheAbcFilePath, abcPath); } else { fileterIntermediateJsBundle.push(inputPaths[i]); } @@ -797,18 +814,28 @@ function writeHashJson(): void { for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { const input:string = fileterIntermediateJsBundle[i].path; const abcPath: string = input.replace(/_.js$/, EXTNAME_ABC); - if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { - logger.error(red, `ETS:ERROR ${input} is lost`, reset); + const cacheOutputPath: string = fileterIntermediateJsBundle[i].cacheOutputPath; + const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc'); + if (!fs.existsSync(cacheOutputPath) || !fs.existsSync(cacheAbcFilePath)) { + logger.error(red, `ETS:ERROR ${cacheOutputPath} is lost`, reset); process.exitCode = FAIL; break; } - const hashInputContentData: any = toHashData(input); - const hashAbcContentData: any = toHashData(abcPath); - hashJsonObject[input] = hashInputContentData; - hashJsonObject[abcPath] = hashAbcContentData; - if (!projectConfig.isPreview) { - fs.unlinkSync(input); + const hashInputContentData: any = toHashData(cacheOutputPath); + const hashAbcContentData: any = toHashData(cacheAbcFilePath); + hashJsonObject[cacheOutputPath] = hashInputContentData; + hashJsonObject[cacheAbcFilePath] = hashAbcContentData; + } + for (let i = 0; i < intermediateJsBundle.length; ++i) { + const abcFile: string = intermediateJsBundle[i].path.replace(/\_.js$/, ".abc"); + const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\_.js$/, ".abc"); + if (!fs.existsSync(cacheAbcFilePath)) { + logger.error(red, `ETS:ERROR ${cacheAbcFilePath} is lost`, reset); + process.exitCode = FAIL; + break; } + mkdirsSync(path.dirname(abcFile)); + fs.copyFileSync(cacheAbcFilePath, abcFile); } const hashFilePath: string = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { @@ -841,3 +868,22 @@ function genHashJsonPath(buildPath: string): string { return ''; } } + +function checkNodeModules() { + if (process.env.panda === TS2ABC) { + let arkEntryPath: string = path.join(arkDir, 'build'); + if (isWin) { + arkEntryPath = path.join(arkDir, 'build-win'); + } else if (isMac) { + arkEntryPath = path.join(arkDir, 'build-mac'); + } + let nodeModulesPath: string = path.join(arkEntryPath, NODE_MODULES); + if (!(fs.existsSync(nodeModulesPath) && fs.statSync(nodeModulesPath).isDirectory())) { + logger.error(red, `ERROR: node_modules for ark compiler not found. + Please make sure switch to non-root user before runing "npm install" for safity requirements and try re-run "npm install" under ${arkEntryPath}`, reset); + return false; + } + } + + return true; +} diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 88d8e61..4a1a473 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -479,3 +479,22 @@ export function genSourceMapFileName(temporaryFile: string): string { } return abcFile; } + +export function compareNodeVersion(nodeVersion: number = 16): boolean { + const currentNodeVersion: number = parseInt(process.versions.node.split('.')[0]); + if (currentNodeVersion >= nodeVersion) { + return true; + } + + return false; +} + +export function removeDir(dirName: string): void { + if (fs.existsSync(dirName)) { + if (compareNodeVersion()) { + fs.rmSync(dirName, { recursive: true}); + } else { + fs.rmdirSync(dirName, { recursive: true}); + } + } +} \ No newline at end of file