• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15const main = require("./main");
16const tsMain = require("../tsGen/tsMain");
17const re = require("./tools/re");
18const { checkFileError } = require("./tools/common");
19const { NapiLog } = require("./tools/NapiLog");
20const path = require("path");
21const stdio = require("stdio");
22var fs = require('fs');
23const { print } = require("./tools/tool");
24
25let ops = stdio.getopt({
26    'filename': { key: 'f', args: 1, description: ".d.ts file", default: "" },
27    'directory': { key: 'd', args: 1, description: ".d.ts directory", default: "" },
28    'imports': { key: 'i', args: 1, description: "enable or disable support imports self-define file", default: false },
29    'out': { key: 'o', args: 1, description: "output directory", default: "." },
30    'loglevel': { key: 'l', args: 1, description: "Log Level : 0~3", default: "1" },
31    // 新增控制number类型转C++类型参数
32    'numbertype':{key: 'n', args: 1, description: "optional elemtype: basic cpp elemtype", default: "uint32_t"},
33    'tsGen':{key: 't', args: 1, description: "enable or disable generate typescript file", default: false }
34});
35
36NapiLog.init(ops.loglevel, path.join("" + ops.out, "napi_gen.log"))
37
38let fileNames = ops.filename;
39var pathDir = ops.directory;
40var imports = ops.imports;
41if (fileNames == null && pathDir == null) {
42    NapiLog.logInfo("fileNames and pathDir both cannot be empty at the same time");
43} else if (pathDir != '') {
44    readDirFiles();
45} else if (fileNames != '') {
46    readFiles();
47}
48
49function readFiles() {
50    fileNames = fileNames.replace(/(^\s*)|(\s*$)/g, ''); // trim before and after espace
51    let regex = ',';
52    let filenameArray = fileNames.toString().split(regex);
53
54    let n = filenameArray.length;
55    for (let i = 0; i < n; i++) {
56        let fileName = filenameArray[i];
57        if (fileName !== ' ') {
58            fileName = fileName.replace(/(^\s*)|(\s*$)/g, '');
59            checkGenerate(fileName);
60        }
61    }
62}
63
64function handleDirFiles(files) {
65    if (0 === files.length) {
66        NapiLog.logInfo('[Func: readDirFiles] No files in path  %s!'.format(pathDir));
67        return;
68    }
69    (function iterator(i) {
70        if (i === files.length) {
71            return;
72        }
73        let data = fs.statSync(path.join(pathDir + '', files[i]))
74        if (data.isFile()) {
75            let fileName = files[i];
76            checkGenerate(pathDir + '/' + fileName);
77        }
78        iterator(i + 1);
79    })(0);
80}
81
82function readDirFiles() {
83    let fileList;
84    try {
85        fileList = fs.readdirSync(pathDir + '');
86    } catch (err) {
87        NapiLog.logError('readdir file error ' + err);
88        return;
89    }
90
91    handleDirFiles(fileList);
92}
93
94function checkGenerate(fileName) {
95    NapiLog.logInfo("check file []".format(fileName))
96    let suffix = fileName.split('.').pop().toLowerCase();
97    if (ops.tsGen == 'true' && suffix === 'h') {
98        NapiLog.logInfo("convert .h file to .ts file...")
99        tsMain.doGenerate(fileName, ops.out);
100        return;
101    }
102    let fn = re.getFileInPath(fileName);
103    let tt = re.match('(@ohos\.)*([.a-z_A-Z0-9]+).d.ts', fn);
104    if (tt) {
105        let result = checkFileError(fileName);
106        if (result[0]) {
107            main.doGenerate(fileName, ops.out, imports, ops.numbertype);
108        }
109        else {
110            NapiLog.logError(result[1]);
111        }
112
113    }
114    else {
115        NapiLog.logError('file name ' + fn + ' format invalid in function of checkGenerate!');
116    }
117}
118
119let ret = NapiLog.getResult();
120if (ret[0]) {
121    print('success');
122    NapiLog.logInfo('success');
123}
124else {
125    print('fail\n' + ret[1]);
126    NapiLog.logInfo('fail\n' + ret[1]);
127}
128