node-karin 0.12.2-5.pr.206.e304472 → 0.12.2-5.pr.206.f697b50
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/default/config/config.json +1 -3
- package/dist/cli/index.cjs +527 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +518 -0
- package/{lib → dist}/index.d.ts +8 -43
- package/dist/index.js +12650 -0
- package/dist/module/dotenv.cjs +22 -0
- package/dist/module/dotenv.d.ts +2 -0
- package/dist/module/dotenv.js +2 -0
- package/dist/root.d.ts +40 -0
- package/dist/root.js +44 -0
- package/package.json +95 -79
- package/LICENSE +0 -21
- package/README.md +0 -29
- package/lib/index.js +0 -38
- /package/{packages → dist}/module/art-template.cjs +0 -0
- /package/{packages → dist}/module/art-template.d.ts +0 -0
- /package/{packages → dist}/module/art-template.js +0 -0
- /package/{packages → dist}/module/axios.cjs +0 -0
- /package/{packages → dist}/module/axios.d.ts +0 -0
- /package/{packages → dist}/module/axios.js +0 -0
- /package/{packages → dist}/module/chalk.cjs +0 -0
- /package/{packages → dist}/module/chalk.d.ts +0 -0
- /package/{packages → dist}/module/chalk.js +0 -0
- /package/{packages → dist}/module/chokidar.cjs +0 -0
- /package/{packages → dist}/module/chokidar.d.ts +0 -0
- /package/{packages → dist}/module/chokidar.js +0 -0
- /package/{packages → dist}/module/commander.cjs +0 -0
- /package/{packages → dist}/module/commander.d.ts +0 -0
- /package/{packages → dist}/module/commander.js +0 -0
- /package/{packages → dist}/module/express.cjs +0 -0
- /package/{packages → dist}/module/express.d.ts +0 -0
- /package/{packages → dist}/module/express.js +0 -0
- /package/{packages → dist}/module/level.cjs +0 -0
- /package/{packages → dist}/module/level.d.ts +0 -0
- /package/{packages → dist}/module/level.js +0 -0
- /package/{packages → dist}/module/lodash.cjs +0 -0
- /package/{packages → dist}/module/lodash.d.ts +0 -0
- /package/{packages → dist}/module/lodash.js +0 -0
- /package/{packages → dist}/module/log4js.cjs +0 -0
- /package/{packages → dist}/module/log4js.d.ts +0 -0
- /package/{packages → dist}/module/log4js.js +0 -0
- /package/{packages → dist}/module/moment.cjs +0 -0
- /package/{packages → dist}/module/moment.d.ts +0 -0
- /package/{packages → dist}/module/moment.js +0 -0
- /package/{packages → dist}/module/node-schedule.cjs +0 -0
- /package/{packages → dist}/module/node-schedule.d.ts +0 -0
- /package/{packages → dist}/module/node-schedule.js +0 -0
- /package/{packages → dist}/module/redis.cjs +0 -0
- /package/{packages → dist}/module/redis.d.ts +0 -0
- /package/{packages → dist}/module/redis.js +0 -0
- /package/{packages → dist}/module/ws.cjs +0 -0
- /package/{packages → dist}/module/ws.d.ts +0 -0
- /package/{packages → dist}/module/ws.js +0 -0
- /package/{packages → dist}/module/yaml.cjs +0 -0
- /package/{packages → dist}/module/yaml.d.ts +0 -0
- /package/{packages → dist}/module/yaml.js +0 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var fs2 = require('fs');
|
|
5
|
+
var url = require('url');
|
|
6
|
+
var path2 = require('path');
|
|
7
|
+
var child_process = require('child_process');
|
|
8
|
+
var dotenv = require('dotenv');
|
|
9
|
+
var commander = require('commander');
|
|
10
|
+
|
|
11
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
12
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
|
+
|
|
14
|
+
var fs2__default = /*#__PURE__*/_interopDefault(fs2);
|
|
15
|
+
var path2__default = /*#__PURE__*/_interopDefault(path2);
|
|
16
|
+
var dotenv__default = /*#__PURE__*/_interopDefault(dotenv);
|
|
17
|
+
|
|
18
|
+
var execSync = (cmd, options = {}) => {
|
|
19
|
+
try {
|
|
20
|
+
const result = child_process.execSync(cmd, options);
|
|
21
|
+
return { status: true, error: null, stdout: result.toString(), stderr: "" };
|
|
22
|
+
} catch (error) {
|
|
23
|
+
return { status: false, error, stdout: "", stderr: "" };
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var exec = (cmd, options = {}) => {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
child_process.exec(cmd, options, (error, stdout, stderr) => {
|
|
29
|
+
const status = !error;
|
|
30
|
+
resolve({ status, error, stdout, stderr });
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var pm2Dir = path2__default.default.join(process.cwd(), "./@karinjs/config/pm2.json");
|
|
35
|
+
var start = () => {
|
|
36
|
+
if (!fs2__default.default.existsSync(pm2Dir)) {
|
|
37
|
+
console.log(`[pm2] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728 \u8BF7\u68C0\u67E5 ${pm2Dir} \u662F\u5426\u5B58\u5728`);
|
|
38
|
+
console.log("[pm2] \u5982\u679C\u662F\u65B0\u9879\u76EE\uFF0C\u8BF7\u5148\u524D\u53F0\u542F\u52A8\u751F\u6210\u914D\u7F6E\u6587\u4EF6: pnpm app");
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
console.log("[pm2] \u542F\u52A8\u4E2D...");
|
|
42
|
+
const { error } = execSync(`pm2 start ${pm2Dir}`, { cwd: process.cwd() });
|
|
43
|
+
if (error) {
|
|
44
|
+
console.log("[pm2] \u542F\u52A8\u5931\u8D25");
|
|
45
|
+
console.log(error);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
console.log("[pm2] \u542F\u52A8\u6210\u529F");
|
|
49
|
+
console.log("[pm2] \u91CD\u542F\u670D\u52A1: pnpm rs");
|
|
50
|
+
console.log("[pm2] \u67E5\u770B\u65E5\u5FD7: pnpm log");
|
|
51
|
+
console.log("[pm2] \u505C\u6B62\u670D\u52A1: pnpm stop");
|
|
52
|
+
console.log("[pm2] \u67E5\u770B\u76D1\u63A7: pm2 monit");
|
|
53
|
+
console.log("[pm2] \u67E5\u770B\u5217\u8868: pm2 list");
|
|
54
|
+
process.exit(0);
|
|
55
|
+
};
|
|
56
|
+
var log = () => {
|
|
57
|
+
if (!fs2__default.default.existsSync(pm2Dir)) {
|
|
58
|
+
console.log(`[pm2] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728 \u8BF7\u68C0\u67E5 ${pm2Dir} \u662F\u5426\u5B58\u5728`);
|
|
59
|
+
console.log("[pm2] \u5982\u679C\u662F\u65B0\u9879\u76EE\uFF0C\u8BF7\u5148\u524D\u53F0\u542F\u52A8\u751F\u6210\u914D\u7F6E\u6587\u4EF6: pnpm app");
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
const data = JSON.parse(fs2__default.default.readFileSync(pm2Dir, "utf-8"));
|
|
63
|
+
try {
|
|
64
|
+
const prefix = process.platform === "win32" ? "pm2.cmd" : "pm2";
|
|
65
|
+
child_process.spawn(prefix, ["logs", data.name, "--lines", data.lines || 1e3], { stdio: "inherit", shell: true });
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error("[pm2] \u53D1\u751F\u672A\u77E5\u9519\u8BEF: \u8BF7\u68C0\u67E5pm2\u662F\u5426\u5B89\u88C5 \u3010npm install -g pm2\u3011");
|
|
68
|
+
console.error(error);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var stop = () => {
|
|
73
|
+
if (!fs2__default.default.existsSync(pm2Dir)) {
|
|
74
|
+
console.log(`[pm2] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728 \u8BF7\u68C0\u67E5 ${pm2Dir} \u662F\u5426\u5B58\u5728`);
|
|
75
|
+
console.log("[pm2] \u5982\u679C\u662F\u65B0\u9879\u76EE\uFF0C\u8BF7\u5148\u524D\u53F0\u542F\u52A8\u751F\u6210\u914D\u7F6E\u6587\u4EF6: pnpm app");
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
const data = JSON.parse(fs2__default.default.readFileSync(pm2Dir, "utf-8"));
|
|
79
|
+
execSync(`pm2 stop ${data.name}`, { cwd: process.cwd() });
|
|
80
|
+
console.log("[pm2] \u505C\u6B62\u6210\u529F");
|
|
81
|
+
process.exit(0);
|
|
82
|
+
};
|
|
83
|
+
var restart = () => {
|
|
84
|
+
if (!fs2__default.default.existsSync(pm2Dir)) {
|
|
85
|
+
console.log(`[pm2] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728 \u8BF7\u68C0\u67E5 ${pm2Dir} \u662F\u5426\u5B58\u5728`);
|
|
86
|
+
console.log("[pm2] \u5982\u679C\u662F\u65B0\u9879\u76EE\uFF0C\u8BF7\u5148\u524D\u53F0\u542F\u52A8\u751F\u6210\u914D\u7F6E\u6587\u4EF6: pnpm app");
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
var pm2 = {
|
|
91
|
+
start,
|
|
92
|
+
log,
|
|
93
|
+
stop,
|
|
94
|
+
restart
|
|
95
|
+
};
|
|
96
|
+
var dir = process.env.INIT_CWD || process.cwd();
|
|
97
|
+
var pkgDir = url.fileURLToPath(new url.URL("../..", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
|
|
98
|
+
var createDir = () => {
|
|
99
|
+
const list = [
|
|
100
|
+
path2__default.default.join(dir, "logs"),
|
|
101
|
+
path2__default.default.join(dir, "plugins", "karin-plugin-example"),
|
|
102
|
+
path2__default.default.join(dir, "@karinjs", "config"),
|
|
103
|
+
path2__default.default.join(dir, "@karinjs", "data"),
|
|
104
|
+
path2__default.default.join(dir, "@karinjs", "temp", "console"),
|
|
105
|
+
path2__default.default.join(dir, "@karinjs", "temp", "html"),
|
|
106
|
+
path2__default.default.join(dir, "@karinjs", "resource")
|
|
107
|
+
];
|
|
108
|
+
list.forEach((item) => {
|
|
109
|
+
if (!fs2__default.default.existsSync(item)) fs2__default.default.mkdirSync(item, { recursive: true });
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
var createPnpmFile = (dir2) => {
|
|
113
|
+
const pnpmfile = path2__default.default.join(dir2, ".pnpmfile.cjs");
|
|
114
|
+
if (fs2__default.default.existsSync(pnpmfile)) return;
|
|
115
|
+
const content = [
|
|
116
|
+
"// \u6E05\u7A7A\u5BF9\u7B49\u4F9D\u8D56\u4E2D\u7684node-karin",
|
|
117
|
+
"function readPackage (pkg, context) {",
|
|
118
|
+
" if (pkg?.['peerDependencies']?.['node-karin'] && pkg['peerDependencies']['node-karin'] !== 'file:./lib') {",
|
|
119
|
+
" delete pkg['peerDependencies']['node-karin']",
|
|
120
|
+
" }",
|
|
121
|
+
" return pkg",
|
|
122
|
+
"}",
|
|
123
|
+
"module.exports = {",
|
|
124
|
+
" hooks: {",
|
|
125
|
+
" readPackage",
|
|
126
|
+
" },",
|
|
127
|
+
"}"
|
|
128
|
+
].join("\n");
|
|
129
|
+
fs2__default.default.writeFileSync(pnpmfile, content);
|
|
130
|
+
};
|
|
131
|
+
var shouldSkipNpmrc = () => {
|
|
132
|
+
const { stdout } = execSync("npm config get registry");
|
|
133
|
+
if (stdout.includes("registry.npmjs.org")) return true;
|
|
134
|
+
const { stdout: proxy } = execSync("npm config get proxy");
|
|
135
|
+
return !!proxy;
|
|
136
|
+
};
|
|
137
|
+
var createOrUpdateNpmrc = (dir2) => {
|
|
138
|
+
const list = [
|
|
139
|
+
"node_sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/sqlite3",
|
|
140
|
+
"better_sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/better-sqlite3",
|
|
141
|
+
"sass_binary_site=https://registry.npmmirror.com/-/binary/node-sass",
|
|
142
|
+
"sharp_binary_host=https://registry.npmmirror.com/-/binary/sharp",
|
|
143
|
+
"sharp_libvips_binary_host=https://registry.npmmirror.com/-/binary/sharp-libvips",
|
|
144
|
+
"canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas",
|
|
145
|
+
"# 19\u4EE5\u4E0B\u7248\u672C",
|
|
146
|
+
"puppeteer_download_host=https://registry.npmmirror.com/mirrors",
|
|
147
|
+
"# 20\u4EE5\u4E0A\u7248\u672C",
|
|
148
|
+
"PUPPETEER_DOWNLOAD_BASE_URL = https://registry.npmmirror.com/binaries/chrome-for-testing"
|
|
149
|
+
];
|
|
150
|
+
const npmrc = path2__default.default.join(dir2, ".npmrc");
|
|
151
|
+
if (!fs2__default.default.existsSync(npmrc)) {
|
|
152
|
+
fs2__default.default.writeFileSync(npmrc, list.join("\n"));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const data = fs2__default.default.readFileSync(npmrc, "utf-8");
|
|
156
|
+
const newEntries = list.filter((item) => {
|
|
157
|
+
const key = item.split("=")[0];
|
|
158
|
+
return !data.includes(key);
|
|
159
|
+
});
|
|
160
|
+
if (newEntries.length > 0) {
|
|
161
|
+
fs2__default.default.appendFileSync(npmrc, "\n" + newEntries.join("\n"));
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var createOrUpdateEnv = (dir2) => {
|
|
165
|
+
const generateRandomKey = () => {
|
|
166
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
167
|
+
return Array.from({ length: 6 }, () => chars.charAt(Math.floor(Math.random() * chars.length))).join("");
|
|
168
|
+
};
|
|
169
|
+
const envData = [
|
|
170
|
+
"# \u662F\u5426\u542F\u7528HTTP",
|
|
171
|
+
"HTTP_ENABLE=true",
|
|
172
|
+
"# HTTP\u76D1\u542C\u7AEF\u53E3",
|
|
173
|
+
"HTTP_PORT=7777",
|
|
174
|
+
"# HTTP\u76D1\u542C\u5730\u5740",
|
|
175
|
+
"HTTP_HOST=0.0.0.0",
|
|
176
|
+
"# HTTP\u9274\u6743\u79D8\u94A5 \u4EC5\u7528\u4E8Ekarin\u81EA\u8EABApi",
|
|
177
|
+
`HTTP_AUTH_KEY=${generateRandomKey()}`,
|
|
178
|
+
"# ws_server\u9274\u6743\u79D8\u94A5",
|
|
179
|
+
`WS_SERVER_AUTH_KEY=${generateRandomKey()}`,
|
|
180
|
+
"\n",
|
|
181
|
+
"# \u662F\u5426\u542F\u7528Redis \u5173\u95ED\u540E\u5C06\u4F7F\u7528\u5185\u90E8\u865A\u62DFRedis",
|
|
182
|
+
"REDIS_ENABLE=true",
|
|
183
|
+
"# \u91CD\u542F\u662F\u5426\u8C03\u7528pm2 \u5982\u679C\u4E0D\u8C03\u7528\u5219\u4F1A\u76F4\u63A5\u5173\u673A \u6B64\u914D\u7F6E\u9002\u5408\u6709\u8FDB\u7A0B\u5B88\u62A4\u7684\u7A0B\u5E8F",
|
|
184
|
+
"PM2_RESTART=true",
|
|
185
|
+
"\n",
|
|
186
|
+
"# \u65E5\u5FD7\u7B49\u7EA7",
|
|
187
|
+
"LOG_LEVEL=info",
|
|
188
|
+
"# \u65E5\u5FD7\u4FDD\u7559\u5929\u6570",
|
|
189
|
+
"LOG_DAYS_TO_KEEP=7",
|
|
190
|
+
"# \u65E5\u5FD7\u6587\u4EF6\u6700\u5927\u5927\u5C0F \u5982\u679C\u6B64\u9879\u5927\u4E8E0\u5219\u542F\u7528\u65E5\u5FD7\u5206\u5272",
|
|
191
|
+
"LOG_MAX_LOG_SIZE=0",
|
|
192
|
+
"# logger.fnc\u989C\u8272",
|
|
193
|
+
'LOG_FNC_COLOR="#E1D919"',
|
|
194
|
+
"\n",
|
|
195
|
+
"# ffmpeg",
|
|
196
|
+
"FFMPEG_PATH=",
|
|
197
|
+
"# ffprobe",
|
|
198
|
+
"FFPROBE_PATH=",
|
|
199
|
+
"# ffplay",
|
|
200
|
+
"FFPLAY_PATH=",
|
|
201
|
+
"\n",
|
|
202
|
+
"# \u8FD9\u91CC\u8BF7\u52FF\u4FEE\u6539",
|
|
203
|
+
"RUNTIME=node"
|
|
204
|
+
];
|
|
205
|
+
const env = path2__default.default.join(dir2, ".env");
|
|
206
|
+
if (!fs2__default.default.existsSync(env)) {
|
|
207
|
+
fs2__default.default.writeFileSync(env, envData.join("\n"));
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const value = fs2__default.default.readFileSync(env, "utf-8");
|
|
211
|
+
const newEntries = envData.filter((item) => {
|
|
212
|
+
if (item.includes("#")) return false;
|
|
213
|
+
const key = item.split("=")[0];
|
|
214
|
+
return !value.includes(key);
|
|
215
|
+
});
|
|
216
|
+
if (newEntries.length > 0) {
|
|
217
|
+
fs2__default.default.appendFileSync(env, "\n" + newEntries.join("\n"));
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
var createWorkspace = (dir2) => {
|
|
221
|
+
const workspace = path2__default.default.join(dir2, "pnpm-workspace.yaml");
|
|
222
|
+
if (fs2__default.default.existsSync(workspace)) return;
|
|
223
|
+
const content = "packages:\n - 'plugins/**'\n";
|
|
224
|
+
fs2__default.default.writeFileSync(workspace, content);
|
|
225
|
+
};
|
|
226
|
+
var createOtherFile = async () => {
|
|
227
|
+
createPnpmFile(dir);
|
|
228
|
+
createWorkspace(dir);
|
|
229
|
+
if (!shouldSkipNpmrc()) {
|
|
230
|
+
createOrUpdateNpmrc(dir);
|
|
231
|
+
}
|
|
232
|
+
createOrUpdateEnv(dir);
|
|
233
|
+
};
|
|
234
|
+
var createConfig = () => {
|
|
235
|
+
const defCfg = path2__default.default.join(pkgDir, "default", "config");
|
|
236
|
+
const files = fs2__default.default.readdirSync(defCfg);
|
|
237
|
+
files.forEach((file) => {
|
|
238
|
+
if (!file.endsWith(".json")) return;
|
|
239
|
+
const filePath = path2__default.default.join(defCfg, file);
|
|
240
|
+
const targetPath = path2__default.default.join(dir, "@karinjs", "config");
|
|
241
|
+
const targetFile = path2__default.default.join(targetPath, file);
|
|
242
|
+
if (!fs2__default.default.existsSync(targetFile)) {
|
|
243
|
+
fs2__default.default.copyFileSync(filePath, targetFile);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const defData = fs2__default.default.readFileSync(filePath, "utf-8");
|
|
247
|
+
const targetData = fs2__default.default.readFileSync(targetFile, "utf-8");
|
|
248
|
+
const mergedData = { ...JSON.parse(defData), ...JSON.parse(targetData) };
|
|
249
|
+
fs2__default.default.writeFileSync(targetFile, JSON.stringify(mergedData, null, 2));
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
var modifyPackageJson = () => {
|
|
253
|
+
const pkg = fs2__default.default.readFileSync(path2__default.default.join(dir, "package.json"), "utf-8");
|
|
254
|
+
const data = JSON.parse(pkg);
|
|
255
|
+
data.type = "module";
|
|
256
|
+
data.scripts.karin = "karin";
|
|
257
|
+
const list = [
|
|
258
|
+
"app",
|
|
259
|
+
"start",
|
|
260
|
+
"pm2",
|
|
261
|
+
"stop",
|
|
262
|
+
"rs",
|
|
263
|
+
"log",
|
|
264
|
+
"up",
|
|
265
|
+
"init",
|
|
266
|
+
"dev",
|
|
267
|
+
"ts",
|
|
268
|
+
"watch"
|
|
269
|
+
];
|
|
270
|
+
list.forEach((v) => {
|
|
271
|
+
data.scripts[v] = `karin ${v}`;
|
|
272
|
+
});
|
|
273
|
+
fs2__default.default.writeFileSync(path2__default.default.join(dir, "package.json"), JSON.stringify(data, null, 2));
|
|
274
|
+
return data;
|
|
275
|
+
};
|
|
276
|
+
var init = async () => {
|
|
277
|
+
createDir();
|
|
278
|
+
await createOtherFile();
|
|
279
|
+
createConfig();
|
|
280
|
+
modifyPackageJson();
|
|
281
|
+
process.exit(0);
|
|
282
|
+
};
|
|
283
|
+
var parseEnvFiles = (env) => {
|
|
284
|
+
if (!env) return [".env"];
|
|
285
|
+
return env.split(",").map((file) => file.trim());
|
|
286
|
+
};
|
|
287
|
+
var loadEnv = (env) => {
|
|
288
|
+
const dir2 = process.cwd();
|
|
289
|
+
const files = parseEnvFiles(env);
|
|
290
|
+
if (!files.includes(".env")) {
|
|
291
|
+
files.unshift(".env");
|
|
292
|
+
}
|
|
293
|
+
files.forEach((file) => {
|
|
294
|
+
if (!fs2__default.default.existsSync(`${dir2}/${file}`)) {
|
|
295
|
+
if (file === ".env") {
|
|
296
|
+
console.error(`\u672A\u627E\u5230${file}\u6587\u4EF6\uFF0C\u8BF7\u4F7F\u7528 pnpm init \u8FDB\u884C\u521D\u59CB\u5316\u9879\u76EE`);
|
|
297
|
+
} else {
|
|
298
|
+
console.error(`\u672A\u627E\u5230${file}\u6587\u4EF6\uFF0C\u8BF7\u5C06\u5176\u653E\u7F6E\u5728\u9879\u76EE\u6839\u76EE\u5F55`);
|
|
299
|
+
}
|
|
300
|
+
process.exit(1);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
const paths = files.map((file) => `${dir2}/${file}`);
|
|
304
|
+
dotenv__default.default.config({ path: paths, override: true });
|
|
305
|
+
};
|
|
306
|
+
var start2 = async (env) => {
|
|
307
|
+
loadEnv(env);
|
|
308
|
+
const index = "../root.js";
|
|
309
|
+
const { karinMain } = await import(index);
|
|
310
|
+
const child = child_process.fork(karinMain);
|
|
311
|
+
child.on("message", (message) => {
|
|
312
|
+
if (message === "restart") {
|
|
313
|
+
child.kill();
|
|
314
|
+
child.removeAllListeners();
|
|
315
|
+
start2(env);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
child.on("exit", (code) => process.exit(code));
|
|
319
|
+
};
|
|
320
|
+
var dev = async (env) => {
|
|
321
|
+
loadEnv(env);
|
|
322
|
+
const index = "../root.js";
|
|
323
|
+
process.env.NODE_ENV = "development";
|
|
324
|
+
const { karinMain } = await import(index);
|
|
325
|
+
await import(url.pathToFileURL(karinMain).toString());
|
|
326
|
+
};
|
|
327
|
+
var tsStart = async (env) => {
|
|
328
|
+
loadEnv(env);
|
|
329
|
+
process.env.NODE_ENV = "development";
|
|
330
|
+
const index = "../root.js";
|
|
331
|
+
const { karinMain } = await import(index);
|
|
332
|
+
const child = child_process.spawn("npx", ["tsx", karinMain], {
|
|
333
|
+
env: {
|
|
334
|
+
...process.env,
|
|
335
|
+
RUNTIME: "tsx"
|
|
336
|
+
},
|
|
337
|
+
stdio: "inherit",
|
|
338
|
+
shell: true
|
|
339
|
+
});
|
|
340
|
+
child.on("exit", (code) => process.exit(code ?? 0));
|
|
341
|
+
};
|
|
342
|
+
var tsWatch = async (options) => {
|
|
343
|
+
loadEnv(options.env);
|
|
344
|
+
process.env.NODE_ENV = "development";
|
|
345
|
+
const index = "../root.js";
|
|
346
|
+
const { karinMain } = await import(index);
|
|
347
|
+
const args = ["tsx", "watch"];
|
|
348
|
+
if (options.include) {
|
|
349
|
+
options.include.split(",").forEach((pattern) => {
|
|
350
|
+
args.push("--include", pattern.trim());
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
if (options.exclude) {
|
|
354
|
+
options.exclude.split(",").forEach((pattern) => {
|
|
355
|
+
args.push("--exclude", pattern.trim());
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (!options.clearScreen) {
|
|
359
|
+
args.push("--clear-screen=false");
|
|
360
|
+
}
|
|
361
|
+
args.push(karinMain);
|
|
362
|
+
const child = child_process.spawn("npx", args, {
|
|
363
|
+
env: {
|
|
364
|
+
...process.env,
|
|
365
|
+
RUNTIME: "tsx",
|
|
366
|
+
TSX_WATCH: "true"
|
|
367
|
+
},
|
|
368
|
+
stdio: "inherit",
|
|
369
|
+
shell: true
|
|
370
|
+
});
|
|
371
|
+
child.on("exit", (code) => process.exit(code ?? 0));
|
|
372
|
+
};
|
|
373
|
+
var checkGitInstalled = async () => {
|
|
374
|
+
try {
|
|
375
|
+
const { status } = await exec("git --version");
|
|
376
|
+
return status;
|
|
377
|
+
} catch {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
var updateDependencies = async (packagePath) => {
|
|
382
|
+
try {
|
|
383
|
+
const packageJson = JSON.parse(fs2__default.default.readFileSync(packagePath, "utf-8"));
|
|
384
|
+
const dependencies = packageJson.dependencies || {};
|
|
385
|
+
const packagesToUpdate = [];
|
|
386
|
+
const checkVersionTasks = Object.entries(dependencies).map(async ([pkg, version]) => {
|
|
387
|
+
if (pkg.startsWith("@types/")) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
try {
|
|
391
|
+
const { stdout: pkgInfoStr } = await exec(`pnpm view ${pkg} --json`);
|
|
392
|
+
const pkgInfo = JSON.parse(pkgInfoStr.trim());
|
|
393
|
+
if (!pkgInfo.karin) return;
|
|
394
|
+
const currentVersion = version.replace(/[\^~]/g, "");
|
|
395
|
+
const { stdout: distTagsStr } = await exec(`pnpm view ${pkg} dist-tags --json`);
|
|
396
|
+
const distTags = JSON.parse(distTagsStr.trim());
|
|
397
|
+
const isPreRelease = /[-+]/.test(currentVersion);
|
|
398
|
+
const latestVersion = distTags.latest;
|
|
399
|
+
console.log(`[npm] ${pkg}:`);
|
|
400
|
+
console.log(` \u5F53\u524D\u7248\u672C: ${currentVersion}${isPreRelease ? " (\u9884\u53D1\u5E03\u7248\u672C)" : ""}`);
|
|
401
|
+
console.log(` \u6700\u65B0\u7248\u672C: ${latestVersion}`);
|
|
402
|
+
if (isPreRelease || currentVersion !== latestVersion) {
|
|
403
|
+
console.log(`[npm] \u53D1\u73B0\u65B0\u7248\u672C ${pkg}: ${currentVersion} -> ${latestVersion}`);
|
|
404
|
+
packagesToUpdate.push(`${pkg}@latest`);
|
|
405
|
+
} else {
|
|
406
|
+
console.log(`[npm] ${pkg} \u5DF2\u7ECF\u662F\u6700\u65B0\u7248\u672C`);
|
|
407
|
+
}
|
|
408
|
+
} catch (error) {
|
|
409
|
+
console.error(`[npm] \u68C0\u67E5 ${pkg} \u7248\u672C\u5931\u8D25:`, error);
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
await Promise.all(checkVersionTasks);
|
|
413
|
+
if (packagesToUpdate.length > 0) {
|
|
414
|
+
console.log("[npm] \u5F00\u59CB\u66F4\u65B0\u4EE5\u4E0B\u5305:\n", packagesToUpdate.join("\n"));
|
|
415
|
+
try {
|
|
416
|
+
const { status, error } = await exec(`pnpm update ${packagesToUpdate.join(" ")}`);
|
|
417
|
+
if (status) {
|
|
418
|
+
console.log("[npm] \u6240\u6709\u63D2\u4EF6\u66F4\u65B0\u5B8C\u6210");
|
|
419
|
+
} else {
|
|
420
|
+
console.error("[npm] \u6279\u91CF\u66F4\u65B0\u5931\u8D25:", error);
|
|
421
|
+
}
|
|
422
|
+
} catch (error) {
|
|
423
|
+
console.error("[npm] \u6279\u91CF\u66F4\u65B0\u5931\u8D25:", error);
|
|
424
|
+
}
|
|
425
|
+
} else {
|
|
426
|
+
console.log("[npm] \u6240\u6709\u63D2\u4EF6\u90FD\u5DF2\u7ECF\u662F\u6700\u65B0\u7248\u672C");
|
|
427
|
+
}
|
|
428
|
+
} catch (error) {
|
|
429
|
+
console.error("[npm] \u66F4\u65B0\u4F9D\u8D56\u5931\u8D25:", error);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
var updatePlugins = async (pluginsPath) => {
|
|
433
|
+
if (!await checkGitInstalled()) {
|
|
434
|
+
console.error("[git] \u8BF7\u5148\u5B89\u88C5git");
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (!fs2__default.default.existsSync(pluginsPath)) {
|
|
438
|
+
console.error("[git] plugins\u76EE\u5F55\u4E0D\u5B58\u5728");
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const isDirectory = (await fs2__default.default.promises.stat(pluginsPath)).isDirectory();
|
|
442
|
+
if (!isDirectory) {
|
|
443
|
+
console.error("[git] plugins\u8DEF\u5F84\u4E0D\u662F\u4E00\u4E2A\u76EE\u5F55");
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const dirs = await fs2__default.default.promises.readdir(pluginsPath);
|
|
447
|
+
const updateTasks = dirs.map(async (dir2) => {
|
|
448
|
+
const pluginPath = path2.join(pluginsPath, dir2);
|
|
449
|
+
const gitPath = path2.join(pluginPath, ".git");
|
|
450
|
+
const packageJsonPath = path2.join(pluginPath, "package.json");
|
|
451
|
+
if (!dir2.startsWith("karin-plugin-") || !(await fs2__default.default.promises.stat(pluginPath)).isDirectory()) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (!fs2__default.default.existsSync(gitPath)) {
|
|
455
|
+
console.log(`[git] ${dir2} \u4E0D\u662Fgit\u4ED3\u5E93\uFF0C\u8DF3\u8FC7`);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
try {
|
|
459
|
+
const packageJson = JSON.parse(fs2__default.default.readFileSync(packageJsonPath, "utf-8"));
|
|
460
|
+
if (!packageJson.karin) {
|
|
461
|
+
console.log(`[git] ${dir2} \u7684package.json\u4E2D\u6CA1\u6709karin\u5B57\u6BB5\uFF0C\u8DF3\u8FC7`);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
console.log(`[git] \u6B63\u5728\u66F4\u65B0 ${dir2}...`);
|
|
465
|
+
const { status, error } = await exec("git pull", { cwd: pluginPath });
|
|
466
|
+
if (!status) {
|
|
467
|
+
console.error(`[git] \u66F4\u65B0 ${dir2} \u5931\u8D25:`, error);
|
|
468
|
+
}
|
|
469
|
+
} catch (error) {
|
|
470
|
+
console.error(`[git] \u66F4\u65B0 ${dir2} \u5931\u8D25:`, error);
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
await Promise.all(updateTasks);
|
|
474
|
+
};
|
|
475
|
+
var updateAll = async () => {
|
|
476
|
+
try {
|
|
477
|
+
console.log("[all] \u5F00\u59CB\u6267\u884C\u66F4\u65B0\u4EFB\u52A1\n");
|
|
478
|
+
await Promise.all([updateDependencies("./package.json"), updatePlugins("./plugins")]);
|
|
479
|
+
console.log("\n[all] \u66F4\u65B0\u4EFB\u52A1\u6267\u884C\u5B8C\u6210");
|
|
480
|
+
} finally {
|
|
481
|
+
process.exit(0);
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
var _a;
|
|
485
|
+
if (!((_a = process.argv) == null ? undefined : _a[2])) process.argv.push("-h");
|
|
486
|
+
var addEnvOption = (command) => {
|
|
487
|
+
return command.option("-e, --env <files>", "\u6307\u5B9A\u73AF\u5883\u53D8\u91CF\u6587\u4EF6\uFF0C\u591A\u4E2A\u6587\u4EF6\u7528\u9017\u53F7\u5206\u9694");
|
|
488
|
+
};
|
|
489
|
+
var getVersion = () => {
|
|
490
|
+
if (process.env.npm_package_version) return process.env.npm_package_version;
|
|
491
|
+
try {
|
|
492
|
+
const file = url.fileURLToPath(new URL("../../package.json", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
|
|
493
|
+
const packageJson = JSON.parse(fs2__default.default.readFileSync(file, "utf-8"));
|
|
494
|
+
return packageJson.version;
|
|
495
|
+
} catch {
|
|
496
|
+
return "unknown";
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
commander.program.version(getVersion(), "-v, --version", "\u663E\u793A\u7248\u672C\u53F7");
|
|
500
|
+
commander.program.command("pm2").description("\u540E\u53F0\u8FD0\u884C").action(pm2.start);
|
|
501
|
+
commander.program.command("stop").description("\u505C\u6B62\u540E\u53F0\u8FD0\u884C").action(pm2.stop);
|
|
502
|
+
commander.program.command("rs").description("\u91CD\u542Fpm2\u670D\u52A1").action(pm2.restart);
|
|
503
|
+
commander.program.command("log").description("\u67E5\u770B\u65E5\u5FD7").action(pm2.log);
|
|
504
|
+
commander.program.command("up").description("\u66F4\u65B0\u63D2\u4EF6").action(updateAll);
|
|
505
|
+
commander.program.command("init").description("\u521D\u59CB\u5316\u9879\u76EE").action(init);
|
|
506
|
+
addEnvOption(commander.program.command(".").description("\u524D\u53F0\u542F\u52A8")).action((options) => start2(options.env));
|
|
507
|
+
addEnvOption(commander.program.command("app").description("\u524D\u53F0\u542F\u52A8")).action((options) => start2(options.env));
|
|
508
|
+
addEnvOption(commander.program.command("start").description("\u524D\u53F0\u542F\u52A8")).action((options) => start2(options.env));
|
|
509
|
+
addEnvOption(commander.program.command("dev").description("JavaScript\u5F00\u53D1\u6A21\u5F0F")).action((options) => dev(options.env));
|
|
510
|
+
addEnvOption(
|
|
511
|
+
commander.program.command("ts").description("TypeScript\u5F00\u53D1\u6A21\u5F0F").addHelpText("after", `
|
|
512
|
+
\u793A\u4F8B:
|
|
513
|
+
$ karin ts # \u4F7F\u7528\u9ED8\u8BA4 .env \u6587\u4EF6\u542F\u52A8
|
|
514
|
+
$ karin ts -e .env.dev # \u4F7F\u7528\u6307\u5B9A\u7684\u73AF\u5883\u53D8\u91CF\u6587\u4EF6\u542F\u52A8
|
|
515
|
+
$ karin ts -e .env.dev,.env.local # \u4F7F\u7528\u591A\u4E2A\u73AF\u5883\u53D8\u91CF\u6587\u4EF6\u542F\u52A8
|
|
516
|
+
`)
|
|
517
|
+
).action((options) => tsStart(options.env));
|
|
518
|
+
addEnvOption(
|
|
519
|
+
commander.program.command("watch").description("TypeScript\u76D1\u89C6\u6A21\u5F0F").option("--include <patterns>", "\u6DFB\u52A0\u8981\u76D1\u89C6\u7684\u6587\u4EF6/\u76EE\u5F55\uFF0C\u591A\u4E2A\u7528\u9017\u53F7\u5206\u9694").option("--exclude <patterns>", "\u6392\u9664\u8981\u76D1\u89C6\u7684\u6587\u4EF6/\u76EE\u5F55\uFF0C\u591A\u4E2A\u7528\u9017\u53F7\u5206\u9694").option("--clear-screen", "\u91CD\u65B0\u8FD0\u884C\u65F6\u662F\u5426\u6E05\u5C4F", true).addHelpText("after", `
|
|
520
|
+
\u793A\u4F8B:
|
|
521
|
+
$ karin watch # \u9ED8\u8BA4\u76D1\u89C6\u6A21\u5F0F
|
|
522
|
+
$ karin watch --include src # \u76D1\u89C6 src \u76EE\u5F55
|
|
523
|
+
$ karin watch --exclude node_modules,dist # \u6392\u9664\u6307\u5B9A\u76EE\u5F55
|
|
524
|
+
$ karin watch --no-clear-screen # \u7981\u7528\u6E05\u5C4F
|
|
525
|
+
`)
|
|
526
|
+
).action((options) => tsWatch(options));
|
|
527
|
+
commander.program.parse(process.argv);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|