• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2025 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.
15
16import argparse
17import os
18import shutil
19import subprocess
20import sys
21import tarfile
22
23
24def copy_files(source_path, dest_path, is_file=False):
25    try:
26        if is_file:
27            shutil.copy(source_path, dest_path)
28        else:
29            shutil.copytree(source_path, dest_path, dirs_exist_ok=True,
30                symlinks=True)
31    except Exception as err:
32        raise Exception("Copy files failed. Error: " + str(err)) from err
33
34
35def run_cmd(cmd, execution_path=None):
36    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
37                           stdin=subprocess.PIPE,
38                           stderr=subprocess.PIPE,
39                           cwd=execution_path)
40    stdout, stderr = proc.communicate(timeout=300)
41    if proc.returncode != 0:
42        raise Exception(stderr.decode())
43
44
45def build(options):
46    build_cmd = [options.npm, 'run', 'run']
47    run_cmd(build_cmd, options.source_path)
48
49
50def copy_output(options):
51    run_cmd(['rm', '-rf', options.output_path])
52    copy_files(os.path.join(options.source_path, 'dist'),
53               os.path.join(options.output_path, 'dist'))
54
55    copy_files(os.path.join(options.source_path, 'node_modules', 'json5'),
56               os.path.join(options.output_path, 'node_modules', 'json5'))
57
58    copy_files(os.path.join(options.source_path, 'package.json'),
59               os.path.join(options.output_path, 'package.json'), True)
60
61    if options.current_os == "mingw" :
62        copy_files(os.path.join(options.root_out_dir, 'libts_bindings.dll'),
63                os.path.join(options.output_path, 'ts_bindings.node'), True)
64        copy_files(os.path.join(options.root_out_dir, 'libpublic.dll'),
65                os.path.join(options.output_path, 'public.node'), True)
66
67    if options.current_os == "linux" or options.current_os == "mac":
68        copy_files(os.path.join(options.root_out_dir, 'ts_bindings.node'),
69                os.path.join(options.output_path, 'ts_bindings.node'), True)
70        copy_files(os.path.join(options.root_out_dir, 'public.node'),
71                os.path.join(options.output_path, 'public.node'), True)
72
73
74def parse_args():
75    parser = argparse.ArgumentParser()
76    parser.add_argument('--npm', help='path to a npm exetuable')
77    parser.add_argument('--source-path', help='path to build system source')
78    parser.add_argument('--output-path', help='path to output')
79    parser.add_argument('--current-os', help='current os')
80    parser.add_argument('--root-out-dir', help='root_out_dir')
81
82    options = parser.parse_args()
83    return options
84
85
86def main():
87    options = parse_args()
88
89    build(options)
90    copy_output(options)
91
92
93if __name__ == '__main__':
94    sys.exit(main())
95