@shuiyangsuan/cli 0.0.3 → 0.0.4
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/README.md +197 -6
- package/dist/index.d.ts +963 -1
- package/dist/index.js +107 -63
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
package/dist/index.js
CHANGED
|
@@ -8747,74 +8747,118 @@ async function deleteDir(dirPath) {
|
|
|
8747
8747
|
|
|
8748
8748
|
//#endregion
|
|
8749
8749
|
//#region src/commands/delete.ts
|
|
8750
|
-
|
|
8751
|
-
|
|
8752
|
-
|
|
8753
|
-
|
|
8754
|
-
|
|
8755
|
-
|
|
8756
|
-
|
|
8757
|
-
|
|
8758
|
-
|
|
8759
|
-
|
|
8760
|
-
|
|
8761
|
-
|
|
8762
|
-
|
|
8763
|
-
|
|
8764
|
-
|
|
8765
|
-
|
|
8766
|
-
|
|
8767
|
-
|
|
8768
|
-
|
|
8769
|
-
|
|
8770
|
-
|
|
8771
|
-
|
|
8750
|
+
/**
|
|
8751
|
+
* 执行删除操作(编程模式)
|
|
8752
|
+
* @param patterns - 文件或文件夹路径/模式
|
|
8753
|
+
* @param config - CLI 配置
|
|
8754
|
+
* @param options - 删除选项
|
|
8755
|
+
* @returns 删除结果
|
|
8756
|
+
* @example
|
|
8757
|
+
* ```ts
|
|
8758
|
+
* import { executeDelete } from '@shuiyangsuan/cli';
|
|
8759
|
+
*
|
|
8760
|
+
* const result = await executeDelete(['node_modules', 'dist'], config, { force: true, verbose: true });
|
|
8761
|
+
* console.log(`删除了 ${result.deletedCount} 个文件`);
|
|
8762
|
+
* ```
|
|
8763
|
+
*/
|
|
8764
|
+
async function executeDelete(patterns, config, options = {}) {
|
|
8765
|
+
const result = {
|
|
8766
|
+
deletedCount: 0,
|
|
8767
|
+
failedCount: 0,
|
|
8768
|
+
deletedFiles: [],
|
|
8769
|
+
errors: []
|
|
8770
|
+
};
|
|
8771
|
+
try {
|
|
8772
|
+
const finalOptions = {
|
|
8773
|
+
...config,
|
|
8774
|
+
...options
|
|
8775
|
+
};
|
|
8776
|
+
let targetPatterns = patterns;
|
|
8777
|
+
if (targetPatterns.length === 0) if (finalOptions.useDefaults && config.defaultPatterns && config.defaultPatterns.length > 0) targetPatterns = [...config.defaultPatterns];
|
|
8778
|
+
else if (config.defaultPatterns && config.defaultPatterns.length > 0) targetPatterns = [...config.defaultPatterns];
|
|
8779
|
+
else throw new Error("请指定要删除的文件或文件夹,或在配置文件中设置 defaultPatterns");
|
|
8780
|
+
else if (finalOptions.useDefaults && config.defaultPatterns && config.defaultPatterns.length > 0) targetPatterns = [...config.defaultPatterns, ...patterns];
|
|
8781
|
+
const filesToDelete = await collectFilesToDelete(targetPatterns, finalOptions);
|
|
8782
|
+
if (filesToDelete.length === 0) {
|
|
8783
|
+
warn("没有找到匹配的文件或文件夹");
|
|
8784
|
+
return result;
|
|
8785
|
+
}
|
|
8786
|
+
if (finalOptions.dryRun || finalOptions.verbose) {
|
|
8787
|
+
info(`\n📋 将要删除 ${filesToDelete.length} 个项目:\n`);
|
|
8788
|
+
filesToDelete.forEach((file, index) => {
|
|
8789
|
+
log(` ${index + 1}. ${file}`);
|
|
8790
|
+
});
|
|
8791
|
+
log("");
|
|
8792
|
+
}
|
|
8793
|
+
if (finalOptions.dryRun) {
|
|
8794
|
+
info("🔍 预览模式 - 没有实际删除任何文件");
|
|
8795
|
+
return result;
|
|
8796
|
+
}
|
|
8797
|
+
if (!finalOptions.force) {
|
|
8798
|
+
const rl = (await import("readline")).createInterface({
|
|
8799
|
+
input: process.stdin,
|
|
8800
|
+
output: process.stdout
|
|
8801
|
+
});
|
|
8802
|
+
const answer = await new Promise((resolve) => {
|
|
8803
|
+
rl.question("⚠️ 确定要删除这些文件吗?(yes/no): ", (ans) => {
|
|
8804
|
+
rl.close();
|
|
8805
|
+
resolve(ans);
|
|
8772
8806
|
});
|
|
8773
|
-
|
|
8774
|
-
|
|
8775
|
-
|
|
8776
|
-
|
|
8777
|
-
return;
|
|
8807
|
+
});
|
|
8808
|
+
if (answer.toLowerCase() !== "yes" && answer.toLowerCase() !== "y") {
|
|
8809
|
+
info("❌ 已取消删除操作");
|
|
8810
|
+
return result;
|
|
8778
8811
|
}
|
|
8779
|
-
|
|
8780
|
-
|
|
8781
|
-
|
|
8782
|
-
|
|
8783
|
-
|
|
8784
|
-
|
|
8785
|
-
|
|
8786
|
-
|
|
8787
|
-
|
|
8788
|
-
|
|
8789
|
-
|
|
8790
|
-
|
|
8791
|
-
|
|
8792
|
-
|
|
8793
|
-
|
|
8812
|
+
}
|
|
8813
|
+
let deletedCount = 0;
|
|
8814
|
+
let failedCount = 0;
|
|
8815
|
+
const deleteTasks = filesToDelete.map(async (file) => {
|
|
8816
|
+
try {
|
|
8817
|
+
if (isDirectory(file)) await deleteDir(file);
|
|
8818
|
+
else await deleteFile(file);
|
|
8819
|
+
deletedCount++;
|
|
8820
|
+
result.deletedFiles.push(file);
|
|
8821
|
+
if (finalOptions.verbose) success(`✓ 已删除:${file}`);
|
|
8822
|
+
} catch (err) {
|
|
8823
|
+
failedCount++;
|
|
8824
|
+
const errorMsg = `✗ 删除失败:${file} - ${err.message}`;
|
|
8825
|
+
error(errorMsg);
|
|
8826
|
+
result.errors.push(errorMsg);
|
|
8794
8827
|
}
|
|
8795
|
-
|
|
8796
|
-
|
|
8797
|
-
|
|
8798
|
-
|
|
8799
|
-
|
|
8800
|
-
|
|
8801
|
-
|
|
8802
|
-
|
|
8803
|
-
|
|
8804
|
-
|
|
8805
|
-
|
|
8806
|
-
|
|
8807
|
-
|
|
8808
|
-
|
|
8809
|
-
|
|
8810
|
-
|
|
8811
|
-
|
|
8812
|
-
|
|
8813
|
-
|
|
8828
|
+
});
|
|
8829
|
+
await Promise.all(deleteTasks);
|
|
8830
|
+
log("");
|
|
8831
|
+
success(`✅ 删除完成:成功 ${deletedCount} 个,失败 ${failedCount} 个`);
|
|
8832
|
+
result.deletedCount = deletedCount;
|
|
8833
|
+
result.failedCount = failedCount;
|
|
8834
|
+
if (failedCount > 0) throw new Error(`部分文件删除失败:共失败 ${failedCount} 个`);
|
|
8835
|
+
return result;
|
|
8836
|
+
} catch (err) {
|
|
8837
|
+
error(`删除过程中出错:${err.message}`);
|
|
8838
|
+
result.errors.push(err.message);
|
|
8839
|
+
throw err;
|
|
8840
|
+
}
|
|
8841
|
+
}
|
|
8842
|
+
/**
|
|
8843
|
+
* 注册 delete 命令到 CLI
|
|
8844
|
+
* @param program - Commander 程序实例
|
|
8845
|
+
* @param config - CLI 配置
|
|
8846
|
+
*/
|
|
8847
|
+
function deleteCommand(program, config) {
|
|
8848
|
+
program.command("delete").alias("rm").alias("del").description("删除文件和文件夹,支持 glob 模式匹配").argument("[patterns...]", "要删除的文件或文件夹路径/模式", []).option("-f, --force", "强制删除,不提示确认", config.force).option("-n, --dry-run", "预览模式,仅显示将要删除的内容", config.dryRun).option("-v, --verbose", "详细输出模式", config.verbose).option("-d, --use-defaults", "使用配置文件中的默认模式").action(async (patterns, options) => {
|
|
8849
|
+
try {
|
|
8850
|
+
await executeDelete(patterns, config, options);
|
|
8851
|
+
} catch {
|
|
8814
8852
|
process.exit(1);
|
|
8815
8853
|
}
|
|
8816
8854
|
});
|
|
8817
8855
|
}
|
|
8856
|
+
/**
|
|
8857
|
+
* 收集要删除的文件列表
|
|
8858
|
+
* @param patterns - 文件或文件夹路径/模式
|
|
8859
|
+
* @param options - 删除选项
|
|
8860
|
+
* @returns 匹配的文件路径列表
|
|
8861
|
+
*/
|
|
8818
8862
|
async function collectFilesToDelete(patterns, _options) {
|
|
8819
8863
|
const filesToDelete = /* @__PURE__ */ new Set();
|
|
8820
8864
|
const matchTasks = patterns.map((pattern) => {
|
|
@@ -8882,8 +8926,8 @@ const program = new Command();
|
|
|
8882
8926
|
const config = await readConfig();
|
|
8883
8927
|
program.name("shuiyangsuan").description("个人 CLI 工具 - 支持文件和文件夹删除等操作").version("0.0.1");
|
|
8884
8928
|
deleteCommand(program, config);
|
|
8885
|
-
program.parse();
|
|
8929
|
+
if (process.argv[1] && process.argv[1].endsWith("/dist/index.js")) program.parse();
|
|
8886
8930
|
|
|
8887
8931
|
//#endregion
|
|
8888
|
-
export {
|
|
8932
|
+
export { collectFilesToDelete, deleteCommand, executeDelete, readConfig };
|
|
8889
8933
|
//# sourceMappingURL=index.js.map
|