1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use rollupObject 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 os from 'os'; 17import fs from "fs"; 18import path from "path"; 19import * as ts from 'typescript'; 20import { PROJECT_ROOT } from "../mock/rollup_mock/path_config"; 21import { DEFAULT_PROJECT } from "../mock/rollup_mock/path_config"; 22 23export function cpus() { 24 return os.cpus().length < 16 ? os.cpus().length : 16; 25} 26 27export function getProjectPath(name?: string) { 28 return name ? `${PROJECT_ROOT}/${name}` : `${PROJECT_ROOT}/${DEFAULT_PROJECT}`; 29} 30 31export function scanFiles(filepath: string, fileList: Set<string>) { 32 if (!fs.existsSync(filepath)) { 33 return; 34 } 35 const files = fs.readdirSync(filepath); 36 files.forEach((file) => { 37 const child = path.join(filepath, file); 38 const stat = fs.statSync(child); 39 if (stat.isDirectory()) { 40 scanFiles(child, fileList); 41 } else { 42 fileList.add(child); 43 } 44 }); 45} 46export function sleep(ms) { 47 return new Promise(resolve => setTimeout(resolve, ms)); 48} 49 50export function findImportSpecifier(node) { 51 if (ts.isImportSpecifier(node)) { 52 return node; 53 } 54 let result = null; 55 let found = false; 56 ts.forEachChild(node, child => { 57 if (!found) { 58 const potentialResult = findImportSpecifier(child); 59 if (potentialResult !== null) { 60 result = potentialResult; 61 found = true; 62 } 63 } 64 }); 65 return result; 66} 67