1/* 2 * Copyright (c) 2022 Huawei Device 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 16import fs from 'fs'; 17import path from 'path'; 18import os from 'os'; 19import { createSourceFile, ScriptTarget } from 'typescript'; 20import { collectAllFileName, getAllClassDeclaration } from './common/commonUtils'; 21import { getSourceFileAssembly } from './declaration-node/sourceFileElementsAssemply'; 22import { generateEntry } from './generate/generateEntry'; 23import { generateIndex } from './generate/generateIndex'; 24import { generateSourceFileElements } from './generate/generateMockJsFile'; 25import { generateSystemIndex } from './generate/generateSystemIndex'; 26 27const dtsFileList: Array<string> = []; 28 29/** 30 * get all api .d.ts file path 31 * @param dir 32 * @returns 33 */ 34function getAllDtsFile(dir: string): Array<string> { 35 const arr = fs.readdirSync(dir); 36 if (!dir.toString().includes('node_modules') && !dir.toString().includes('/@internal/component/')) { 37 arr.forEach(value => { 38 const fullPath = path.join(dir, value); 39 const stats = fs.statSync(fullPath); 40 if (stats.isDirectory()) { 41 getAllDtsFile(fullPath); 42 } else { 43 dtsFileList.push(fullPath); 44 } 45 }); 46 } 47 return dtsFileList; 48} 49 50/** 51 * delete the old mock file befor generate new mock file 52 * @param outDir 53 */ 54 function deleteOldMockJsFile(outDir: string) { 55 const arr = fs.readdirSync(outDir); 56 arr.forEach(value => { 57 const currPath = path.join(outDir, value); 58 const stas = fs.statSync(currPath); 59 if (stas.isDirectory()) { 60 deleteOldMockJsFile(currPath); 61 } else { 62 fs.unlink(currPath, function(err) { 63 if (err) { 64 console.log(err); 65 } 66 }); 67 } 68 }); 69} 70 71/** 72 * mkdir 73 * @param dirname 74 * @returns 75 */ 76function mkdirsSync(dirname) { 77 if (fs.existsSync(dirname)) { 78 return true; 79 } else { 80 if (mkdirsSync(path.dirname(dirname))) { 81 fs.mkdirSync(dirname); 82 return true; 83 } 84 } 85} 86 87function main() { 88 let interfaceRootDir = ''; 89 if (os.platform() === 'linux' || os.platform() === 'darwin') { 90 interfaceRootDir = __dirname.split('/out')[0]; 91 } else { 92 interfaceRootDir = __dirname.split('\\out')[0]; 93 } 94 const dtsDir = path.join(interfaceRootDir, './interface/sdk-js/api'); 95 const outMockJsFileDir = path.join(__dirname, '../../runtime/main/extend/systemplugin'); 96 deleteOldMockJsFile(outMockJsFileDir); 97 getAllDtsFile(dtsDir); 98 99 dtsFileList.forEach(value => { 100 collectAllFileName(value); 101 if (value.endsWith('.d.ts')) { 102 const code = fs.readFileSync(value); 103 const sourceFile = createSourceFile('', code.toString(), ScriptTarget.Latest); 104 getAllClassDeclaration(sourceFile); 105 } 106 }); 107 108 dtsFileList.forEach(value => { 109 if (value.endsWith('.d.ts')) { 110 const code = fs.readFileSync(value); 111 const sourceFile = createSourceFile('', code.toString(), ScriptTarget.Latest); 112 const fileName = path.basename(value).split('.d.ts')[0]; 113 let outputFileName = ''; 114 if (fileName.includes('@')) { 115 outputFileName = fileName.split('@')[1].replace(/\./g, '_'); 116 } else { 117 outputFileName = fileName; 118 } 119 120 let tmpOutputMockJsFileDir = outMockJsFileDir; 121 if (!outputFileName.startsWith('system_')) { 122 tmpOutputMockJsFileDir = path.join(outMockJsFileDir, 'napi'); 123 } 124 let dirName = ''; 125 if (os.platform() === 'linux' || os.platform() === 'darwin') { 126 dirName = path.join(tmpOutputMockJsFileDir, path.dirname(value).split('/api')[1]); 127 } else { 128 dirName = path.join(tmpOutputMockJsFileDir, path.dirname(value).split('\\api')[1]); 129 } 130 if (!fs.existsSync(dirName)) { 131 mkdirsSync(dirName); 132 } 133 const sourceFileEntity = getSourceFileAssembly(sourceFile, fileName); 134 const filePath = path.join(dirName, outputFileName + '.js'); 135 fs.writeFileSync(filePath, ''); 136 fs.appendFileSync(path.join(filePath), generateSourceFileElements('', sourceFileEntity, sourceFile, outputFileName)); 137 } 138 }); 139 if (!fs.existsSync(path.join(outMockJsFileDir, 'napi'))) { 140 mkdirsSync(path.join(outMockJsFileDir, 'napi')); 141 } 142 fs.writeFileSync(path.join(outMockJsFileDir, 'napi', 'index.js'), generateIndex()); 143 fs.writeFileSync(path.join(outMockJsFileDir, 'index.js'), generateSystemIndex()); 144 fs.writeFileSync(path.join(outMockJsFileDir, 'entry.js'), generateEntry()); 145} 146 147main(); 148