1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2022 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15import os
16import shutil
17import platform
18import argparse
19import subprocess
20import sys
21
22def run_command(in_cmd):
23    print(" ".join(in_cmd))
24    proc = subprocess.Popen(in_cmd, stdout=subprocess.PIPE,
25                          stderr=subprocess.PIPE,
26                          universal_newlines=True,
27                          shell=False)
28    stdout, stderr = proc.communicate()
29    if stdout != "":
30        raise Exception(stdout)
31
32if __name__ == '__main__':
33    PARSER_INST = argparse.ArgumentParser()
34    PARSER_INST.add_argument('--dst-file',
35                        help='the converted target file')
36    PARSER_INST.add_argument('--module-path',
37                        help='the module path')
38    PARSER_INST.add_argument('--out-file',
39                        help='js output file')
40    PARSER_INST.add_argument('--out-filePath',
41                        help='js output filePath')
42    PARSER_INST.add_argument('--relative-path',
43                        help='the code root path relative the root_build_dir')
44    INPUT_ARGUMENTS = PARSER_INST.parse_args()
45
46    BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), INPUT_ARGUMENTS.relative_path))
47    os.chdir(("%s" + INPUT_ARGUMENTS.module_path) % BUILD_PATH)
48    os_name = sys.platform
49    NODE_PATH = '../../../../prebuilts/build-tools/common/nodejs/current/bin/node'
50    if not os.path.exists(NODE_PATH):
51        raise Exception('error:NO such file or directory')
52    TSC_PATH = '../../../../third_party/typescript/bin/tsc'
53    CMD_INST = [NODE_PATH, TSC_PATH, "--outDir", INPUT_ARGUMENTS.out_filePath]
54    run_command(CMD_INST)
55    if not os.path.exists(INPUT_ARGUMENTS.out_file):
56        raise Exception('error:NO such file or directory')
57    CMD_INST = shutil.copy(INPUT_ARGUMENTS.out_file, INPUT_ARGUMENTS.dst_file)
58
59    CMD_INST = shutil.rmtree(INPUT_ARGUMENTS.out_filePath)
60
61    exit(0)
62