@zzclub/z-cli 0.4.5

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/CHANGELOG.md ADDED
@@ -0,0 +1,60 @@
1
+ # Changelog
2
+
3
+
4
+ ## v0.4.5
5
+
6
+ [compare changes](https://github.com/aatrooox/z-cli/compare/v0.4.4...v0.4.5)
7
+
8
+ ### 🩹 Fixes
9
+
10
+ - Env ignore ([535fc54](https://github.com/aatrooox/z-cli/commit/535fc54))
11
+
12
+ ### ❤️ Contributors
13
+
14
+ - Aatrox <gnakzz@qq.com>
15
+
16
+ ## v0.4.4
17
+
18
+ [compare changes](https://github.com/aatrooox/z-cli/compare/v0.4.3...v0.4.4)
19
+
20
+ ### 🚀 Enhancements
21
+
22
+ - 增加版本更新检测机制 ([d8876d3](https://github.com/aatrooox/z-cli/commit/d8876d3))
23
+
24
+ ### ❤️ Contributors
25
+
26
+ - Aatrox <gnakzz@qq.com>
27
+
28
+ ## v0.4.3
29
+
30
+ [compare changes](https://github.com/aatrooox/z-cli/compare/v0.4.2...v0.4.3)
31
+
32
+ ## v0.4.2
33
+
34
+ [compare changes](https://github.com/aatrooox/z-cli/compare/v0.4.1...v0.4.2)
35
+
36
+ ### 🚀 Enhancements
37
+
38
+ - 配置自动发布npm ([75890f6](https://github.com/aatrooox/z-cli/commit/75890f6))
39
+
40
+ ### 📖 Documentation
41
+
42
+ - Readme ([f7e5f7c](https://github.com/aatrooox/z-cli/commit/f7e5f7c))
43
+ - LICENSE & readme ([d86fc37](https://github.com/aatrooox/z-cli/commit/d86fc37))
44
+
45
+ ### ❤️ Contributors
46
+
47
+ - Aatrox <gnakzz@qq.com>
48
+
49
+ ## v0.4.1
50
+
51
+ [compare changes](https://github.com/aatrooox/zzoffduty-cli/compare/v0.4.0...v0.4.1)
52
+
53
+ ### 🚀 Enhancements
54
+
55
+ - 修改全局配置文件保存地址;修改项目名称等 ([2d93fae](https://github.com/aatrooox/zzoffduty-cli/commit/2d93fae))
56
+
57
+ ### ❤️ Contributors
58
+
59
+ - Aatrox <gnakzz@qq.com>
60
+
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 z-cli
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,195 @@
1
+ # z-cli
2
+
3
+ **这是一个效率类命令行工具, 为了解决工作中或个人开发过程中的重复或繁琐问题**
4
+
5
+ node >= 18.18.0
6
+
7
+ 推荐版本(我的):`20.18.1`
8
+
9
+ ## 功能一览
10
+
11
+ - `translate`: 国际化文件翻译 (英译中)
12
+ - `tiny`: 压缩文件。基于sharpjs, sharp支持的文件都可以压缩
13
+ - `picgo`: 通过 Picgo 上传到图床
14
+ 1. 支持指定文件 -f 上传
15
+ 2. 支持指定文件夹 -d 批量上传全部
16
+ 3. 支持限制大小 -m 默认只上传60kb以内的图片
17
+ 4. 支持模糊匹配 -co 文件名中含有co的图片, 且满足大小限制, 都会被上传
18
+
19
+ **特别注意:**
20
+ **tiny命令不再支持和picgo联动, 仅支持压缩后,自动替换一下md文件里的图片链接, 之后的操作可以用obsidian里的插件[image auto upload Plugin]完成~~**
21
+
22
+
23
+ ## 安装
24
+
25
+ `1.0.0` 之前的版本都为不稳定版本
26
+
27
+ ```shell
28
+ npm i -g @zzclub/z-cli
29
+ ```
30
+
31
+ ## 翻译功能配置说明
32
+ ### 初始化翻译平台appId和key
33
+
34
+ ```shell
35
+ zz set translate account.appId xxx
36
+ zz set translate account.key xxx
37
+ ```
38
+
39
+ ### 在哪里可以创建appId和key
40
+
41
+ > 请使用前仔细阅读百度翻译开发平台相关规则
42
+
43
+ [百度翻译开放平台](https://fanyi-api.baidu.com/api/trans/product/desktop)
44
+
45
+ 1. 注册
46
+ 2. 实名认证
47
+ 1. 标准版 qbs 1 每月5万字符
48
+ 2. 高级版 qbs 10 每月100万字符
49
+ 3. 开通通用文本翻译功能
50
+ 4. 生成appId和key
51
+ 5. 生成后的文件请仔细检查,有可能会有遗漏的翻译,如有,重新执行即可
52
+ 6. 注意: 百度翻译的api有一定的调用限制, 请自行评估是否需要使用高级版
53
+
54
+ ### 翻译单个文件
55
+
56
+ ```shell
57
+ zz translate -f ./yourfile.js
58
+ # 会在同级目录下生成 yourfile-en.js
59
+ ```
60
+ 如test.js
61
+ ```js
62
+ export default {
63
+ isok: '早早下班',
64
+ common: {
65
+ listTitle: '标题',
66
+ addTitle: '测试'
67
+ },
68
+ test: {
69
+ a: {
70
+ b: {
71
+ c: '哈哈哈'
72
+ }
73
+ },
74
+ aaa: {
75
+ value: '输入'
76
+ }
77
+ }
78
+ }
79
+ ```
80
+ 输出文件为test-en.js, 内容如下
81
+ ```js
82
+ export default {
83
+ isok: "Leave work early",
84
+ common: {
85
+ listTitle: "title",
86
+ addTitle: "test"
87
+ },
88
+ test: {
89
+ a: {
90
+ b: {
91
+ c: "Hahaha"
92
+ }
93
+ },
94
+ aaa: {
95
+ value: "input"
96
+ }
97
+ }
98
+ }
99
+ ```
100
+ ### 批量翻译
101
+
102
+ > 检索目标文件夹内所有langs文件夹下的zh-CN 文件夹下的所有文件, 输出至其同级的en-US下, 文件名同名
103
+
104
+ ```shell
105
+ zz translate -d ./demo
106
+ ```
107
+ 如: demo文件夹是以下结构, zh-CN中所有JS会翻译后输出至en-US
108
+
109
+ 每个文件输出内容同翻译单个文件
110
+
111
+ ```bash
112
+ .
113
+ ├── en-US
114
+ │   ├── test.js
115
+ │   ├── test2.js
116
+ │   └── test3.js
117
+ ├── test-en.js
118
+ ├── test.js
119
+ └── zh-CN
120
+ ├── test.js
121
+ ├── test2.js
122
+ └── test3.js
123
+
124
+
125
+ ```
126
+ ## 压缩图片
127
+
128
+ 使用help命令查看所有支持的功能
129
+ ```
130
+ zz tiny --help
131
+
132
+ -t, --type <fileType> 转换后的图片类型 (default: null)
133
+ -f, --file <file> 要压缩的图片文件 (default: null)
134
+ -d, --dir <dir> 压缩文件夹内所有文件 (default: null)
135
+ -co, --condition <condition> 压缩文件夹内所有名称包含[--condition]的图片文件 (default: null)
136
+ -q, --quality <quality> 压缩质量(1-100) (default: 75)
137
+ -c, --colours <colours> GIF色彩保留(2-256) (default: 128)
138
+ -n, --name <name> 指定文件名输出 (default: "")
139
+ -m, --max <max> 限制要上传的文件大小(kb)(仅当开启 --picgo 时会用到) (default: 60)
140
+ --picgo [type] 调用picgo (无参数) (default: null)
141
+ --no-picgo [type] 不调用picgo (无参数) (default: null)
142
+ -h, --help display help for command
143
+ ```
144
+
145
+ ## 通过PicGo上传到图床
146
+
147
+ **此功能通过Http请求的方式调用Picgo Server, 所以需要本地已经安装并启动Picgo, 并已经配置好了图床**
148
+
149
+ 使用help命令查看所有支持的功能
150
+
151
+ ```
152
+ Options:
153
+ -f, --file <file> 要上传的图片文件 (default: null)
154
+ -d, --dir <dir> 上传文件夹内所有图片文件 (default: null)
155
+ -co, --condition <condition> 上传文件夹内所有名称包含[--condition]的图片文件 (default: null)
156
+ -m, --max <max> 大于指定大小(kb)的图片不会被上传 (default: 60)
157
+ -h, --help display help for command
158
+ ```
159
+
160
+ ## Obsidian相关(Alpha)
161
+ 1. 如果你使用Obsidian编写markdown,本工具还提供了一个压缩=>上传=>自动替换文件路径为外链的联动功能, 请仔细阅读以下!!!
162
+ 2. 我的设置是: **设置=>文件与连接=>附件默认存放路径=> 当前文件所在文件夹下指定的子文件夹中 =>子文件夹名称: 配图**
163
+ 1. 这样做的原因是, 方便你用tiny命令压缩**所有和这篇文章相关的图片**, 当然你自己指定压缩的目录下文件不多的话, 也可以
164
+ 2. Obsidian里, 插入本地图片的语法为 `![[demo.png]]` , 此联动功能目前**只支持替换图片名称**, 替换后的字符串为 `![[demo-tiny.png]]`
165
+ 3. 注意参数要打全, 缺一不可
166
+ 1. 使用--replace 参数, 开启替换功能
167
+ 2. 使用--replace-file=./xxx.md 参数, 指定需要替换的markdown文件
168
+ 3. 正确演示: **zz tiny -d ./demo/md/配图 --replace --replace-file=./demo/md/demo5.md**
169
+ 4. Obsidian文件替换后, 应该是不能用撤回键回退了, **所以先拿一个没用的文件, 玩明白了再开始搞自己的重要文件, 如有问题, 自己承担!**
170
+ 5. 第四条不会造成图片文件丢失,只是贴图太多的话,一般图片名称没有规则, 再还原就比较恶心了
171
+ 6. 压缩完成,替换完成后, 请使用image auto upload 插件来进行上传操作. 后续看能否给作者提PR加入压缩功能
172
+ 7. **演示文件在demo/md里**
173
+
174
+
175
+ ## 提出新需求、定制
176
+
177
+ 欢迎提出新需求,只要合理、常见,我会尽快集成。
178
+
179
+ 或有定制需求
180
+
181
+ 请联系V详谈: 523748995
182
+
183
+ ## 免责声明
184
+
185
+ 任何用户在使用 z-cli 前,请您仔细阅读并透彻理解本声明。您可以选择不使用 z-cli ,若您一旦使用 z-cli ,您的使用行为即被视为对本声明全部内容的认可和接受。
186
+
187
+ 1. 任何单位或个人因下载使用 z-cli 而产生的任何意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带或衍生的损失等),本人不承担任何法律责任。
188
+
189
+ 2. 任何单位或个人不得在未经本团队书面授权的情况下对 z-cli 工具本身申请相关的知识产权。
190
+
191
+ 3. 如果本声明的任何部分被认为无效或不可执行,则该部分将被解释为反映本人的初衷,其余部分仍具有完全效力。不可执行的部分声明,并不构成我放弃执行该声明的权利。
192
+
193
+ ## LICENSE
194
+
195
+ [MIT](/LICENSE)
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@zzclub/z-cli",
3
+ "version": "0.4.5",
4
+ "description": "all-in-one 工具箱,专为提升日常及工作效率而生",
5
+ "main": "src/index.js",
6
+ "type": "module",
7
+ "author": "aatrox",
8
+ "bin": {
9
+ "z": "src/index.js",
10
+ "zz": "src/index.js"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/aatrooox/z-cli.git"
15
+ },
16
+ "keywords": [
17
+ "cli",
18
+ "zz",
19
+ "zzclub",
20
+ "node",
21
+ "sharp",
22
+ "translate",
23
+ "picgo",
24
+ "tool"
25
+ ],
26
+ "scripts": {
27
+ "release": "pnpm release:patch",
28
+ "release:patch": "npx changelogen@latest --patch --release --push --publish",
29
+ "release:minor": "npx changelogen@latest --minor --release --push --publish",
30
+ "release:major": "npx changelogen@latest --major --release --push --publish",
31
+ "release:main": "npx changelogen@latest -r v1.0.0 --release --push --publish"
32
+ },
33
+ "engines": {
34
+ "node": ">=18.18.0"
35
+ },
36
+ "license": "MIT",
37
+ "dependencies": {
38
+ "axios": "1.6.2",
39
+ "chalk": "5.3.0",
40
+ "commander": "11.0.0",
41
+ "inquirer": "9.2.8",
42
+ "latest-version": "^9.0.0",
43
+ "ora": "7.0.0",
44
+ "sharp": "0.33.0",
45
+ "shelljs": "0.8.5"
46
+ }
47
+ }
@@ -0,0 +1,97 @@
1
+ import { writeFileContent, getLocalConfig, readJsonFile, setLocalConfig } from "../utils/common.js";
2
+ import chalk from "chalk";
3
+ import ora from "ora";
4
+ import path from "node:path";
5
+ import fs, { lchown } from "node:fs";
6
+ import { dirname } from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = dirname(__filename);
11
+ function checkFilepath(filePath, configName, spinner) {
12
+ if (!filePath) {
13
+ let caseText;
14
+ if (configName === "export") {
15
+ caseText = `导出到指定目录 => zz config export /home/data`;
16
+ }
17
+ if (configName === "import") {
18
+ caseText = `导入指定配置文件 => zz config import /home/data/我的配置.json`;
19
+ }
20
+ spinner.fail("请指定文件路径后重试, 如:" + caseText);
21
+ return false;
22
+ }
23
+
24
+ return true;
25
+ }
26
+ export const configCmd = {
27
+ name: "config <configName> [payload...]",
28
+ description:
29
+ "全局config.json的导入[import]/导出[export]/还原[reset] \n如: zz config import ./abc.json",
30
+ options: [],
31
+ action: async (configName, payload, cmd) => {
32
+ let config_spinner = ora();
33
+ let config = await getLocalConfig();
34
+ let filePath = payload[0];
35
+ switch (configName) {
36
+ // 导出config
37
+ case "export":
38
+ //导出
39
+ if (checkFilepath(filePath, configName, config_spinner)) {
40
+ try {
41
+ let stats = fs.statSync(filePath);
42
+ let isDir = stats.isDirectory();
43
+ if (isDir) {
44
+ // 有效的文件夹路径, 无文件名, 生成默认的json名
45
+ filePath = path.resolve(
46
+ __dirname,
47
+ filePath,
48
+ "zzclub-z-cli.json"
49
+ );
50
+ fs.writeFileSync(
51
+ filePath,
52
+ JSON.stringify(config, null, 2),
53
+ "utf-8"
54
+ );
55
+ config_spinner.succeed(`导出配置成功! [${filePath}]`);
56
+ }
57
+ } catch (error) {
58
+ config_spinner.fail(`不存在的文件夹路径: [${filePath}]`);
59
+ }
60
+ }
61
+ process.exit(1);
62
+ case "import":
63
+ //导入
64
+ if (checkFilepath(filePath, configName, config_spinner)) {
65
+ try {
66
+ let stats = fs.statSync(filePath);
67
+ let isFile = stats.isFile();
68
+ if (isFile) {
69
+ let extname = path.extname(filePath);
70
+ if (extname !== ".json") {
71
+ config_spinner.fail(`只支持导入json文件: [${filePath}]`);
72
+ process.exit(1);
73
+ }
74
+ let localConfig = await readJsonFile(filePath);
75
+ let result = Object.assign({}, config, localConfig);
76
+
77
+ setLocalConfig(result, config_spinner)
78
+ config_spinner.succeed(`导入配置成功!`);
79
+ } else {
80
+ config_spinner.fail(`不存在的文件路径: [${filePath}]`);
81
+ }
82
+ } catch (error) {
83
+ config_spinner.fail(`不存在的文件路径: [${filePath}]`);
84
+ }
85
+ }
86
+ process.exit(1);
87
+ case "reset":
88
+ //重置
89
+ setLocalConfig({}, config_spinner)
90
+ config_spinner.succeed(`已重置为默认配置!`);
91
+ process.exit(1);
92
+ }
93
+
94
+ config_spinner.fail(`不存在操作: [${configName}]`);
95
+ process.exit(1);
96
+ },
97
+ };
@@ -0,0 +1,26 @@
1
+ import { dirname } from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ import path from "node:path";
4
+ import ora from "ora";
5
+ import { readJsonFile } from "../utils/common.js";
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = dirname(__filename);
8
+
9
+ export const registerCommand = (program, config) => {
10
+ let app = program.command(config.name).description(config.description);
11
+
12
+ config.options.forEach((option) => {
13
+ app.option(option.flags, option.description, option.defaultValue);
14
+ });
15
+
16
+ app.action(config.action);
17
+ };
18
+
19
+ export const initProgram = async (program, cb) => {
20
+ readJsonFile(path.resolve(__dirname, "../../package.json")).then((pkg) => {
21
+ program.usage("<command> [options]");
22
+ program.version(pkg.version);
23
+
24
+ cb && cb();
25
+ });
26
+ };
@@ -0,0 +1,122 @@
1
+ import {
2
+ writeFileContent,
3
+ getLocalConfig,
4
+ setHighLightStr,
5
+ } from "../utils/common.js";
6
+ import {
7
+ requestPicGo,
8
+ checkFileSize,
9
+ checkFileSizeAndFilter,
10
+ uploadFileByPicGo,
11
+ batchUploadByPicGo,
12
+ } from "../utils/picgo.js";
13
+ import { getFormatedFileSize, replaceFileContent } from "../utils/file.js";
14
+ import chalk from "chalk";
15
+ import ora from "ora";
16
+ import path from "node:path";
17
+ import { dirname } from "node:path";
18
+ import { fileURLToPath } from "node:url";
19
+ import fs from "node:fs";
20
+
21
+ const __filename = fileURLToPath(import.meta.url);
22
+ const __dirname = dirname(__filename);
23
+
24
+ const supportFileTypes = ["png", "jpg", "jpeg", "gif", "webp"];
25
+
26
+ export const picgoCmd = {
27
+ name: "picgo",
28
+ description: "上传图片(此命令暂时废弃, 原因见readme)",
29
+ options: [
30
+ {
31
+ flags: "-f, --file <file>",
32
+ description: "要上传的图片文件",
33
+ defaultValue: null,
34
+ },
35
+ {
36
+ flags: "-d, --dir <dir>",
37
+ description: "上传文件夹内所有文件",
38
+ defaultValue: null,
39
+ },
40
+ {
41
+ flags: "-co, --condition <condition>",
42
+ description: "上传文件夹内所有名称包含[--condition]的图片文件",
43
+ defaultValue: null,
44
+ },
45
+ {
46
+ flags: "-m, --max <max>",
47
+ description: "大于指定大小的图片不会被上传",
48
+ defaultValue: 60,
49
+ },
50
+ ],
51
+ action: async (option, test) => {
52
+ const spinner = ora();
53
+ const config = await getLocalConfig();
54
+ const { file, dir, condition, tinyFiles } = option;
55
+
56
+ if (!file && !dir && !condition && (!tinyFiles || !tinyFiles.length)) {
57
+ spinner.fail("请指定要上传的文件(--file=xxx)或文件夹(--dir=/a/b)");
58
+ process.exit(1);
59
+ }
60
+
61
+ // const _replaceMaps = replaceMaps.concat();
62
+ // 从压缩命令调用过来的
63
+ if (tinyFiles && tinyFiles.length) {
64
+ await batchUploadByPicGo(tinyFiles, option);
65
+ if (!option.replace) process.exit(1);
66
+ spinner.start("开始替换文件内容, 替换后请仔细检查!");
67
+ const fileContent = await replaceFileContent(
68
+ option.replaceFile,
69
+ option.replaceMaps
70
+ );
71
+ if (!fileContent) {
72
+ spinner.fail("替换文件内容失败! 请检查要替换的文件是否存在!");
73
+ process.exit(1);
74
+ }
75
+ fs.writeFile(option.replaceFile, fileContent, "utf-8", (error) => {
76
+ if (!error) {
77
+ spinner.succeed(
78
+ `[${chalk.red(
79
+ path.basename(option.replaceFile)
80
+ )}]图片链接替换完成!请前往检查!`
81
+ );
82
+ } else {
83
+ console.log(error);
84
+ // spinner.fail(`写入${chalk.red(filePath)}文件失败, 请重试`);
85
+ spinner.fail("图片链接替换失败!");
86
+ }
87
+ process.exit(1);
88
+ });
89
+ }
90
+ // 指定文件上传
91
+ if (file) {
92
+ await uploadFileByPicGo(file, option);
93
+ process.exit(1);
94
+ }
95
+
96
+ // 指定文件夹, 翻译文件夹内所有文件
97
+ if (dir || condition) {
98
+ let inputDirPath = dir || "./";
99
+ let files;
100
+ try {
101
+ // 读取所有文件
102
+ files = fs.readdirSync(inputDirPath);
103
+ files = files.map((filePath) =>
104
+ path.resolve(process.cwd(), inputDirPath, filePath)
105
+ );
106
+
107
+ files = files.filter((f) => {
108
+ let fileName = path.basename(f);
109
+ let index = fileName.indexOf(condition);
110
+ return index !== -1;
111
+ });
112
+ await batchUploadByPicGo(files, option);
113
+ // spinner.succeed(`共有${files.length}个文件${files}`);
114
+ } catch (err) {
115
+ spinner.fail("出错啦, 文件夹似乎不存在");
116
+ process.exit(1);
117
+ }
118
+
119
+ process.exit(1);
120
+ }
121
+ },
122
+ };
@@ -0,0 +1,81 @@
1
+ import {
2
+ writeFileContent,
3
+ getLocalConfig,
4
+ setHighLightStr,
5
+ } from "../utils/common.js";
6
+ import {
7
+ requestPicGo,
8
+ checkFileSize,
9
+ checkFileSizeAndFilter,
10
+ uploadFileByPicGo,
11
+ batchUploadByPicGo,
12
+ } from "../utils/picgo.js";
13
+ import { getFormatedFileSize } from "../utils/file.js";
14
+ import chalk from "chalk";
15
+ import ora from "ora";
16
+ import path from "node:path";
17
+ import { dirname } from "node:path";
18
+ import { fileURLToPath } from "node:url";
19
+ import fs from "node:fs";
20
+
21
+ const __filename = fileURLToPath(import.meta.url);
22
+ const __dirname = dirname(__filename);
23
+
24
+ const supportFileTypes = ["png", "jpg", "jpeg", "gif", "webp"];
25
+
26
+ export const replaceCmd = {
27
+ name: "replace",
28
+ description:
29
+ "替换文字功能, 用于上传后文件后, 自动更换markdown里的图片链接(还在开发中)",
30
+ options: [
31
+ {
32
+ flags: "-f, --file <file>",
33
+ description: "目标文件",
34
+ defaultValue: "",
35
+ },
36
+ {
37
+ flags: "-t, --text <text>",
38
+ description: "要替换的文字",
39
+ defaultValue: "",
40
+ },
41
+ {
42
+ flags: "-nt, --new-text <nextText>",
43
+ description: "替换后的文字内容",
44
+ defaultValue: null,
45
+ },
46
+ {
47
+ flags: "-co, --condition <condition>",
48
+ description: "上传文件夹内所有名称包含[--condition]的图片文件",
49
+ defaultValue: null,
50
+ },
51
+ {
52
+ flags: "-m, --max <max>",
53
+ description: "大于指定大小的图片不会被上传",
54
+ defaultValue: 60,
55
+ },
56
+ ],
57
+ action: async (option) => {
58
+ const spinner = ora();
59
+ const config = await getLocalConfig();
60
+ const { text, newText, file } = option;
61
+
62
+ if (!text && !newText && !file) {
63
+ spinner.fail("请指定要替换的文件及替换方式");
64
+ process.exit(1);
65
+ }
66
+
67
+ // 绝对路径
68
+ let filePath = path.resolve(process.cwd(), file);
69
+
70
+ try {
71
+ // 使用fs.readFileSync读取文件内容
72
+ // let fileContent = fs.readFileSync(filePath);
73
+ // 将文件内容转换为JSON对象
74
+ // fileContent = fileContent.replaceAll(text, newText)
75
+ // replaceFileContent(filePath, [{text, newText}])
76
+ } catch (err) {
77
+ // 如果读取文件失败,则将jsonData初始化为空对象
78
+ jsonData = {};
79
+ }
80
+ },
81
+ };