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*/ 15 16const tsMain = require('./tsMain'); 17const { NapiLog } = require('./tools/NapiLog'); 18const path = require('path'); 19const stdio = require('stdio'); 20let fs = require('fs'); 21const util = require('util'); 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 'tsGen':{key: 't', args: 1, description: 'enable or disable generate typescript file', default: false }, 29}); 30 31let vscode = null; 32try { 33 vscode = require('vscode'); 34} 35catch (err) { 36 vscode = null; 37} 38 39NapiLog.init(ops.loglevel, path.join('' + ops.out, 'napi_gen.log')); 40 41let fileNames = ops.filename; 42let pathDir = ops.directory; 43if (fileNames == null && pathDir == null) { 44 NapiLog.logInfo('fileNames and pathDir both cannot be empty at the same time'); 45} else if (pathDir !== '') { 46 readDirFiles(); 47} else if (fileNames !== '') { 48 readFiles(); 49} 50 51function print(...args) { 52 if (vscode) { 53 vscode.window.showInformationMessage(...args); 54 } 55 console.log(...args); 56} 57 58function readFiles() { 59 fileNames = fileNames.replace(/(^\s*)|(\s*$)/g, ''); // trim before and after espace 60 let regex = ','; 61 let filenameArray = fileNames.toString().split(regex); 62 63 let n = filenameArray.length; 64 for (let i = 0; i < n; i++) { 65 let fileName = filenameArray[i]; 66 if (fileName !== ' ') { 67 fileName = fileName.replace(/(^\s*)|(\s*$)/g, ''); 68 checkGenerate(fileName); 69 } 70 } 71} 72 73function handleDirFiles(files) { 74 if (0 === files.length) { 75 NapiLog.logInfo(util.format('[Func: readDirFiles] No files in path %s!', pathDir)); 76 return; 77 } 78 (function iterator(i) { 79 if (i === files.length) { 80 return; 81 } 82 let data = fs.statSync(path.join(pathDir + '', files[i])); 83 if (data.isFile()) { 84 let fileName = files[i]; 85 checkGenerate(pathDir + '/' + fileName); 86 } 87 iterator(i + 1); 88 })(0); 89} 90 91function readDirFiles() { 92 let fileList; 93 try { 94 fileList = fs.readdirSync(pathDir + ''); 95 } catch (err) { 96 NapiLog.logError('readdir file error ' + err); 97 return; 98 } 99 100 handleDirFiles(fileList); 101} 102 103function checkGenerate(fileName) { 104 NapiLog.logInfo(util.format('check file []', fileName)); 105 let suffix = fileName.split('.').pop().toLowerCase(); 106 if (suffix === 'h') { 107 NapiLog.logInfo('convert .h file to .ts file...'); 108 tsMain.doGenerate(fileName, ops.out); 109 return; 110 } 111 else { 112 NapiLog.logError('file name ' + fileName + ' format invalid in function of checkGenerate!'); 113 } 114} 115 116let ret = NapiLog.getResult(); 117if (ret[0]) { 118 print('success'); 119 NapiLog.logInfo('success'); 120} 121else { 122 print('fail\n' + ret[1]); 123 NapiLog.logInfo('fail\n' + ret[1]); 124} 125