Skip to content

Commit 9b289f4

Browse files
committed
💡 perf(项目): 优化写法
1 parent 774b1ed commit 9b289f4

24 files changed

Lines changed: 197 additions & 175 deletions

bin/cc.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/usr/bin/env node
22

3-
const path = require('path');
4-
const { program } = require('commander');
3+
import { program } from 'commander';
4+
import { readFileSync } from 'fs';
5+
import { fileURLToPath } from 'url';
6+
import { dirname, join } from 'path';
57

6-
// 添加项目根目录到模块搜索路径
7-
const projectRoot = path.join(__dirname, '..');
8-
require.main.paths.unshift(path.join(projectRoot, 'src'));
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = dirname(__filename);
910

10-
const packageJson = require('../package.json');
11+
// 读取 package.json
12+
const packageJson = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf8'));
1113

1214
// 设置程序信息
1315
program
@@ -16,7 +18,7 @@ program
1618
.version(packageJson.version);
1719

1820
// 导入主程序入口
19-
const main = require('../src/index');
21+
const { default: main } = await import('../src/index.js');
2022

2123
// 启动主程序
2224
main(program)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.4.2",
44
"description": "Claude Code配置管理CLI工具",
55
"main": "src/index.js",
6+
"type": "module",
67
"bin": {
78
"cc": "./bin/cc.js",
89
"cc-cli": "./bin/cc.js"

src/commands/backup/backup.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const fs = require("fs-extra");
2-
const path = require("path");
3-
const os = require("os");
4-
const chalk = require("chalk");
5-
const inquirer = require("inquirer");
6-
const ora = require("ora");
7-
const FileManager = require("./file-manager");
8-
const WebDAVClient = require("./webdav-client");
1+
import fs from "fs-extra";
2+
import path from "path";
3+
import os from "os";
4+
import chalk from "chalk";
5+
import inquirer from "inquirer";
6+
import ora from "ora";
7+
import FileManager from "./file-manager.js";
8+
import WebDAVClient from "./webdav-client.js";
99

1010
/**
1111
* 备份功能实现
@@ -609,7 +609,7 @@ class BackupManager {
609609
* @returns {boolean} 是否继续备份流程
610610
*/
611611
async performMigration(oldConfigPath) {
612-
const ora = require("ora");
612+
const ora = (await import("ora")).default;
613613
const newConfigDir = path.join(os.homedir(), ".cc-cli");
614614
const newConfigPath = path.join(newConfigDir, "api_configs.json");
615615

@@ -793,4 +793,4 @@ class BackupManager {
793793
}
794794
}
795795

796-
module.exports = BackupManager;
796+
export default BackupManager;

src/commands/backup/file-manager.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
const path = require('path');
2-
const os = require('os');
3-
const fs = require('fs-extra');
4-
const chalk = require('chalk');
1+
import path from 'path';
2+
import os from 'os';
3+
import fs from 'fs-extra';
4+
import chalk from 'chalk';
5+
import { fileURLToPath } from 'url';
6+
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = path.dirname(__filename);
59

610
/**
711
* 文件路径管理器
@@ -245,4 +249,4 @@ class FileManager {
245249
}
246250
}
247251

248-
module.exports = FileManager;
252+
export default FileManager;

src/commands/backup/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const chalk = require('chalk');
2-
const inquirer = require('inquirer');
3-
const { createBackChoice } = require('../../utils/ui');
1+
import chalk from 'chalk';
2+
import inquirer from 'inquirer';
3+
import { createBackChoice } from '../../utils/ui.js';
44

55
/**
66
* 备份命令主控制器
@@ -103,7 +103,7 @@ class BackupCommand {
103103
*/
104104
async handleBackup() {
105105
try {
106-
const BackupManager = require('./backup');
106+
const { default: BackupManager } = await import('./backup.js');
107107
const backupManager = new BackupManager();
108108

109109
await backupManager.performBackup();
@@ -119,7 +119,7 @@ class BackupCommand {
119119
*/
120120
async handleRestore() {
121121
try {
122-
const RestoreManager = require('./restore');
122+
const { default: RestoreManager } = await import('./restore.js');
123123
const restoreManager = new RestoreManager();
124124

125125
await restoreManager.performRestore();
@@ -147,9 +147,9 @@ class BackupCommand {
147147
* 显示备份状态
148148
*/
149149
async showBackupStatus() {
150-
const ora = require('ora');
151-
const FileManager = require('./file-manager');
152-
const WebDAVClient = require('./webdav-client');
150+
const ora = (await import('ora')).default;
151+
const { default: FileManager } = await import('./file-manager.js');
152+
const { default: WebDAVClient } = await import('./webdav-client.js');
153153

154154
console.log(chalk.cyan.bold('\n📊 备份状态报告\n'));
155155

@@ -232,4 +232,4 @@ class BackupCommand {
232232
}
233233
}
234234

235-
module.exports = new BackupCommand();
235+
export default new BackupCommand();

src/commands/backup/restore.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const fs = require("fs-extra");
2-
const path = require("path");
3-
const chalk = require("chalk");
4-
const inquirer = require("inquirer");
5-
const ora = require("ora");
6-
const FileManager = require("./file-manager");
7-
const WebDAVClient = require("./webdav-client");
1+
import fs from "fs-extra";
2+
import path from "path";
3+
import chalk from "chalk";
4+
import inquirer from "inquirer";
5+
import ora from "ora";
6+
import FileManager from "./file-manager.js";
7+
import WebDAVClient from "./webdav-client.js";
88

99
/**
1010
* 恢复功能实现
@@ -459,4 +459,4 @@ class RestoreManager {
459459
}
460460
}
461461

462-
module.exports = RestoreManager;
462+
export default RestoreManager;

src/commands/backup/webdav-client.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { createClient } = require('webdav');
2-
const fs = require('fs-extra');
3-
const path = require('path');
4-
const os = require('os');
5-
const chalk = require('chalk');
6-
const inquirer = require('inquirer');
1+
import { createClient } from 'webdav';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
import os from 'os';
5+
import chalk from 'chalk';
6+
import inquirer from 'inquirer';
77

88
/**
99
* WebDAV客户端管理器
@@ -405,4 +405,4 @@ class WebDAVClient {
405405
}
406406
}
407407

408-
module.exports = WebDAVClient;
408+
export default WebDAVClient;

src/commands/claude/add.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const chalk = require('chalk');
2-
const inquirer = require('inquirer');
3-
const ora = require('ora');
4-
const fs = require('fs-extra');
1+
import chalk from 'chalk';
2+
import inquirer from 'inquirer';
3+
import ora from 'ora';
4+
import fs from 'fs-extra';
55

6-
const ConfigManager = require('../../core/ConfigManager');
7-
const { showError, showSuccess, showInfo, showWarning } = require('../../utils/ui');
8-
const { formatError } = require('../../utils/formatter');
6+
import ConfigManager from '../../core/ConfigManager.js';
7+
import { showError, showSuccess, showInfo, showWarning } from '../../utils/ui.js';
8+
import { formatError } from '../../utils/formatter.js';
99

1010
/**
1111
* API配置添加命令
@@ -338,4 +338,4 @@ class AddCommand {
338338
}
339339
}
340340

341-
module.exports = new AddCommand();
341+
export default new AddCommand();

src/commands/claude/apiuse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const chalk = require('chalk');
2-
const ora = require('ora');
1+
import chalk from 'chalk';
2+
import ora from 'ora';
33

4-
const ConfigManager = require('../../core/ConfigManager');
5-
const { selectSite, selectToken, confirmSwitch, showSuccess, showError, showInfo } = require('../../utils/ui');
6-
const { formatSwitchSuccess } = require('../../utils/formatter');
4+
import ConfigManager from '../../core/ConfigManager.js';
5+
import { selectSite, selectToken, confirmSwitch, showSuccess, showError, showInfo } from '../../utils/ui.js';
6+
import { formatSwitchSuccess } from '../../utils/formatter.js';
77

88
/**
99
* API快速使用命令
@@ -135,4 +135,4 @@ class ApiUseCommand {
135135
}
136136
}
137137

138-
module.exports = new ApiUseCommand();
138+
export default new ApiUseCommand();

src/commands/claude/delete.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const chalk = require("chalk");
2-
const inquirer = require("inquirer");
3-
const ora = require("ora");
4-
const fs = require("fs-extra");
1+
import chalk from "chalk";
2+
import inquirer from "inquirer";
3+
import ora from "ora";
4+
import fs from "fs-extra";
55

6-
const ConfigManager = require("../../core/ConfigManager");
7-
const {
6+
import ConfigManager from "../../core/ConfigManager.js";
7+
import {
88
showError,
99
showSuccess,
1010
showInfo,
1111
showWarning,
1212
createBackChoice,
13-
} = require("../../utils/ui");
14-
const { formatError, getSiteIcon } = require("../../utils/formatter");
13+
} from "../../utils/ui.js";
14+
import { formatError, getSiteIcon } from "../../utils/formatter.js";
1515

1616
/**
1717
* API配置删除命令
@@ -355,4 +355,4 @@ class DeleteCommand {
355355
}
356356
}
357357

358-
module.exports = new DeleteCommand();
358+
export default new DeleteCommand();

0 commit comments

Comments
 (0)