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_ciimport os
164d6c458bSopenharmony_ciimport shutil
174d6c458bSopenharmony_ciimport platform
184d6c458bSopenharmony_ciimport argparse
194d6c458bSopenharmony_ciimport subprocess
204d6c458bSopenharmony_ciimport sys
214d6c458bSopenharmony_ci
224d6c458bSopenharmony_cidef run_command(in_cmd):
234d6c458bSopenharmony_ci    print(" ".join(in_cmd))
244d6c458bSopenharmony_ci    proc = subprocess.Popen(in_cmd, stdout=subprocess.PIPE,
254d6c458bSopenharmony_ci                          stderr=subprocess.PIPE,
264d6c458bSopenharmony_ci                          universal_newlines=True,
274d6c458bSopenharmony_ci                          shell=False)
284d6c458bSopenharmony_ci    stdout, stderr = proc.communicate()
294d6c458bSopenharmony_ci    if stdout != "":
304d6c458bSopenharmony_ci        raise Exception(stdout)
314d6c458bSopenharmony_ci
324d6c458bSopenharmony_ciif __name__ == '__main__':
334d6c458bSopenharmony_ci    PARSER_INST = argparse.ArgumentParser()
344d6c458bSopenharmony_ci    PARSER_INST.add_argument('--dst-file',
354d6c458bSopenharmony_ci                        help='the converted target file')
364d6c458bSopenharmony_ci    PARSER_INST.add_argument('--module-path',
374d6c458bSopenharmony_ci                        help='the module path')
384d6c458bSopenharmony_ci    PARSER_INST.add_argument('--out-file',
394d6c458bSopenharmony_ci                        help='js output file')
404d6c458bSopenharmony_ci    PARSER_INST.add_argument('--out-filePath',
414d6c458bSopenharmony_ci                        help='js output filePath')
424d6c458bSopenharmony_ci    PARSER_INST.add_argument('--relative-path',
434d6c458bSopenharmony_ci                        help='the code root path relative the root_build_dir')
444d6c458bSopenharmony_ci    INPUT_ARGUMENTS = PARSER_INST.parse_args()
454d6c458bSopenharmony_ci
464d6c458bSopenharmony_ci    BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), INPUT_ARGUMENTS.relative_path))
474d6c458bSopenharmony_ci    os.chdir(("%s" + INPUT_ARGUMENTS.module_path) % BUILD_PATH)
484d6c458bSopenharmony_ci    os_name = sys.platform
494d6c458bSopenharmony_ci    NODE_PATH = '../../../../prebuilts/build-tools/common/nodejs/current/bin/node'
504d6c458bSopenharmony_ci    if not os.path.exists(NODE_PATH):
514d6c458bSopenharmony_ci        raise Exception('error:NO such file or directory')
524d6c458bSopenharmony_ci    TSC_PATH = '../../../../third_party/typescript/bin/tsc'
534d6c458bSopenharmony_ci    CMD_INST = [NODE_PATH, TSC_PATH, "--outDir", INPUT_ARGUMENTS.out_filePath]
544d6c458bSopenharmony_ci    run_command(CMD_INST)
554d6c458bSopenharmony_ci    if not os.path.exists(INPUT_ARGUMENTS.out_file):
564d6c458bSopenharmony_ci        raise Exception('error:NO such file or directory')
574d6c458bSopenharmony_ci    CMD_INST = shutil.copy(INPUT_ARGUMENTS.out_file, INPUT_ARGUMENTS.dst_file)
584d6c458bSopenharmony_ci
594d6c458bSopenharmony_ci    CMD_INST = shutil.rmtree(INPUT_ARGUMENTS.out_filePath)
604d6c458bSopenharmony_ci
614d6c458bSopenharmony_ci    exit(0)
62