@tomjs/hbuilderx-cli 1.1.0 → 1.3.0
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/dist/index.js +36 -13
- package/dist/types.d.ts +42 -0
- package/dist/types.js +1 -0
- package/package.json +10 -3
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { cosmiconfig } from "cosmiconfig";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { mkdirp, readJson, writeFile } from "@tomjs/node";
|
|
7
7
|
import archiver from "archiver";
|
|
8
|
+
import { glob } from "glob";
|
|
8
9
|
import colors from "picocolors";
|
|
9
10
|
import Logger from "@tomjs/logger";
|
|
10
11
|
import chokidar from "chokidar";
|
|
@@ -28,9 +29,16 @@ async function getConfig(opts) {
|
|
|
28
29
|
return (await explorer.search())?.config || {};
|
|
29
30
|
}
|
|
30
31
|
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/constant.ts
|
|
34
|
+
const LOG_PREFIX = "[tomjs:hbuilderx]";
|
|
35
|
+
|
|
31
36
|
//#endregion
|
|
32
37
|
//#region src/util.ts
|
|
33
|
-
const logger = new Logger({
|
|
38
|
+
const logger = new Logger({
|
|
39
|
+
prefix: LOG_PREFIX,
|
|
40
|
+
time: true
|
|
41
|
+
});
|
|
34
42
|
const isWindows = process.platform === "win32";
|
|
35
43
|
let _opts = {};
|
|
36
44
|
function setOptions(opts) {
|
|
@@ -40,12 +48,6 @@ function setOptions(opts) {
|
|
|
40
48
|
//#endregion
|
|
41
49
|
//#region src/pack.ts
|
|
42
50
|
async function packExtension(opts) {
|
|
43
|
-
const packFiles = Array.isArray(opts.packFiles) ? opts.packFiles : [
|
|
44
|
-
"dist",
|
|
45
|
-
"package.json",
|
|
46
|
-
"license"
|
|
47
|
-
];
|
|
48
|
-
if (!packFiles.includes("package.json")) packFiles.push("package.json");
|
|
49
51
|
const cwd = opts.cwd || process.cwd();
|
|
50
52
|
const pkgPath = path.join(cwd, "package.json");
|
|
51
53
|
if (!fs.existsSync(pkgPath)) {
|
|
@@ -53,12 +55,23 @@ async function packExtension(opts) {
|
|
|
53
55
|
return;
|
|
54
56
|
}
|
|
55
57
|
const pkg = await readJson(pkgPath);
|
|
56
|
-
if (!
|
|
58
|
+
if (!checkPackageFields(pkg)) return;
|
|
57
59
|
const mainFile = path.join(cwd, pkg.main);
|
|
58
60
|
if (!fs.existsSync(mainFile)) {
|
|
59
61
|
logger.error(`未找到 ${colors.bold("package.json")} 中 ${colors.bold("main")} 字段值路径: ${colors.green(mainFile)}`);
|
|
60
62
|
return;
|
|
61
63
|
}
|
|
64
|
+
const packFilters = Array.isArray(pkg.files) && pkg.files.length ? pkg.files : [
|
|
65
|
+
"dist",
|
|
66
|
+
"resources",
|
|
67
|
+
"package.json",
|
|
68
|
+
"LICENSE"
|
|
69
|
+
];
|
|
70
|
+
const pkgFiles = await glob(packFilters.filter((s) => !s.startsWith("!")), {
|
|
71
|
+
cwd,
|
|
72
|
+
ignore: packFilters.filter((s) => s.startsWith("!")).map((s) => s.slice(1)).concat(["node_modules/**"])
|
|
73
|
+
});
|
|
74
|
+
console.log(pkgFiles);
|
|
62
75
|
logger.info(`${colors.blue(pkg.name)} 插件打包开始`);
|
|
63
76
|
const archive = archiver("zip", { zlib: { level: 9 } });
|
|
64
77
|
archive.on("error", (err) => {
|
|
@@ -74,7 +87,7 @@ async function packExtension(opts) {
|
|
|
74
87
|
});
|
|
75
88
|
const output = fs.createWriteStream(path.join(cwd, `${pkg.name}.zip`));
|
|
76
89
|
archive.pipe(output);
|
|
77
|
-
|
|
90
|
+
pkgFiles.forEach((file) => {
|
|
78
91
|
const filePath = path.join(cwd, file);
|
|
79
92
|
if (!fs.existsSync(filePath)) return;
|
|
80
93
|
if (fs.lstatSync(filePath).isDirectory()) archive.directory(filePath, file);
|
|
@@ -82,7 +95,7 @@ async function packExtension(opts) {
|
|
|
82
95
|
});
|
|
83
96
|
archive.finalize();
|
|
84
97
|
}
|
|
85
|
-
function
|
|
98
|
+
function checkPackageFields(pkg) {
|
|
86
99
|
if (!pkg) {
|
|
87
100
|
logger.error("package.json 文件格式错误");
|
|
88
101
|
return false;
|
|
@@ -116,7 +129,7 @@ function createWatcher(paths, callback) {
|
|
|
116
129
|
let ready = false;
|
|
117
130
|
watcher.on("ready", async () => {
|
|
118
131
|
ready = true;
|
|
119
|
-
logger.info(
|
|
132
|
+
logger.info(`监听 ${watchPaths.map((s) => colors.green(s))}`);
|
|
120
133
|
await callback();
|
|
121
134
|
});
|
|
122
135
|
watcher.on("all", async (event, path$1) => {
|
|
@@ -142,9 +155,9 @@ async function watchFiles(opts) {
|
|
|
142
155
|
}
|
|
143
156
|
async function genDtsFromPkg(pkgPath, opts) {
|
|
144
157
|
const pkg = await readJson(pkgPath);
|
|
145
|
-
if (!pkg) return logger.error(`${colors.
|
|
158
|
+
if (!pkg) return logger.error(`${colors.blue("package.json")} 文件解析失败`);
|
|
146
159
|
const contributes = pkg.contributes;
|
|
147
|
-
if (!contributes) return logger.error(`${colors.
|
|
160
|
+
if (!contributes) return logger.error(`${colors.blue("package.json")} 文件缺少 contributes 属性`);
|
|
148
161
|
const code = `// 通过 @tomjs/hbuilderx-cli 生成的 d.ts 文件
|
|
149
162
|
declare module 'hbuilderx' {
|
|
150
163
|
${getCommandCode(contributes, opts)}
|
|
@@ -252,6 +265,7 @@ Usage
|
|
|
252
265
|
Options
|
|
253
266
|
--watch, -w 监听 package.json,生产 d.ts 文件
|
|
254
267
|
--pack, -p 生成插件打包文件
|
|
268
|
+
--flag, -f 日志标识:time/symbol/none,默认 "time"
|
|
255
269
|
--config 指定配置文件,如 "hx-cli.config.mjs"
|
|
256
270
|
--verbose 显示更多信息
|
|
257
271
|
--help, -h 显示帮助信息
|
|
@@ -274,6 +288,14 @@ Examples
|
|
|
274
288
|
type: "boolean",
|
|
275
289
|
shortFlag: "p"
|
|
276
290
|
},
|
|
291
|
+
flag: {
|
|
292
|
+
type: "string",
|
|
293
|
+
choices: [
|
|
294
|
+
"time",
|
|
295
|
+
"symbol",
|
|
296
|
+
"none"
|
|
297
|
+
]
|
|
298
|
+
},
|
|
277
299
|
verbose: {
|
|
278
300
|
type: "boolean",
|
|
279
301
|
default: false
|
|
@@ -293,6 +315,7 @@ if (flags.help) cli.showHelp(0);
|
|
|
293
315
|
else if (flags.version) cli.showVersion();
|
|
294
316
|
else {
|
|
295
317
|
logger.enableDebug(flags.verbose);
|
|
318
|
+
logger.setOptions({ flag: flags.flag || "time" });
|
|
296
319
|
const cliOpts = Object.assign({ cwd: input[0] }, flags);
|
|
297
320
|
logger.debug("cli options:", cliOpts);
|
|
298
321
|
const config = await getConfig(cliOpts);
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* cli 参数
|
|
4
|
+
*/
|
|
5
|
+
interface CliOptions {
|
|
6
|
+
/**
|
|
7
|
+
* 监听文件变化
|
|
8
|
+
*/
|
|
9
|
+
watch?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 打包插件,根据 package.json 中的 files 字段打包,如果未设置,默认取 dist,resources,package.json,LICENSE 文件夹或文件。
|
|
12
|
+
*/
|
|
13
|
+
pack?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* 时间前缀
|
|
16
|
+
*/
|
|
17
|
+
time?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* 当前工作目录/根目录
|
|
20
|
+
*/
|
|
21
|
+
cwd?: string;
|
|
22
|
+
/**
|
|
23
|
+
* 配置文件
|
|
24
|
+
*/
|
|
25
|
+
config?: string;
|
|
26
|
+
/**
|
|
27
|
+
* 生成 d.ts 文件路径
|
|
28
|
+
* @default 'hbuilderx.d.ts'
|
|
29
|
+
*/
|
|
30
|
+
dts?: string;
|
|
31
|
+
/**
|
|
32
|
+
* 添加到生成的 d.ts 中的命令,如用到的内置命令
|
|
33
|
+
*/
|
|
34
|
+
commands?: string[];
|
|
35
|
+
/**
|
|
36
|
+
* verbose 模式
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
verbose?: boolean;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
export { CliOptions };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomjs/hbuilderx-cli",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"description": "为 HBuilderX 开发插件提供便利的 cli 工具,根据 package.json 中的 contributes 配置,为 hbuilderx 的命令、视图等 API 提供 id 提示",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Tom Gao",
|
|
@@ -19,6 +19,12 @@
|
|
|
19
19
|
"extension",
|
|
20
20
|
"webview"
|
|
21
21
|
],
|
|
22
|
+
"exports": {
|
|
23
|
+
"./types": {
|
|
24
|
+
"types": "./dist/types.d.ts"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"types": "./dist/types.d.ts",
|
|
22
28
|
"bin": {
|
|
23
29
|
"hx-cli": "./dist/index.js"
|
|
24
30
|
},
|
|
@@ -29,11 +35,12 @@
|
|
|
29
35
|
"node": ">=20"
|
|
30
36
|
},
|
|
31
37
|
"dependencies": {
|
|
32
|
-
"@tomjs/logger": "^
|
|
38
|
+
"@tomjs/logger": "^3.0.0",
|
|
33
39
|
"@tomjs/node": "^2.2.3",
|
|
34
40
|
"archiver": "^7.0.1",
|
|
35
41
|
"chokidar": "^5.0.0",
|
|
36
42
|
"cosmiconfig": "^9.0.0",
|
|
43
|
+
"glob": "^13.0.0",
|
|
37
44
|
"meow": "^14.0.0",
|
|
38
45
|
"picocolors": "^1.1.1"
|
|
39
46
|
},
|
|
@@ -42,7 +49,7 @@
|
|
|
42
49
|
},
|
|
43
50
|
"scripts": {
|
|
44
51
|
"dev": "tsdown --watch",
|
|
45
|
-
"build": "tsdown",
|
|
52
|
+
"build": "rimraf ./dist && tsdown",
|
|
46
53
|
"lint": "eslint --fix"
|
|
47
54
|
}
|
|
48
55
|
}
|