14d6c458bSopenharmony_ci#!/usr/bin/env python
24d6c458bSopenharmony_ci# -*- coding: utf-8 -*-
34d6c458bSopenharmony_ci# Copyright (c) 2022 Huawei Device Co., Ltd.
44d6c458bSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
54d6c458bSopenharmony_ci# you may not use this file except in compliance with the License.
64d6c458bSopenharmony_ci# You may obtain a copy of the License at
74d6c458bSopenharmony_ci#
84d6c458bSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
94d6c458bSopenharmony_ci#
104d6c458bSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
114d6c458bSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
124d6c458bSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134d6c458bSopenharmony_ci# See the License for the specific language governing permissions and
144d6c458bSopenharmony_ci# limitations under the License.
154d6c458bSopenharmony_ci
164d6c458bSopenharmony_ciimport os
174d6c458bSopenharmony_ciimport shutil
184d6c458bSopenharmony_ciimport platform
194d6c458bSopenharmony_ciimport argparse
204d6c458bSopenharmony_ciimport subprocess
214d6c458bSopenharmony_ciimport sys
224d6c458bSopenharmony_ci
234d6c458bSopenharmony_cidef run_command(in_cmd):
244d6c458bSopenharmony_ci    print(" ".join(in_cmd))
254d6c458bSopenharmony_ci    proc = subprocess.Popen(in_cmd, stdout=subprocess.PIPE,
264d6c458bSopenharmony_ci                          stderr=subprocess.PIPE,
274d6c458bSopenharmony_ci                          universal_newlines=True,
284d6c458bSopenharmony_ci                          shell=False)
294d6c458bSopenharmony_ci    stdout, stderr = proc.communicate()
304d6c458bSopenharmony_ci    if stdout != "":
314d6c458bSopenharmony_ci        raise Exception(stdout)
324d6c458bSopenharmony_ci
334d6c458bSopenharmony_ciif __name__ == '__main__':
344d6c458bSopenharmony_ci    PARSER_INST = argparse.ArgumentParser()
354d6c458bSopenharmony_ci    PARSER_INST.add_argument('--dst-file',
364d6c458bSopenharmony_ci                        help='the converted target file')
374d6c458bSopenharmony_ci    PARSER_INST.add_argument('--module-path',
384d6c458bSopenharmony_ci                        help='the module path')
394d6c458bSopenharmony_ci    PARSER_INST.add_argument('--out-file',
404d6c458bSopenharmony_ci                        help='js output file')
414d6c458bSopenharmony_ci    PARSER_INST.add_argument('--out-filePath',
424d6c458bSopenharmony_ci                        help='js output filePath')
434d6c458bSopenharmony_ci    PARSER_INST.add_argument('--relative-path',
444d6c458bSopenharmony_ci                        help='the code root path relative the root_build_dir')
454d6c458bSopenharmony_ci    INPUT_ARGUMENTS = PARSER_INST.parse_args()
464d6c458bSopenharmony_ci
474d6c458bSopenharmony_ci    BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), INPUT_ARGUMENTS.relative_path))
484d6c458bSopenharmony_ci    os.chdir(("%s" + INPUT_ARGUMENTS.module_path) % BUILD_PATH)
494d6c458bSopenharmony_ci    os_name = sys.platform
504d6c458bSopenharmony_ci
514d6c458bSopenharmony_ci    NODE_PATH = '../../../../prebuilts/build-tools/common/nodejs/current/bin/node'
524d6c458bSopenharmony_ci    if not os.path.exists(NODE_PATH):
534d6c458bSopenharmony_ci        raise Exception('error:NO such file or directory')
544d6c458bSopenharmony_ci    TSC_PATH = '../../../../third_party/typescript/bin/tsc'
554d6c458bSopenharmony_ci    CMD_INST = [NODE_PATH, TSC_PATH, "--outDir", INPUT_ARGUMENTS.out_filePath]
564d6c458bSopenharmony_ci    run_command(CMD_INST)
574d6c458bSopenharmony_ci    if not os.path.exists(INPUT_ARGUMENTS.out_file):
584d6c458bSopenharmony_ci        raise Exception('error:NO such file or directory')
594d6c458bSopenharmony_ci    CMD_INST = shutil.copy(INPUT_ARGUMENTS.out_file, INPUT_ARGUMENTS.dst_file)
604d6c458bSopenharmony_ci
614d6c458bSopenharmony_ci    CMD_INST = shutil.rmtree(INPUT_ARGUMENTS.out_filePath)
624d6c458bSopenharmony_ci    exit(0)
63