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"; 22import { 23 ArkTSErrorDescription, 24 ErrorCode 25} from '../../../lib/fast_build/ark_compiler/error_code'; 26import { 27 LogData, 28 LogDataFactory 29} from '../../../lib/fast_build/ark_compiler/logger'; 30 31export function cpus() { 32 return os.cpus().length < 16 ? os.cpus().length : 16; 33} 34 35export function getProjectPath(name?: string) { 36 return name ? `${PROJECT_ROOT}/${name}` : `${PROJECT_ROOT}/${DEFAULT_PROJECT}`; 37} 38 39export function scanFiles(filepath: string, fileList: Set<string>) { 40 if (!fs.existsSync(filepath)) { 41 return; 42 } 43 const files = fs.readdirSync(filepath); 44 files.forEach((file) => { 45 const child = path.join(filepath, file); 46 const stat = fs.statSync(child); 47 if (stat.isDirectory()) { 48 scanFiles(child, fileList); 49 } else { 50 if (child.includes("mock")) { 51 return; 52 } 53 fileList.add(child); 54 } 55 }); 56} 57export function sleep(ms) { 58 return new Promise(resolve => setTimeout(resolve, ms)); 59} 60 61export function findImportSpecifier(node) { 62 if (ts.isImportSpecifier(node)) { 63 return node; 64 } 65 let result = null; 66 let found = false; 67 ts.forEachChild(node, child => { 68 if (!found) { 69 const potentialResult = findImportSpecifier(child); 70 if (potentialResult !== null) { 71 result = potentialResult; 72 found = true; 73 } 74 } 75 }); 76 return result; 77} 78 79export function createErrInfo(name: string): LogData { 80 return LogDataFactory.newInstance( 81 ErrorCode.ETS2BUNDLE_EXTERNAL_LAZY_IMPORT_RE_EXPORT_ERROR, 82 ArkTSErrorDescription, 83 `'${name}' of lazy-import is re-export`, 84 '', 85 ['Please make sure the namedBindings of lazy-import are not be re-exported.', 86 'Please check whether the autoLazyImport switch is opened.'] 87 ); 88} 89