@hile/cli 1.1.4 → 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.
Files changed (2) hide show
  1. package/dist/index.js +87 -6
  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) => start({
23
- dev: options.dev,
24
- envFile: options.envFile,
25
- silent: options.silent
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/cli",
3
- "version": "1.1.4",
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": "64f79a1b10cbc557b5b4e72caf369fa328aa180b"
34
+ "gitHead": "aeb010516219fe30e80679b66787aba1b0d33018"
35
35
  }