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 cluster from 'cluster'; 19import { logger } from './compile_info'; 20import { 21 SUCCESS, 22 FAIL 23} from './pre_define'; 24 25const red: string = '\u001b[31m'; 26const reset: string = '\u001b[39m'; 27 28function js2abcByWorkers(jsonInput: string, cmd: string): Promise<void> { 29 const inputPaths: any = JSON.parse(jsonInput); 30 for (let i = 0; i < inputPaths.length; ++i) { 31 const input: string = inputPaths[i].path.replace(/\.temp\.js$/, "_.js"); 32 const cacheOutputPath: string = inputPaths[i].cacheOutputPath; 33 const cacheAbcFilePath: string = cacheOutputPath.replace(/\.temp\.js$/, ".abc"); 34 const sourceFile: string = inputPaths[i].sourceFile; 35 const singleCmd: any = `${cmd} "${cacheOutputPath}" -o "${cacheAbcFilePath}" --source-file "${sourceFile}"`; 36 logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); 37 try { 38 childProcess.execSync(singleCmd); 39 } catch (e) { 40 logger.debug(red, `ArkTS:ERROR Failed to convert file ${input} to abc. Error message: ${e} `, reset); 41 process.exit(FAIL); 42 } 43 } 44 45 return; 46} 47 48logger.debug('worker data is: ', JSON.stringify(process.env)); 49logger.debug('gen_abc isWorker is: ', cluster.isWorker); 50if (cluster.isWorker && process.env['inputs'] !== undefined && process.env['cmd'] !== undefined) { 51 logger.debug('==>worker #', cluster.worker.id, 'started!'); 52 js2abcByWorkers(process.env['inputs'], process.env['cmd']); 53 process.exit(SUCCESS); 54} 55