13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci"use strict";
173af6ab5fSopenharmony_ciconst path = require("path");
183af6ab5fSopenharmony_ciconst fs = require("fs");
193af6ab5fSopenharmony_ciconst spawn = require('child_process').spawn;
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_cilet isWin = !1;
223af6ab5fSopenharmony_cilet isMac = !1;
233af6ab5fSopenharmony_ci
243af6ab5fSopenharmony_cilet args = process.argv.splice(2);
253af6ab5fSopenharmony_cilet es2abc;
263af6ab5fSopenharmony_ciif (args[0].startsWith("es2abc=")) {
273af6ab5fSopenharmony_ci    es2abc = args[0].replace("es2abc=", "");
283af6ab5fSopenharmony_ci    args = args.splice(1);
293af6ab5fSopenharmony_ci} else {
303af6ab5fSopenharmony_ci    const arkDir = path.resolve(__dirname);
313af6ab5fSopenharmony_ci    if (fs.existsSync(path.join(arkDir, 'build-win'))) {
323af6ab5fSopenharmony_ci        isWin = !0;
333af6ab5fSopenharmony_ci    } else if (fs.existsSync(path.join(arkDir, 'build-mac'))) {
343af6ab5fSopenharmony_ci        isMac = !0;
353af6ab5fSopenharmony_ci    } else if (!fs.existsSync(path.join(arkDir, 'build'))) {
363af6ab5fSopenharmony_ci        throw Error('find build fail').message;
373af6ab5fSopenharmony_ci    }
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ci    if (isWin) {
403af6ab5fSopenharmony_ci        es2abc = path.join(arkDir, 'build-win', 'bin', 'es2abc.exe');
413af6ab5fSopenharmony_ci    } else if (isMac) {
423af6ab5fSopenharmony_ci        es2abc = path.join(arkDir, 'build-mac', 'bin', 'es2abc');
433af6ab5fSopenharmony_ci    } else {
443af6ab5fSopenharmony_ci        es2abc = path.join(arkDir, 'build', 'bin', 'es2abc');
453af6ab5fSopenharmony_ci    }
463af6ab5fSopenharmony_ci}
473af6ab5fSopenharmony_ci
483af6ab5fSopenharmony_cifunction callEs2abc(args) {
493af6ab5fSopenharmony_ci    if (!fs.existsSync(es2abc)) {
503af6ab5fSopenharmony_ci        throw Error('find arkcompiler fail').message;
513af6ab5fSopenharmony_ci    }
523af6ab5fSopenharmony_ci    let proc = spawn(`${es2abc}`, args);
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_ci    proc.stderr.on('data', (data) => {
553af6ab5fSopenharmony_ci        throw Error(`${data}`).message;
563af6ab5fSopenharmony_ci    });
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_ci    proc.stdout.on('data', (data) => {
593af6ab5fSopenharmony_ci        process.stdout.write(`${data}`);
603af6ab5fSopenharmony_ci    });
613af6ab5fSopenharmony_ci}
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci// keep bc-version to be compatible with old IDE versions
643af6ab5fSopenharmony_ciif (args.length == 1 && args[0] == "--bc-version") {
653af6ab5fSopenharmony_ci    callEs2abc(args);
663af6ab5fSopenharmony_ci    return;
673af6ab5fSopenharmony_ci}
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_cilet es2abcArgs = [];
703af6ab5fSopenharmony_cies2abcArgs.push("--target-bc-version")
713af6ab5fSopenharmony_ci
723af6ab5fSopenharmony_cifor (let index = 0 ; index < args.length; index += 2) {
733af6ab5fSopenharmony_ci    if (args[index] == "--target-api-version") {
743af6ab5fSopenharmony_ci        if (args[index + 1] == "8") {
753af6ab5fSopenharmony_ci            process.stdout.write("0.0.0.2");
763af6ab5fSopenharmony_ci            return;
773af6ab5fSopenharmony_ci        } else {
783af6ab5fSopenharmony_ci            es2abcArgs.push("--target-api-version", args[index + 1]);
793af6ab5fSopenharmony_ci        }
803af6ab5fSopenharmony_ci    } else if (args[index] == "--target-api-sub-version") {
813af6ab5fSopenharmony_ci        es2abcArgs.push("--target-api-sub-version", args[index + 1]);
823af6ab5fSopenharmony_ci    }
833af6ab5fSopenharmony_ci}
843af6ab5fSopenharmony_ci
853af6ab5fSopenharmony_cicallEs2abc(es2abcArgs);
86