• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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
16const fs = require('fs');
17const childProcess = require('child_process');
18const cluster = require('cluster');
19const SUCCESS = 0;
20const FAIL = 1;
21const red = '\u001b[31m';
22const reset = '\u001b[39m';
23
24function js2abcByWorkers(jsonInput, cmd) {
25  const inputPaths = JSON.parse(jsonInput);
26  for (let i = 0; i < inputPaths.length; ++i) {
27    // for matching debug info mechanism
28    const input = inputPaths[i].path.replace(/\.temp\.js$/, "_.js");
29    const cacheOutputPath = inputPaths[i].cacheOutputPath;
30    const cacheAbcFilePath = cacheOutputPath.replace(/\.temp\.js$/, ".abc");
31    const sourceFile = inputPaths[i].sourceFile;
32    const singleCmd = `${cmd} "${cacheOutputPath}" -o "${cacheAbcFilePath}" --source-file "${sourceFile}"`;
33    try {
34      childProcess.execSync(singleCmd);
35    } catch (e) {
36      console.debug(red, `ERROR Failed to convert file ${input} to abc. Error message: ${e}`, reset);
37      process.exit(FAIL);
38    }
39  }
40}
41
42if (cluster.isWorker && process.env["inputs"] !== undefined && process.env["cmd"] !== undefined) {
43  js2abcByWorkers(process.env["inputs"], process.env["cmd"]);
44  process.exit(SUCCESS);
45}
46