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
16"use strict";
17const path = require("path");
18const fs = require("fs");
19const spawn = require('child_process').spawn;
20
21let isWin = !1;
22let isMac = !1;
23
24let args = process.argv.splice(2);
25let es2abc;
26if (args[0].startsWith("es2abc=")) {
27    es2abc = args[0].replace("es2abc=", "");
28    args = args.splice(1);
29} else {
30    const arkDir = path.resolve(__dirname);
31    if (fs.existsSync(path.join(arkDir, 'build-win'))) {
32        isWin = !0;
33    } else if (fs.existsSync(path.join(arkDir, 'build-mac'))) {
34        isMac = !0;
35    } else if (!fs.existsSync(path.join(arkDir, 'build'))) {
36        throw Error('find build fail').message;
37    }
38
39    if (isWin) {
40        es2abc = path.join(arkDir, 'build-win', 'bin', 'es2abc.exe');
41    } else if (isMac) {
42        es2abc = path.join(arkDir, 'build-mac', 'bin', 'es2abc');
43    } else {
44        es2abc = path.join(arkDir, 'build', 'bin', 'es2abc');
45    }
46}
47
48function callEs2abc(args) {
49    if (!fs.existsSync(es2abc)) {
50        throw Error('find arkcompiler fail').message;
51    }
52    let proc = spawn(`${es2abc}`, args);
53
54    proc.stderr.on('data', (data) => {
55        throw Error(`${data}`).message;
56    });
57
58    proc.stdout.on('data', (data) => {
59        process.stdout.write(`${data}`);
60    });
61}
62
63// keep bc-version to be compatible with old IDE versions
64if (args.length == 1 && args[0] == "--bc-version") {
65    callEs2abc(args);
66    return;
67}
68
69let es2abcArgs = [];
70es2abcArgs.push("--target-bc-version")
71
72for (let index = 0 ; index < args.length; index += 2) {
73    if (args[index] == "--target-api-version") {
74        if (args[index + 1] == "8") {
75            process.stdout.write("0.0.0.2");
76            return;
77        } else {
78            es2abcArgs.push("--target-api-version", args[index + 1]);
79        }
80    } else if (args[index] == "--target-api-sub-version") {
81        es2abcArgs.push("--target-api-sub-version", args[index + 1]);
82    }
83}
84
85callEs2abc(es2abcArgs);
86