prime-dev-cli 1.0.11-beta.1 → 1.0.11-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/mock-file-utils.mjs +103 -0
- package/package.json +3 -2
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import os from 'os';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 将项目.mock文件夹中所有.js文件重命名为.cjs
|
|
7
|
+
* @param {string} projectPath 项目路径
|
|
8
|
+
*/
|
|
9
|
+
export async function renameJsToCjs(projectPath) {
|
|
10
|
+
const mockPath = join(projectPath, '.mock');
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
// 检查.mock目录是否存在
|
|
14
|
+
await fs.access(mockPath);
|
|
15
|
+
} catch {
|
|
16
|
+
// .mock目录不存在,直接返回
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 递归遍历.mock目录下的所有文件
|
|
21
|
+
async function traverseDirectory(dirPath) {
|
|
22
|
+
const items = await fs.readdir(dirPath, { withFileTypes: true });
|
|
23
|
+
|
|
24
|
+
for (const item of items) {
|
|
25
|
+
const fullPath = join(dirPath, item.name);
|
|
26
|
+
|
|
27
|
+
if (item.isDirectory()) {
|
|
28
|
+
// 如果是目录,递归遍历
|
|
29
|
+
await traverseDirectory(fullPath);
|
|
30
|
+
} else if (item.isFile() && item.name.endsWith('.js')) {
|
|
31
|
+
// 如果是.js文件,重命名为.cjs
|
|
32
|
+
const newPath = fullPath.replace(/\.js$/, '.cjs');
|
|
33
|
+
try {
|
|
34
|
+
await fs.rename(fullPath, newPath);
|
|
35
|
+
console.log(`重命名文件: ${fullPath} -> ${newPath}`);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error(`重命名文件失败: ${fullPath}`, error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await traverseDirectory(mockPath);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 从配置文件中获取工作区路径和项目列表
|
|
48
|
+
* @returns {Promise<{workspaceRoot: string, projects: string[]}>}
|
|
49
|
+
*/
|
|
50
|
+
async function getWorkspaceConfig() {
|
|
51
|
+
const configPath = join(os.homedir(), '.prime-projects.json');
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const configData = await fs.readFile(configPath, 'utf8');
|
|
55
|
+
const config = JSON.parse(configData);
|
|
56
|
+
|
|
57
|
+
// 工作区路径固定为 ~/Documents/prime-workspace
|
|
58
|
+
const workspaceRoot = join(os.homedir(), 'Documents', 'prime-workspace');
|
|
59
|
+
|
|
60
|
+
// 项目列表从配置文件的键名中获取
|
|
61
|
+
const projects = Object.keys(config);
|
|
62
|
+
|
|
63
|
+
return { workspaceRoot, projects };
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error('读取配置文件失败:', error);
|
|
66
|
+
throw new Error('无法读取prime配置文件,请先运行 prime init');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 批量处理所有项目的.mock文件重命名
|
|
72
|
+
*/
|
|
73
|
+
export async function renameAllProjectsMockFiles() {
|
|
74
|
+
try {
|
|
75
|
+
console.log('正在读取项目配置...');
|
|
76
|
+
const { workspaceRoot, projects } = await getWorkspaceConfig();
|
|
77
|
+
|
|
78
|
+
if (!workspaceRoot) {
|
|
79
|
+
throw new Error('配置文件中未找到工作区路径');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
console.log(`工作区路径: ${workspaceRoot}`);
|
|
83
|
+
console.log(`开始处理 ${projects.length} 个项目的.mock文件重命名...`);
|
|
84
|
+
|
|
85
|
+
for (const projectName of projects) {
|
|
86
|
+
const projectPath = join(workspaceRoot, projectName);
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
// 检查项目目录是否存在
|
|
90
|
+
await fs.access(projectPath);
|
|
91
|
+
console.log(`处理项目: ${projectName}`);
|
|
92
|
+
await renameJsToCjs(projectPath);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.warn(`跳过项目 ${projectName}: 目录不存在或无法访问`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
console.log('所有项目的.mock文件重命名处理完成');
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error('批量重命名.mock文件失败:', error);
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prime-dev-cli",
|
|
3
|
-
"version": "1.0.11-beta.
|
|
3
|
+
"version": "1.0.11-beta.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"refresh:config": "node ./bin/cli.mjs init",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"files": [
|
|
17
17
|
"bin",
|
|
18
18
|
"dist",
|
|
19
|
-
"config"
|
|
19
|
+
"config",
|
|
20
|
+
"lib"
|
|
20
21
|
],
|
|
21
22
|
"keywords": [],
|
|
22
23
|
"author": "",
|