• 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});
34
35NapiLog.init(ops.loglevel, path.join("" + ops.out, "napi_gen.log"))
36
37let fileNames = ops.filename;
38var pathDir = ops.directory;
39var imports = ops.imports;
40if (fileNames == null && pathDir == null) {
41    NapiLog.logInfo("fileNames and pathDir both cannot be empty at the same time");
42} else if (pathDir != '') {
43    readDirFiles();
44} else if (fileNames != '') {
45    readFiles();
46}
47
48function readFiles() {
49    fileNames = fileNames.replace(/(^\s*)|(\s*$)/g, ''); // trim before and after espace
50    let regex = ',';
51    let filenameArray = fileNames.toString().split(regex);
52
53    let n = filenameArray.length;
54    for (let i = 0; i < n; i++) {
55        let fileName = filenameArray[i];
56        if (fileName !== ' ') {
57            fileName = fileName.replace(/(^\s*)|(\s*$)/g, '');
58            checkGenerate(fileName);
59        }
60    }
61}
62
63function readDirFiles() {
64    fs.readdir(pathDir + '', function (err, files) {
65        if (err) {
66            NapiLog.logError('readdir file error' + err);
67            return;
68        }
69        (function iterator(i) {
70            if (i === files.length) {
71                return;
72            }
73            fs.stat(path.join(pathDir + '', files[i]), function (err, data) {
74                if (err) {
75                    NapiLog.logError('read file error' + err);
76                    return;
77                }
78                if (data.isFile()) {
79                    let fileName = files[i];
80                    checkGenerate(pathDir + '/' + fileName);
81                }
82                iterator(i + 1);
83            });
84        })(0);
85    });
86}
87
88function checkGenerate(fileName) {
89    NapiLog.logInfo("check file []".format(fileName))
90    let suffix = fileName.split('.').pop().toLowerCase();
91    if (suffix === 'h') {
92        NapiLog.logInfo("convert .h file to .ts file...")
93        tsMain.doGenerate(fileName, ops.out);
94        return;
95    }
96    let fn = re.getFileInPath(fileName);
97    let tt = re.match('@ohos([.a-z_A-Z0-9]+).d.ts', fn);
98    if (tt) {
99        let result = checkFileError(fileName);
100        if (result[0]) {
101            main.doGenerate(fileName, ops.out, imports, ops.numbertype);
102        }
103        else {
104            NapiLog.logError(result[1]);
105        }
106
107    }
108    else {
109        NapiLog.logError('file name ' + fn + ' format invalid in function of checkGenerate!');
110    }
111}
112
113let ret = NapiLog.getResult();
114if (ret[0]) {
115    print('success');
116    NapiLog.logInfo('success');
117}
118else {
119    print('fail\n' + ret[1]);
120    NapiLog.logInfo('fail\n' + ret[1]);
121}
122