1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as childProcess from 'child_process'; 17import * as process from 'process'; 18import * as fs from 'fs'; 19import * as path from 'path'; 20import cluster from 'cluster'; 21import { logger } from './compile_info'; 22import { 23 SUCCESS, 24 FAIL 25} from './pre_define'; 26import { 27 toUnixPath 28} from './utils'; 29 30const red: string = '\u001b[31m'; 31const reset: string = '\u001b[39m'; 32 33function js2abcByWorkers(jsonInput: string, cmd: string, workerFileName: string): Promise<void> { 34 const inputPaths: any = JSON.parse(jsonInput); 35 // cmd `${cmd} --input-file xx --output-proto --merge-abc` 36 let filePath: string = path.join(process.env.cachePath, workerFileName); 37 let content: string = ''; 38 for (let i = 0; i < inputPaths.length; ++i) { 39 let info: any = inputPaths[i]; 40 const moduleType: string = info.isCommonJs ? 'commonjs' : 'esm'; 41 content += 42 `${info.tempFilePath};${info.recordName};${moduleType};${toUnixPath(info.sourceFile)};${info.packageName}`; 43 if (i < inputPaths.length - 1) { 44 content += '\n'; 45 } 46 } 47 fs.writeFileSync(filePath, content, 'utf-8'); 48 const singleCmd: string = `${cmd} --input-file "${filePath}" --output-proto --merge-abc`; 49 logger.debug('gen abc cmd is: ', singleCmd); 50 try { 51 childProcess.execSync(singleCmd); 52 } catch (e) { 53 logger.debug(red, `ArkTS:ERROR Failed to convert file to proto `, reset); 54 process.exit(FAIL); 55 } 56 57 return; 58} 59 60logger.debug('worker data is: ', JSON.stringify(process.env)); 61logger.debug('gen_abc isWorker is: ', cluster.isWorker); 62if (cluster.isWorker && process.env.inputs !== undefined && process.env.cmd !== undefined && 63 process.env.workerFileName !== undefined && process.env.cachePath !== undefined) { 64 logger.debug('==>worker #', cluster.worker.id, 'started!'); 65 js2abcByWorkers(process.env.inputs, process.env.cmd, process.env.workerFileName); 66 process.exit(SUCCESS); 67} 68