@skillbuilder/minitool-cli 0.0.0-beta-20261027053041051

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.
Files changed (79) hide show
  1. package/README.md +185 -0
  2. package/bin/redminitool.js +176 -0
  3. package/lib/bootstrap.js +69 -0
  4. package/lib/commands/auth/identity.js +98 -0
  5. package/lib/commands/auth/index.js +20 -0
  6. package/lib/commands/auth/login.js +148 -0
  7. package/lib/commands/auth/logout.js +52 -0
  8. package/lib/commands/auth/storage.js +105 -0
  9. package/lib/commands/dev/dev.test.js +108 -0
  10. package/lib/commands/dev.js +119 -0
  11. package/lib/commands/index.js +19 -0
  12. package/lib/commands/minitool/cli.test.js +222 -0
  13. package/lib/commands/minitool/commands.test.js +119 -0
  14. package/lib/commands/minitool/common.js +103 -0
  15. package/lib/commands/minitool/config.js +51 -0
  16. package/lib/commands/minitool/edit.js +55 -0
  17. package/lib/commands/minitool/history.js +56 -0
  18. package/lib/commands/minitool/index.js +24 -0
  19. package/lib/commands/minitool/list.js +65 -0
  20. package/lib/commands/minitool/preview.js +115 -0
  21. package/lib/commands/minitool/status.js +55 -0
  22. package/lib/commands/minitool/submit.js +58 -0
  23. package/lib/commands/minitool/upload.js +80 -0
  24. package/lib/core/base.js +133 -0
  25. package/lib/decorators/auth.js +26 -0
  26. package/lib/decorators/captureError.js +32 -0
  27. package/lib/decorators/constants.js +11 -0
  28. package/lib/decorators/decorators.test.js +94 -0
  29. package/lib/decorators/index.js +21 -0
  30. package/lib/decorators/injectParams.js +30 -0
  31. package/lib/decorators/json-output.js +18 -0
  32. package/lib/decorators/params/common.js +21 -0
  33. package/lib/decorators/params/index.js +24 -0
  34. package/lib/index.js +18 -0
  35. package/lib/main.js +26 -0
  36. package/lib/mcp.js +197 -0
  37. package/lib/output.js +7 -0
  38. package/lib/ui.js +39 -0
  39. package/lib/utils/agent-check.js +157 -0
  40. package/lib/utils/templates.js +75 -0
  41. package/package.json +48 -0
  42. package/types/bootstrap.d.ts +14 -0
  43. package/types/commands/auth/identity.d.ts +9 -0
  44. package/types/commands/auth/index.d.ts +4 -0
  45. package/types/commands/auth/login.d.ts +13 -0
  46. package/types/commands/auth/logout.d.ts +7 -0
  47. package/types/commands/auth/storage.d.ts +14 -0
  48. package/types/commands/dev/dev.test.d.ts +1 -0
  49. package/types/commands/dev.d.ts +8 -0
  50. package/types/commands/index.d.ts +3 -0
  51. package/types/commands/minitool/cli.test.d.ts +1 -0
  52. package/types/commands/minitool/commands.test.d.ts +1 -0
  53. package/types/commands/minitool/common.d.ts +12 -0
  54. package/types/commands/minitool/config.d.ts +5 -0
  55. package/types/commands/minitool/edit.d.ts +20 -0
  56. package/types/commands/minitool/history.d.ts +5 -0
  57. package/types/commands/minitool/index.d.ts +8 -0
  58. package/types/commands/minitool/list.d.ts +5 -0
  59. package/types/commands/minitool/preview.d.ts +23 -0
  60. package/types/commands/minitool/status.d.ts +5 -0
  61. package/types/commands/minitool/submit.d.ts +25 -0
  62. package/types/commands/minitool/upload.d.ts +20 -0
  63. package/types/core/base.d.ts +37 -0
  64. package/types/decorators/auth.d.ts +1 -0
  65. package/types/decorators/captureError.d.ts +1 -0
  66. package/types/decorators/constants.d.ts +7 -0
  67. package/types/decorators/decorators.test.d.ts +1 -0
  68. package/types/decorators/index.d.ts +5 -0
  69. package/types/decorators/injectParams.d.ts +1 -0
  70. package/types/decorators/json-output.d.ts +2 -0
  71. package/types/decorators/params/common.d.ts +10 -0
  72. package/types/decorators/params/index.d.ts +5 -0
  73. package/types/index.d.ts +2 -0
  74. package/types/main.d.ts +5 -0
  75. package/types/mcp.d.ts +9 -0
  76. package/types/output.d.ts +4 -0
  77. package/types/ui.d.ts +4 -0
  78. package/types/utils/agent-check.d.ts +16 -0
  79. package/types/utils/templates.d.ts +17 -0
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ };
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
42
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
+ return new (P || (P = Promise))(function (resolve, reject) {
44
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
45
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
46
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
47
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
48
+ });
49
+ };
50
+ Object.defineProperty(exports, "__esModule", { value: true });
51
+ exports.LoginCommand = void 0;
52
+ const fs = __importStar(require("fs"));
53
+ const os = __importStar(require("os"));
54
+ const path = __importStar(require("path"));
55
+ const base_1 = require("../../core/base");
56
+ const ui_1 = require("../../ui");
57
+ let LoginCommand = class LoginCommand extends base_1.Command {
58
+ execute(ctx) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ var _a, _b, _c;
61
+ if (ctx.options.cancel) {
62
+ yield this.services.auth.cancelLogin();
63
+ return { cancelled: true };
64
+ }
65
+ const method = (_a = ctx.options.method) !== null && _a !== void 0 ? _a : "qr";
66
+ if (method !== "qr" && method !== "secret")
67
+ throw new Error("--method 仅支持 qr 或 secret");
68
+ let result;
69
+ if (method === "secret") {
70
+ if (!process.stdin.isTTY)
71
+ throw new Error("Secret 登录需在交互终端输入密钥");
72
+ const answers = yield require("inquirer").prompt([
73
+ { type: "input", name: "secretId", message: "Secret ID:" },
74
+ { type: "password", name: "secretKey", message: "Secret Key:", mask: "*" },
75
+ ]);
76
+ result = yield this.services.auth.loginWithSecret(answers.secretId, answers.secretKey);
77
+ }
78
+ else {
79
+ let qrDirectory;
80
+ const controller = new AbortController();
81
+ const cancel = () => controller.abort();
82
+ process.once("SIGINT", cancel);
83
+ process.once("SIGTERM", cancel);
84
+ try {
85
+ result = yield this.services.auth.loginWithQr({
86
+ signal: controller.signal,
87
+ onPrompt: (prompt) => __awaiter(this, void 0, void 0, function* () {
88
+ if (this.isJson(ctx)) {
89
+ qrDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "redminitool-login-"));
90
+ const qrCodePath = path.join(qrDirectory, "auth-qr.png");
91
+ fs.writeFileSync(qrCodePath, yield require("qrcode").toBuffer(prompt.authorizeCommonUrl), { mode: 0o600, flag: "wx" });
92
+ process.stderr.write(`AUTH_PROMPT:${JSON.stringify(Object.assign(Object.assign({}, prompt), { qrCodePath, message: "扫码后,手机端提示终端授权码时,请输入 userCode 并确认授权" }))}\n`);
93
+ }
94
+ else {
95
+ process.stderr.write("请使用小红书 App 扫描二维码完成授权:\n");
96
+ process.stderr.write(yield require("qrcode").toString(prompt.authorizeCommonUrl, {
97
+ type: "terminal",
98
+ small: true,
99
+ }));
100
+ process.stderr.write(`终端授权码:${prompt.userCode} · 有效期至:${prompt.expiresAt}\n`);
101
+ process.stderr.write("手机端提示「终端授权码」时,请输入上述代码并确认授权。\n");
102
+ process.stderr.write(`无法扫码时可在手机浏览器打开:\n${prompt.authorizeH5Url}\n`);
103
+ }
104
+ }),
105
+ });
106
+ }
107
+ finally {
108
+ process.removeListener("SIGINT", cancel);
109
+ process.removeListener("SIGTERM", cancel);
110
+ if (qrDirectory)
111
+ require("fs").rmSync(qrDirectory, { recursive: true, force: true });
112
+ }
113
+ }
114
+ const storage = this.services.credentialStorage.inspect();
115
+ const output = {
116
+ authenticated: true,
117
+ creator: (_b = result.creator) !== null && _b !== void 0 ? _b : null,
118
+ credentialStore: storage.configuredMode,
119
+ credentialProvider: storage.provider,
120
+ expiresAt: result.expiresAt,
121
+ };
122
+ if (!this.isJson(ctx)) {
123
+ (0, ui_1.printHeading)("登录成功");
124
+ (0, ui_1.printDetails)([
125
+ ["创作者", (_c = result.creator) === null || _c === void 0 ? void 0 : _c.displayName],
126
+ ["凭据存储", storage.provider],
127
+ ["有效期至", result.expiresAt],
128
+ ]);
129
+ (0, ui_1.printHint)("下一步:redminitool list --source <平台分配的来源>");
130
+ }
131
+ return output;
132
+ });
133
+ }
134
+ get options() {
135
+ return {
136
+ cmd: "login",
137
+ desc: "使用小红书 App 扫码登录",
138
+ options: [
139
+ { flags: "--method <method>", desc: "qr(默认)或 secret(交互输入密钥)" },
140
+ { flags: "--cancel", desc: "取消当前环境正在等待的扫码登录" },
141
+ ],
142
+ };
143
+ }
144
+ };
145
+ exports.LoginCommand = LoginCommand;
146
+ exports.LoginCommand = LoginCommand = __decorate([
147
+ (0, base_1.ICommand)()
148
+ ], LoginCommand);
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.LogoutCommand = void 0;
19
+ const base_1 = require("../../core/base");
20
+ const ui_1 = require("../../ui");
21
+ let LogoutCommand = class LogoutCommand extends base_1.Command {
22
+ execute(ctx) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const json = this.isJson(ctx);
25
+ const spinner = json ? undefined : require("ora")("正在清除本地登录态...").start();
26
+ try {
27
+ yield this.services.auth.logout();
28
+ spinner === null || spinner === void 0 ? void 0 : spinner.succeed("本地登录态已清除");
29
+ if (!json) {
30
+ (0, ui_1.printHeading)("已退出登录", "本机凭据已清除");
31
+ (0, ui_1.printHint)("如需重新登录,请运行 redminitool login");
32
+ }
33
+ return { loggedOut: true };
34
+ }
35
+ catch (error) {
36
+ spinner === null || spinner === void 0 ? void 0 : spinner.fail("清除登录态失败");
37
+ throw error;
38
+ }
39
+ });
40
+ }
41
+ get options() {
42
+ return {
43
+ cmd: "logout",
44
+ desc: "退出登录并清除本地登录信息",
45
+ options: [],
46
+ };
47
+ }
48
+ };
49
+ exports.LogoutCommand = LogoutCommand;
50
+ exports.LogoutCommand = LogoutCommand = __decorate([
51
+ (0, base_1.ICommand)()
52
+ ], LogoutCommand);
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.AuthStorageCommand = void 0;
19
+ const base_1 = require("../../core/base");
20
+ const ui_1 = require("../../ui");
21
+ let AuthStorageCommand = class AuthStorageCommand extends base_1.Command {
22
+ execute(ctx) {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const requested = ctx.options.set === "keyring" ? "keychain" : ctx.options.set;
25
+ const checkRequested = Boolean(ctx.options.check);
26
+ if (checkRequested && requested !== undefined) {
27
+ throw new Error("--check 只做只读检查,不能与 --set 同时使用");
28
+ }
29
+ let sourceRetained = false;
30
+ if (requested !== undefined) {
31
+ if (requested !== "file" && requested !== "keychain")
32
+ throw new Error("存储方式只支持 file、keychain(或跨平台别名 keyring)");
33
+ sourceRetained = this.services.credentialStorage.use(requested).sourceRetained;
34
+ }
35
+ const status = this.services.credentialStorage.inspect(checkRequested);
36
+ if (checkRequested && status.keyringAvailable === false) {
37
+ const reason = status.keyringIssue === "package_unavailable"
38
+ ? "Keyring 原生包或平台二进制不可用"
39
+ : "系统凭据库不可访问或已锁定";
40
+ const guidance = status.configuredMode === "keychain"
41
+ ? "如需改用本地文件,请运行 redminitool auth storage --set file;旧 Keyring 条目将保留,并需要重新登录"
42
+ : "当前已使用本地文件,无需降级;修复 Keyring 环境后再切换";
43
+ throw new Error(`${reason}。${guidance}。`);
44
+ }
45
+ const mode = status.configuredMode;
46
+ const authState = yield this.services.auth.getAuthState();
47
+ const output = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ credentialStore: mode, provider: status.provider, protection: status.protection }, (status.keyringAvailable === undefined
48
+ ? {}
49
+ : { keyringAvailable: status.keyringAvailable })), (status.keyringIssue === undefined ? {} : { keyringIssue: status.keyringIssue })), (sourceRetained ? { sourceRetained: true } : {})), { authState: authState.status }), (authState.status === "authenticated" ? {} : { authReason: authState.reason }));
50
+ if (this.isJson(ctx))
51
+ return output;
52
+ (0, ui_1.printHeading)("凭据存储", mode === "keychain" ? "由系统 Keychain 托管登录态" : "本地受限文件模式");
53
+ (0, ui_1.printDetails)([
54
+ ["当前模式", mode === "keychain" ? "系统 Keyring" : "本地文件"],
55
+ ["实际后端", status.provider],
56
+ [
57
+ "Keyring 检查",
58
+ status.keyringAvailable === undefined
59
+ ? undefined
60
+ : status.keyringAvailable
61
+ ? "可用"
62
+ : "不可用",
63
+ ],
64
+ [
65
+ "登录状态",
66
+ authState.status === "authenticated"
67
+ ? "已登录"
68
+ : authState.status === "unavailable"
69
+ ? "凭据存储不可用"
70
+ : authState.reason === "expired"
71
+ ? "已过期"
72
+ : "未登录",
73
+ ],
74
+ ]);
75
+ if (sourceRetained) {
76
+ (0, ui_1.printHint)("已切换到 auth.json;原 Keyring 不可读取且条目未删除,请重新登录。");
77
+ }
78
+ if (mode === "file")
79
+ (0, ui_1.printHint)("auth.json 依赖 0600 权限,不提供静态加密。");
80
+ (0, ui_1.printHint)("切换:redminitool auth storage --set keyring|file;检测:--check");
81
+ return output;
82
+ });
83
+ }
84
+ get options() {
85
+ return {
86
+ cmd: "auth",
87
+ childCmd: { cmd: "storage", desc: "查看或切换登录凭据存储方式" },
88
+ desc: "查看或切换登录凭据存储方式",
89
+ options: [
90
+ {
91
+ flags: "--set <mode>",
92
+ desc: "切换为 file 或 keyring(兼容 keychain),并迁移已有登录态",
93
+ },
94
+ {
95
+ flags: "--check",
96
+ desc: "检查 Keyring 包与系统凭据库;不可用时提示人工切换到 auth.json",
97
+ },
98
+ ],
99
+ };
100
+ }
101
+ };
102
+ exports.AuthStorageCommand = AuthStorageCommand;
103
+ exports.AuthStorageCommand = AuthStorageCommand = __decorate([
104
+ (0, base_1.ICommand)()
105
+ ], AuthStorageCommand);
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ const assert_1 = require("assert");
46
+ const child_process_1 = require("child_process");
47
+ const fs = __importStar(require("fs"));
48
+ const os = __importStar(require("os"));
49
+ const path = __importStar(require("path"));
50
+ const test = require("node:test");
51
+ const binary = path.resolve(__dirname, "../../../bin/redminitool.js");
52
+ test("dev 二进制使用模拟器文件协议、监听变更并在退出时释放服务", { timeout: 20000 }, (t) => __awaiter(void 0, void 0, void 0, function* () {
53
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "minitool-dev-"));
54
+ const html = path.join(root, "demo.html");
55
+ fs.writeFileSync(html, "<h1>first</h1>");
56
+ const child = (0, child_process_1.spawn)(process.execPath, [binary, "dev", "--no-open", "--json"], {
57
+ cwd: root,
58
+ env: Object.assign(Object.assign({}, process.env), { CI: "1", REDMINITOOL_HOME: path.join(root, ".auth") }),
59
+ stdio: ["ignore", "pipe", "pipe"],
60
+ });
61
+ t.after(() => {
62
+ if (child.exitCode === null)
63
+ child.kill("SIGKILL");
64
+ require("fs").rmSync(root, { recursive: true, force: true });
65
+ });
66
+ let stdout = "";
67
+ let stderr = "";
68
+ child.stdout.on("data", (data) => {
69
+ stdout += data;
70
+ });
71
+ const exited = new Promise((resolve) => child.once("exit", resolve));
72
+ const ready = yield new Promise((resolve, reject) => {
73
+ child.once("error", reject);
74
+ child.once("exit", () => reject(new Error(`dev 提前退出: ${stdout} ${stderr}`)));
75
+ child.stderr.on("data", (data) => {
76
+ stderr += data;
77
+ const match = stderr.match(/^DEV_READY:(.+)\n/m);
78
+ if (match)
79
+ resolve(JSON.parse(match[1]));
80
+ });
81
+ });
82
+ assert_1.strict.equal(fs.realpathSync(ready.rootDir), fs.realpathSync(root));
83
+ assert_1.strict.equal(new URL(ready.workbenchUrl).hostname, "127.0.0.1");
84
+ const get = (route) => fetch(new URL(route, ready.workbenchUrl));
85
+ const config = yield (yield get("/__miniapp_workbench__/config.json")).json();
86
+ assert_1.strict.equal(config.artifact.type, "files");
87
+ assert_1.strict.deepEqual(yield (yield get(config.artifact.url)).json(), {
88
+ entry: "demo.html",
89
+ files: ["demo.html"],
90
+ });
91
+ assert_1.strict.equal((yield get(config.sdkUrl)).status, 200);
92
+ assert_1.strict.ok((yield (yield get("/")).text()).includes("createMiniAppWorkbench"));
93
+ assert_1.strict.equal(yield (yield get(`${config.artifact.baseUrl}demo.html`)).text(), "<h1>first</h1>");
94
+ const controller = new AbortController();
95
+ t.after(() => controller.abort());
96
+ const events = yield fetch(new URL(config.artifact.eventsUrl, ready.workbenchUrl), {
97
+ signal: controller.signal,
98
+ });
99
+ const reader = events.body.getReader();
100
+ assert_1.strict.ok(new TextDecoder().decode((yield reader.read()).value).includes("event: reload"));
101
+ fs.writeFileSync(html, "<h1>updated</h1>");
102
+ assert_1.strict.ok(new TextDecoder().decode((yield reader.read()).value).includes("event: reload"));
103
+ assert_1.strict.equal(yield (yield get(`${config.artifact.baseUrl}demo.html`)).text(), "<h1>updated</h1>");
104
+ child.kill("SIGTERM");
105
+ assert_1.strict.equal(yield exited, 0);
106
+ assert_1.strict.deepEqual(JSON.parse(stdout), { ok: true, data: ready });
107
+ yield assert_1.strict.rejects(() => get("/"));
108
+ }));
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ };
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
42
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
+ return new (P || (P = Promise))(function (resolve, reject) {
44
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
45
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
46
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
47
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
48
+ });
49
+ };
50
+ var __importDefault = (this && this.__importDefault) || function (mod) {
51
+ return (mod && mod.__esModule) ? mod : { "default": mod };
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.DevCommand = void 0;
55
+ const path = __importStar(require("path"));
56
+ const open_1 = __importDefault(require("open"));
57
+ const base_1 = require("../core/base");
58
+ const ui_1 = require("../ui");
59
+ const loadWorkbenchServer = new Function('return import("@skillbuilder/miniapp-simulator/workbench/server")');
60
+ let DevCommand = class DevCommand extends base_1.Command {
61
+ execute(ctx) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ var _a;
64
+ const rootDir = path.resolve(String((_a = ctx.options.dir) !== null && _a !== void 0 ? _a : "."));
65
+ const { startWorkbenchServer } = yield loadWorkbenchServer();
66
+ const server = yield startWorkbenchServer({ rootDir });
67
+ let stop;
68
+ const stopped = new Promise((resolve) => {
69
+ stop = resolve;
70
+ process.once("SIGINT", stop);
71
+ process.once("SIGTERM", stop);
72
+ });
73
+ const result = { rootDir, workbenchUrl: server.url };
74
+ try {
75
+ if (this.isJson(ctx)) {
76
+ process.stderr.write(`DEV_READY:${JSON.stringify(result)}\n`);
77
+ }
78
+ else {
79
+ (0, ui_1.printHeading)("本地调试工作台已启动", "由 @skillbuilder/miniapp-simulator 提供");
80
+ (0, ui_1.printDetails)([
81
+ ["产物目录", rootDir],
82
+ ["工作台地址", server.url],
83
+ ]);
84
+ (0, ui_1.printHint)("文件更新后自动重载;按 Ctrl+C 停止本地服务");
85
+ }
86
+ if (ctx.options.open !== false) {
87
+ try {
88
+ yield (0, open_1.default)(server.url);
89
+ }
90
+ catch (_b) {
91
+ if (!this.isJson(ctx))
92
+ (0, ui_1.printHint)(`浏览器唤起失败,请手动打开 ${server.url}`);
93
+ }
94
+ }
95
+ yield stopped;
96
+ return result;
97
+ }
98
+ finally {
99
+ process.removeListener("SIGINT", stop);
100
+ process.removeListener("SIGTERM", stop);
101
+ yield server.close();
102
+ }
103
+ });
104
+ }
105
+ get options() {
106
+ return {
107
+ cmd: "dev",
108
+ desc: "启动本地小工具调试工作台",
109
+ options: [
110
+ { flags: "--dir <path>", desc: "包含 HTML 和静态资源的目录,默认当前目录" },
111
+ { flags: "--no-open", desc: "不自动打开浏览器" },
112
+ ],
113
+ };
114
+ }
115
+ };
116
+ exports.DevCommand = DevCommand;
117
+ exports.DevCommand = DevCommand = __decorate([
118
+ (0, base_1.ICommand)()
119
+ ], DevCommand);
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./auth"), exports);
18
+ __exportStar(require("./dev"), exports);
19
+ __exportStar(require("./minitool"), exports);