js使用oclif開發(fā)命令行工具實(shí)現(xiàn)批量修改文件名
前言
前端開發(fā)工作中常用的很多 CLI 命令相信大家已經(jīng)很熟悉了,很方便很實(shí)用,能夠快速幫助你創(chuàng)建項(xiàng)目,快速執(zhí)行某些重復(fù)性操作。
oclif
我們 github.com/oclif/oclif 庫來作為 CLI 的基礎(chǔ)框架,這個官方文檔寫的很詳細(xì),我們這里簡單的擴(kuò)展說一下,并且實(shí)踐一個 demo。
設(shè)計(jì)一個批量修改文件名的小工具
我們設(shè)計(jì)一個批量更改文件名字的小工具,它可以幫助我們將某個目錄的下所有文件,按照自定義的規(guī)則來進(jìn)行批量重命名,這個工具
設(shè)計(jì)我們的命令用法
我們希望這樣使用命令即可批量重命名目錄下的所有文件。
./cl rename ./test -R -N test_{{index}}_{{time}}_{{st}}
其中 ./cl 是 ln -s /bin/dev
我們想設(shè)計(jì)的參數(shù)
{{name}}
代表原始文件名{{index}}
代表遞歸指數(shù):1,2,3,4{{time}}
代表時間:2022-8-23_11-39{{st}}
代表時間戳:1661226082464-R
代表是否開啟目錄深層遞歸-N XXXX
批量重命名后的名字格式
列如我們可以這樣用:
./cl rename ./test -R -N test_{{index}}_{{time}}_{{st}} 正在執(zhí)行批量重命名命令,參數(shù):{ path: './test' },{ deep: true, name: 'test_{{index}}_{{time}}_{{st}}' } 讀取目錄: ./test 讀取目錄: test/sub 讀取目錄: test/sub/commands 讀取目錄: test/sub/commands/hello 重命名文件: test/sub/commands/hello/test_4_2022-8-18_19-47_1661225596495.ts --> test_0_2022-8-18_19-47_1661225679878.ts 重命名文件: test/sub/commands/hello/test_5_2022-8-18_19-47_1661225596495.js --> test_1_2022-8-18_19-47_1661225679879.js 重命名文件: test/sub/commands/hello/test_6_2022-8-18_19-47_1661225596496.ts --> test_2_2022-8-18_19-47_1661225679879.ts 重命名文件: test/sub/commands/hello/test_7_2022-8-18_19-47_1661225596496.js --> test_3_2022-8-18_19-47_1661225679879.js 重命名文件: test/sub/commands/hello/test_8_2022-8-18_19-47_1661225596496.ts --> test_4_2022-8-18_19-47_1661225679879.ts 重命名文件: test/sub/commands/hello/test_9_2022-8-18_19-47_1661225596496.js --> test_5_2022-8-18_19-47_1661225679879.js 讀取目錄: test/sub/commands/setup 重命名文件: test/sub/commands/setup/index.d.ts --> test_6_2022-8-18_19-47_1661225679879.ts 重命名文件: test/sub/commands/setup/index.js --> test_7_2022-8-18_19-47_1661225679879.js 重命名文件: test/sub/index.d.ts --> test_8_2022-8-18_19-47_1661225679879.ts 重命名文件: test/sub/index.js --> test_9_2022-8-18_19-47_1661225679880.js 重命名文件: test/sub/test_3_2022-8-23_10-52_1661225596495 --> test_10_2022-8-23_10-52_1661225679880 重命名文件: test/test_0_2022-8-23_10-52_1661225596494 --> test_11_2022-8-23_10-52_1661225679880 重命名文件: test/test_1_2022-8-16_18-1_1661225596495.png --> test_12_2022-8-16_18-1_1661225679880.png 重命名文件: test/test_2_2022-7-18_14-32_1661225596495.json --> test_13_2022-7-18_14-32_1661225679880.json 重命名文件: test/圖片2.jpg --> test_15_2022-6-24_15-12_1661225679880.jpg 重命名文件: test/矩陣點(diǎn)背景圖 2.png --> test_17_2022-8-12_17-23_1661225679881.png 重命名文件: test/矩陣點(diǎn)背景圖.png --> test_18_2022-8-12_17-23_1661225679881.png
自動的遞歸所有文件,并且全部以某個格式進(jìn)行命名
源碼實(shí)現(xiàn)
目錄結(jié)構(gòu)
首先跟隨官方文檔所說的那樣,安裝并生成 Hello world 程序,再進(jìn)行我們自定義命令的編寫。接下來我們新建一個命令。
- src/commands/rename/index.ts
- 在 rename 目錄下新建 index.ts 之后,框架會為我們自動注冊 rename 命令。
代碼中我寫了注釋,通過配合代碼閱讀相信會更有用。
import { Command, Flags, CliUx } from "@oclif/core"; import * as fs from "fs-extra"; import * as path from "path"; // 執(zhí)行命令: // ./cl rename ./test -R -N test_{{index}}_{{time}}_{{st}} export class Rename extends Command { static description = "description of this example command"; // 注冊標(biāo)識參數(shù):--name 和 --deep,并且設(shè)置簡稱 static flags = { name: Flags.string({ char: "N" }), deep: Flags.boolean({ char: "R" }), }; // 注冊參數(shù),即 rename 空格后的第一段文字,這里是 ./test static args = [{ name: "path" }]; private index = 0; // 命令解析完畢后,執(zhí)行此方法 async run() { const { args } = await this.parse(Rename); const { flags } = await this.parse(Rename); this.log("正在執(zhí)行批量重命名命令,參數(shù):%s,%s", args, flags); // 遍歷目錄并給每一個文件執(zhí)行規(guī)則程序 this.wake(args.path, flags.deep, (path: string) => { this.rule(path, flags.name); }); } // 遞歸遍歷目錄 wake(root: string, recursive = false, callback = (path: string) => {}): void { console.log("讀取目錄:", root); for (const filename of fs.readdirSync(root)) { const absPath = path.normalize(path.join(root, filename)); const file = fs.statSync(absPath); if (file.isDirectory() && recursive) { this.wake(absPath, recursive, callback); continue; } try { callback(absPath); } catch (error) { console.log(error); } } } // 文件名規(guī)則注冊 rule(filePath: string, nameRules?: string) { const fileName = path.basename(filePath); // 規(guī)則程序執(zhí)行,替換關(guān)鍵字 // 目前只做這么幾個規(guī)則,以后看情況加,擴(kuò)展性也不錯,可以單獨(dú)分到 services 層做一層分離,可讀性更高 // 這里注冊了 {{index}} {{time}} 等關(guān)鍵字 let newFileName = this.keyword( String(nameRules), "index", () => this.index++ ); newFileName = this.keyword(newFileName, "name", () => fileName); newFileName = this.keyword(newFileName, "st", () => new Date().getTime()); newFileName = this.keyword(newFileName, "time", () => { const date = new Date(fs.statSync(filePath).mtime); return `${date.getFullYear()}-${ date.getMonth() + 1 }-${date.getDate()}_${date.getHours()}-${date.getMinutes()}`; }); newFileName += path.extname(filePath); const absNewPath = path.normalize( path.join(path.dirname(filePath), newFileName) ); this.log(`重命名文件: ${filePath} --> ${newFileName}`); fs.renameSync(filePath, absNewPath); } // 文件名關(guān)鍵字替換 keyword( fileName: string, rule: string, variableFn: () => string | number ): string { const key = "{{" + rule + "}}"; const variable = variableFn(); while (fileName.includes(key)) { fileName = fileName.replace(key, String(variable)); } return fileName; } }
執(zhí)行命令之后的結(jié)果都在上面的【命令用法】章節(jié)了,可以返回到上面看看輸出。
總結(jié)
總的來說腳手架這個東西在以后都是非常重要的,因?yàn)楹芏嗍虑槎疾豢赡苁菑牧汩_始,寫個腳手架整合一些自己常用的東西,可以很方便的寫一些腳本輔助日常工作。
當(dāng)然,我這篇文章演示的只是冰山一角,還有更多的花樣,比如它還能輸出表格,選擇框,特效,動畫,進(jìn)度條之類的,有很大的探索價(jià)值。
到此這篇關(guān)于js使用oclif開發(fā)命令行工具實(shí)現(xiàn)批量修改文件名的文章就介紹到這了,更多相關(guān)js批量修改文件名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript原生開發(fā)視頻播放器的實(shí)現(xiàn)代碼
這篇文章我們將一起探索一份自定義的視頻播放器實(shí)現(xiàn)代碼,甚至還可以實(shí)現(xiàn)有彈幕功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-06-06如何將一維度數(shù)組轉(zhuǎn)換成三維數(shù)組結(jié)構(gòu)
在開發(fā)過程中,可能會遇到需要將一維數(shù)組轉(zhuǎn)換為多維數(shù)組的情況,以滿足特定數(shù)據(jù)結(jié)構(gòu)的需求,文章介紹了如何將后端返回的一維列表數(shù)據(jù)通過編程方法轉(zhuǎn)換成三維數(shù)組結(jié)構(gòu),以適應(yīng)特定的UI展示需求,通過循環(huán)遍歷和數(shù)據(jù)重組的方式,可以有效地實(shí)現(xiàn)數(shù)組結(jié)構(gòu)的轉(zhuǎn)換2024-09-09如何用JS實(shí)現(xiàn)簡單的數(shù)據(jù)監(jiān)聽
這篇文章主要介紹了如何用JS實(shí)現(xiàn)簡單的數(shù)據(jù)監(jiān)聽,對數(shù)據(jù)監(jiān)聽感興趣的同學(xué),可以參考一下2021-05-05微信小程序?qū)崿F(xiàn)分享商品海報(bào)功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)分享商品海報(bào)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09