1/*
2 * Copyright (c) 2024 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';
17
18import { DEBUG } from './CommonObject';
19
20export function isDebug(projectConfig: any): boolean {
21  return projectConfig.buildMode.toLowerCase() === DEBUG;
22}
23
24export function isFileExist(filePath: string): boolean {
25  let exist = true;
26  try {
27    fs.accessSync(filePath, fs.constants.F_OK);
28  } catch (err) {
29    exist = !err;
30  }
31  return exist;
32}
33
34export function sortAndDeduplicateStringArr(arr: string[]) : string[] {
35  if (arr.length === 0) {
36    return arr;
37  }
38
39  arr.sort((a, b) => {
40    return a.localeCompare(b);
41  });
42
43  let tmpArr: string[] = [arr[0]];
44  for (let i = 1; i < arr.length; i++) {
45    if (arr[i] !== arr[i - 1]) {
46      tmpArr.push(arr[i]);
47    }
48  }
49  return tmpArr;
50}
51
52export function mergeSet(set1: Set<string>, set2: Set<string>): Set<string> {
53  if (set1.size > 0 && set2.size > 0) {
54    return new Set([...set1, ...set2]);
55  }
56  if (set1.size > 0) {
57    return set1;
58  }
59  return set2;
60}
61
62export function convertSetToArray(reservedSet: Set<string> | undefined): string[] {
63  if (!reservedSet) {
64    return [];
65  }
66  return Array.from(reservedSet);
67}