• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2025 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
16const fs = require('fs');
17const path = require('path');
18
19// 获取当前目录
20const currentDirectory = process.cwd();
21let workSpace = currentDirectory;
22for (let i = 0; i < 4; i++) {
23  workSpace = path.dirname(workSpace);
24}
25// JSON 文件路径
26const jsonFilePath = path.join(__dirname, 'arktsconfig_template.json');
27const outJsonFilePath = path.join(__dirname, 'dist/cache/arktsconfig.json');
28
29try {
30    // 读取 JSON 文件内容
31    const data = fs.readFileSync(jsonFilePath, 'utf8');
32    const jsonData = JSON.parse(data);
33
34    // 处理 baseUrl 字段
35    if (jsonData.compilerOptions.baseUrl) {
36        jsonData.compilerOptions.baseUrl = jsonData.compilerOptions.baseUrl.replace(/workspace/g, workSpace);
37    }
38
39    // 处理 Paths 字段
40    if (jsonData.compilerOptions.paths) {
41        for (const key in jsonData.compilerOptions.paths) {
42            const values = jsonData.compilerOptions.paths[key];
43            for (let i = 0; i < values.length; i++) {
44              if (key.startsWith('@ohos.arkui.')) {
45                values[i] = currentDirectory + "/dist/cache/" + key;
46              } else {
47                values[i] = values[i].replace(/workspace/g, workSpace);
48              }
49            }
50        }
51    }
52
53    // 将修改后的内容写回 JSON 文件
54    fs.writeFileSync(outJsonFilePath, JSON.stringify(jsonData, null, 2), 'utf8');
55} catch (error) {
56    console.error('处理 JSON 文件时出错:', error);
57}