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      if (child.includes("mock")) {
43        return;
44      }
45      fileList.add(child);
46    }
47  });
48}
49export function sleep(ms) {
50  return new Promise(resolve => setTimeout(resolve, ms));
51}
52
53export function findImportSpecifier(node) {
54  if (ts.isImportSpecifier(node)) {
55    return node;
56  }
57  let result = null;
58  let found = false;
59  ts.forEachChild(node, child => {
60    if (!found) {
61      const potentialResult = findImportSpecifier(child);
62      if (potentialResult !== null) {
63        result = potentialResult;
64        found = true;
65      }
66    }
67  });
68  return result;
69}
70