@hile/cli 1.1.3 → 1.1.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/dist/index.js +87 -6
- package/dist/start.d.ts +1 -0
- package/dist/start.js +5 -2
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -2,6 +2,62 @@
|
|
|
2
2
|
import pkg from '../package.json' with { type: 'json' };
|
|
3
3
|
import { program } from 'commander';
|
|
4
4
|
import { start } from './start.js';
|
|
5
|
+
import { execSync } from 'node:child_process';
|
|
6
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
7
|
+
import { dirname, join, resolve } from 'node:path';
|
|
8
|
+
import { createRequire } from 'node:module';
|
|
9
|
+
const requireCli = createRequire(import.meta.url);
|
|
10
|
+
/** 从包入口文件路径向上找到 `package.json` 的 `name` 与 `packageName` 一致的目录 */
|
|
11
|
+
function packageDirFromResolvedMain(resolvedMain, packageName) {
|
|
12
|
+
let dir = dirname(resolvedMain);
|
|
13
|
+
for (let depth = 0; depth < 50; depth++) {
|
|
14
|
+
const parent = dirname(dir);
|
|
15
|
+
const pkgPath = join(dir, 'package.json');
|
|
16
|
+
if (existsSync(pkgPath)) {
|
|
17
|
+
try {
|
|
18
|
+
const { name: n } = JSON.parse(readFileSync(pkgPath, 'utf8'));
|
|
19
|
+
if (n === packageName)
|
|
20
|
+
return dir;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
/* 忽略损坏的 package.json */
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (dir === parent)
|
|
27
|
+
break;
|
|
28
|
+
dir = parent;
|
|
29
|
+
}
|
|
30
|
+
return dirname(resolvedMain);
|
|
31
|
+
}
|
|
32
|
+
function tryResolvePackageDir(packageName, req) {
|
|
33
|
+
try {
|
|
34
|
+
const main = req.resolve(packageName);
|
|
35
|
+
return packageDirFromResolvedMain(main, packageName);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** 全局 `npm install -g` 下的包根目录(存在 package.json 时) */
|
|
42
|
+
function tryGlobalPackageDir(packageName) {
|
|
43
|
+
try {
|
|
44
|
+
const root = execSync('npm root -g', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] }).trim();
|
|
45
|
+
let candidate;
|
|
46
|
+
if (packageName.startsWith('@')) {
|
|
47
|
+
const slash = packageName.indexOf('/');
|
|
48
|
+
if (slash <= 0)
|
|
49
|
+
return undefined;
|
|
50
|
+
candidate = join(root, packageName.slice(0, slash), packageName.slice(slash + 1));
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
candidate = join(root, packageName);
|
|
54
|
+
}
|
|
55
|
+
return existsSync(join(candidate, 'package.json')) ? candidate : undefined;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
5
61
|
program.version(pkg.version, '-v, --version', '当前版本号');
|
|
6
62
|
/**
|
|
7
63
|
* 启动服务
|
|
@@ -14,15 +70,40 @@ program.version(pkg.version, '-v, --version', '当前版本号');
|
|
|
14
70
|
* @returns - 启动服务
|
|
15
71
|
*/
|
|
16
72
|
program
|
|
17
|
-
.command('start')
|
|
73
|
+
.command('start [name]')
|
|
18
74
|
.option('-d, --dev', '开发模式', false)
|
|
19
75
|
.option('-s, --silent', '静默模式', false)
|
|
20
76
|
.option('-e, --env-file <path>', '加载指定 env 文件(兼容 Node --env-file 语义;可多次指定,先加载的不被后加载覆盖)', (v, acc) => (acc.push(v), acc), [])
|
|
21
77
|
.description('启动服务,加载所有后缀为 boot.ts 或 boot.js 的服务,并注册退出钩子,在进程退出时销毁所有服务')
|
|
22
|
-
.action((options) =>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
78
|
+
.action((name, options) => {
|
|
79
|
+
const cwd = process.cwd();
|
|
80
|
+
const filePath = resolve(cwd, name);
|
|
81
|
+
let directory;
|
|
82
|
+
if (existsSync(filePath)) {
|
|
83
|
+
directory = filePath;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const cwdPkg = resolve(cwd, 'package.json');
|
|
87
|
+
if (existsSync(cwdPkg)) {
|
|
88
|
+
directory = tryResolvePackageDir(name, createRequire(cwdPkg));
|
|
89
|
+
}
|
|
90
|
+
if (!directory) {
|
|
91
|
+
directory = tryResolvePackageDir(name, requireCli);
|
|
92
|
+
}
|
|
93
|
+
if (!directory) {
|
|
94
|
+
directory = tryGlobalPackageDir(name);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!directory) {
|
|
98
|
+
console.error('package not found');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
return start({
|
|
102
|
+
dev: options.dev,
|
|
103
|
+
cwd: directory,
|
|
104
|
+
envFile: options.envFile,
|
|
105
|
+
silent: options.silent
|
|
106
|
+
});
|
|
107
|
+
});
|
|
27
108
|
program.parseAsync(process.argv);
|
|
28
109
|
export * from './start.js';
|
package/dist/start.d.ts
CHANGED
package/dist/start.js
CHANGED
|
@@ -77,7 +77,7 @@ export async function start(options) {
|
|
|
77
77
|
const offEvent = !options.silent && process.env.NODE_ENV === 'development'
|
|
78
78
|
? container.on(logContainerEvent)
|
|
79
79
|
: () => { };
|
|
80
|
-
const cwd = process.cwd();
|
|
80
|
+
const cwd = options.cwd ?? process.cwd();
|
|
81
81
|
const files = [];
|
|
82
82
|
// 加载 package.json 文件
|
|
83
83
|
// 如果 package.json 中存在 hile.auto_load_packages 属性,则加载该属性值中的所有服务
|
|
@@ -89,7 +89,7 @@ export async function start(options) {
|
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
// 加载所有后缀为 boot.ts 或 boot.js 的服务
|
|
92
|
-
const directory = resolve(cwd,
|
|
92
|
+
const directory = resolve(cwd, options.dev ? 'src' : 'dist');
|
|
93
93
|
const _files = await glob(`**/*.boot.{ts,js}`, { cwd: directory });
|
|
94
94
|
files.push(..._files.map(file => resolve(directory, file)));
|
|
95
95
|
// 加载所有自启动服务
|
|
@@ -103,6 +103,9 @@ export async function start(options) {
|
|
|
103
103
|
if (!fn || !isService(fn))
|
|
104
104
|
throw new Error(`invalid service file: ${file}`);
|
|
105
105
|
await loadService(fn);
|
|
106
|
+
if (!options.silent) {
|
|
107
|
+
console.info(`+ [bootstrap] ${file}`);
|
|
108
|
+
}
|
|
106
109
|
}));
|
|
107
110
|
// 如果没有服务要加载,则提示
|
|
108
111
|
if (!files.length) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/cli",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"glob": "^13.0.6",
|
|
32
32
|
"tsx": "^4.21.0"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "aeb010516219fe30e80679b66787aba1b0d33018"
|
|
35
35
|
}
|