• 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 { analyzeFile } = require('./analyze');
16const { generateAppCode } = require('./generate');
17const { NapiLog } = require('./tools/NapiLog');
18const re = require('./tools/re');
19let fs = require('fs');
20
21function doGenerate(ifname, destdir, jsonCfg) {
22    // step1: analyze file
23    let structOfTs = analyzeFile(ifname);
24    let fn = re.getFileInPath(ifname);
25    let tt = re.match('(@ohos\.)*([.a-z_A-Z0-9]+).d.ts', fn);
26    if (structOfTs === undefined || structOfTs.declareNamespace.length === 0 ||
27        structOfTs.declareNamespace[0].name === undefined) {
28        NapiLog.logError('analyzeFile file fail and file name is: ' + fn);
29        return '';
30    }
31
32    // step2: generate code
33    if (tt) {
34        let moduleName = re.getReg(fn, tt.regs[2]);
35
36        structOfTs.imports = [];
37        generateAppCode(structOfTs, destdir, moduleName, jsonCfg);
38    } else {
39        NapiLog.logError('file name ' + fn + ' format invalid in function of doGenerate!');
40    }
41    return structOfTs.declareNamespace[0].name;
42}
43
44module.exports = {
45    doGenerate,
46};
47