• 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 path = require("path");
16const stdio = require("stdio");
17const fs = require('fs');
18
19const { NapiLog } = require("../tools/NapiLog");
20const { print } = require("../tools/tool");
21const analyze = require("./analyze");
22
23let ops = stdio.getopt({
24    'filename': { key: 'f', args: 1, description: ".d.ts file", default: "" },
25    'directory': { key: 'd', args: 1, description: ".d.ts directory", default: "" },
26    'out': { key: 'o', args: 1, description: "output directory", default: "." },
27    'loglevel': { key: 'l', args: 1, description: "Log Level: 0~3", default: "1" },
28    'serviceId': { key: 's', args: 1, description: "service register id: 9000~16777214", default: "9000" }
29});
30
31NapiLog.init(ops.loglevel, path.join("" + ops.out, "napi_gen.log"));
32
33let fileNames = ops.filename;
34var pathDir = ops.directory;
35if (fileNames == null && pathDir == null) {
36    NapiLog.logInfo("fileNames and pathDir both cannot be empty at the same time");
37} else if (pathDir != '') {
38    readDirFiles();
39} else if (fileNames != '') {
40    readFiles();
41}
42
43function readFiles() {
44    fileNames = fileNames.replace(/(^\s*)|(\s*$)/g, ''); // trim before and after espace
45    let regex = ',';
46    let filenameArray = fileNames.toString().split(regex);
47
48    let n = filenameArray.length;
49    for (let i = 0; i < n; i++) {
50        let fileName = filenameArray[i];
51        if (fileName !== ' ') {
52            fileName = fileName.replace(/(^\s*)|(\s*$)/g, '');
53            checkGenerate(fileName);
54        }
55    }
56}
57
58function readDirFiles() {
59    fs.readdir(pathDir + '', function (err, files) {
60        if (err) {
61            NapiLog.logError('readdir file error' + err);
62            return;
63        }
64        (function iterator(i) {
65            if (i === files.length) {
66                return;
67            }
68            fs.stat(path.join(pathDir + '', files[i]), function (err, data) {
69                if (err) {
70                    NapiLog.logError('read file error' + err);
71                    return;
72                }
73                if (data.isFile()) {
74                    let fileName = files[i];
75                    checkGenerate(pathDir + '/' + fileName);
76                }
77                iterator(i + 1);
78            });
79        })(0);
80    });
81}
82
83function checkGenerate(fileName) {
84    NapiLog.logInfo("check file []".format(fileName));
85    let suffix = fileName.split('.').pop().toLowerCase();
86    if (suffix === 'h') {
87        NapiLog.logInfo("Generating service code from file " + fileName);
88        analyze.doAnalyze(fileName, ops);
89    } else {
90        NapiLog.logError('Only .h file is supported.');
91    }
92}
93
94let ret = NapiLog.getResult();
95if (ret[0]) {
96    print('success');
97    NapiLog.logInfo('success');
98}
99else {
100    print('Finish with error: ' + ret[1]);
101    NapiLog.logInfo('Finish with error: ' + ret[1]);
102}
103