dsh-plugin-gate-kit 0.1.2 → 0.1.3
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/README.md +117 -6
- package/README.zh-CN.md +123 -0
- package/bin/dsh-plugin-gate-kit.mjs +28 -37
- package/package.json +3 -2
- package/src/preflight.mjs +15 -0
package/README.md
CHANGED
|
@@ -1,12 +1,123 @@
|
|
|
1
1
|
# dsh-plugin-gate-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
English | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Minimum gate bootstrap for a DSH plugin repo: a **cross-OS native-package preflight** (the classic "`node_modules` was installed by another OS" trap) plus `init` to drop a `gate/preflight.mjs` into your repo.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i -D dsh-plugin-gate-kit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
4
14
|
|
|
5
15
|
```bash
|
|
6
|
-
|
|
7
|
-
dsh-plugin-gate-kit
|
|
8
|
-
|
|
16
|
+
# is this node_modules safe for the current OS?
|
|
17
|
+
npx dsh-plugin-gate-kit preflight
|
|
18
|
+
|
|
19
|
+
# scaffold gate/preflight.mjs into a repo (idempotent)
|
|
20
|
+
npx dsh-plugin-gate-kit init --dir .
|
|
21
|
+
|
|
22
|
+
# preflight + what gate pieces already exist
|
|
23
|
+
npx dsh-plugin-gate-kit check --dir .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage / API
|
|
27
|
+
|
|
28
|
+
### CLI
|
|
29
|
+
|
|
30
|
+
| Command | What it does |
|
|
31
|
+
|---|---|
|
|
32
|
+
| `preflight` | Detect native packages belonging to a different OS. **Default** — running with no subcommand does this |
|
|
33
|
+
| `init` | Write `gate/preflight.mjs` (skipped if it already exists) and print wiring suggestions |
|
|
34
|
+
| `check` | Run the preflight **and** report which gate components and `package.json` scripts already exist |
|
|
35
|
+
|
|
36
|
+
### Options
|
|
37
|
+
|
|
38
|
+
| Option | Default | Meaning |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `--dir <dir>` | `.` | Directory whose `node_modules` is inspected (for `init`, where `gate/` is written). Must be a **separate token**: `--dir path`, not `--dir=path` |
|
|
41
|
+
| `--help`, `-h`, `help` | — | Print usage and exit `0` |
|
|
42
|
+
|
|
43
|
+
The subcommand must come **first**. `dsh-plugin-gate-kit --dir .` is rejected as `未知子命令: --dir` (exit `2`).
|
|
44
|
+
|
|
45
|
+
### Exit codes
|
|
46
|
+
|
|
47
|
+
| Code | Meaning |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `0` | `preflight` clean / `init` succeeded / `check` found no platform problems |
|
|
50
|
+
| `1` | Platform native-package problems found (both `preflight` and `check`) |
|
|
51
|
+
| `2` | Usage error: unknown subcommand |
|
|
52
|
+
|
|
53
|
+
### Programmatic API
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { checkNative, assertNativePlatform } from 'dsh-plugin-gate-kit'
|
|
57
|
+
|
|
58
|
+
const problems = checkNative(process.cwd()) // [] = clean
|
|
59
|
+
assertNativePlatform(process.cwd()) // throws when problems exist
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
| Export | Signature | Returns / does |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| `checkNative` | `checkNative(dir)` | Array of `{ type, pkg, detail }`; empty array means clean |
|
|
65
|
+
| `assertNativePlatform` | `assertNativePlatform(dir = process.cwd())` | Throws an `Error` listing every problem, with the fix hint |
|
|
66
|
+
|
|
67
|
+
`type` is either `'wrong-os'` (a native package for another platform) or `'missing-current'` (jscpd installed but its current-platform binary is absent).
|
|
68
|
+
|
|
69
|
+
## What the preflight detects
|
|
70
|
+
|
|
71
|
+
It reads `<dir>/node_modules` (scoped packages included) and matches directory names against:
|
|
72
|
+
|
|
73
|
+
```text
|
|
74
|
+
(?:@scope/)?<name>-(windows|linux|darwin)-(x64|arm64|ia32)(-|$)
|
|
9
75
|
```
|
|
10
76
|
|
|
11
|
-
|
|
12
|
-
|
|
77
|
+
| Rule | Example | Reported as |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| Native package for a different OS | `jscpd-linux-x64-gnu` on Windows | `wrong-os` |
|
|
80
|
+
| jscpd present but no `jscpd-<currentOS>-*` | `jscpd` installed, no `jscpd-windows-*` | `missing-current` |
|
|
81
|
+
|
|
82
|
+
The failure message tells you the fix: **reinstall with `npm ci` on the current system** — do not edit `package.json` / `package-lock.json`. Sharing one `node_modules` between Windows and WSL is what causes the mismatch, because npm rewrites `optionalDependencies` native packages for whatever platform is installing.
|
|
83
|
+
|
|
84
|
+
A missing `node_modules` is not an error — it returns an empty array.
|
|
85
|
+
|
|
86
|
+
### `init` output
|
|
87
|
+
|
|
88
|
+
`init` writes `gate/preflight.mjs` and then prints two wiring options:
|
|
89
|
+
|
|
90
|
+
1. at the top of your dup-gate script: `import { assertNativePlatform } from './preflight.mjs'; assertNativePlatform()`
|
|
91
|
+
2. or in `package.json`: `scripts: { "preflight": "node gate/preflight.mjs" }`
|
|
92
|
+
|
|
93
|
+
Re-running `init` prints `已存在:… (跳过)` and changes nothing.
|
|
94
|
+
|
|
95
|
+
### `check` output
|
|
96
|
+
|
|
97
|
+
```text
|
|
98
|
+
gate check: .
|
|
99
|
+
✓ preflight: ok
|
|
100
|
+
gate 组件已存在: dup_check.mjs, lint_runner.mjs, format_check.mjs
|
|
101
|
+
package.json 相关 scripts: check, check:fast, lint, format, dup:check
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
It looks for `gate/dup_check.mjs`, `gate/lint_runner.mjs`, `gate/format_check.mjs`, and any `package.json` script whose name matches `check|dup|lint|format`. This is a **report**, not a gate: missing components are listed, not failed.
|
|
105
|
+
|
|
106
|
+
## Scope — what this package does *not* do
|
|
107
|
+
|
|
108
|
+
This is deliberately a **minimum** bootstrap. The full four-layer gate (four Git hooks + three ratchets for lint / duplication / formatting) is **out of scope**. `check` only reports which pieces are missing; it does not install or enforce them. Bring your own gate scheme, or use the scaffold as the preflight step inside it.
|
|
109
|
+
|
|
110
|
+
## Notes
|
|
111
|
+
|
|
112
|
+
- The scaffolded `gate/preflight.mjs` is a **simplified copy**, not the package's implementation. Two real differences:
|
|
113
|
+
- it only implements the `wrong-os` rule (no `missing-current` / jscpd check);
|
|
114
|
+
- `checkNative` there returns an array of **strings**, whereas the package's export returns `{ type, pkg, detail }` objects.
|
|
115
|
+
If you depend on the return shape or on the jscpd rule, call the package's `checkNative` / `assertNativePlatform` instead of the generated file.
|
|
116
|
+
- The generated script's CLI path calls `checkNative()` with **no argument**, so running `node gate/preflight.mjs` inspects the **current working directory**, not the `--dir` you passed to `init`. Run it from the repo root, or pass a directory explicitly when importing it.
|
|
117
|
+
- `--dir` is read positionally; `--dir=value` silently falls back to `.`.
|
|
118
|
+
- `init` never overwrites an existing `gate/preflight.mjs` — delete it first if you want a fresh copy.
|
|
119
|
+
- Requires **Node >= 22.15.0** (repo-wide baseline).
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# dsh-plugin-gate-kit
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
DSH 插件仓库的**最小门禁引导**:跨 OS 原生包 **preflight**(「`node_modules` 被另一个系统装过」这个经典坑)+ `init` 把 `gate/preflight.mjs` 铺进仓库。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i -D dsh-plugin-gate-kit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 快速开始
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# 当前 node_modules 对这套系统安全吗?
|
|
17
|
+
npx dsh-plugin-gate-kit preflight
|
|
18
|
+
|
|
19
|
+
# 往仓库里铺 gate/preflight.mjs(幂等)
|
|
20
|
+
npx dsh-plugin-gate-kit init --dir .
|
|
21
|
+
|
|
22
|
+
# preflight + 看门禁组件现状
|
|
23
|
+
npx dsh-plugin-gate-kit check --dir .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 用法 / API
|
|
27
|
+
|
|
28
|
+
### CLI
|
|
29
|
+
|
|
30
|
+
| 命令 | 作用 |
|
|
31
|
+
|---|---|
|
|
32
|
+
| `preflight` | 检测属于其他 OS 的原生包。**默认子命令**——不带子命令直接跑就是它 |
|
|
33
|
+
| `init` | 写入 `gate/preflight.mjs`(已存在则跳过),并打印接入建议 |
|
|
34
|
+
| `check` | 跑 preflight **并**报告已有哪些门禁组件与 `package.json` scripts |
|
|
35
|
+
|
|
36
|
+
### 选项
|
|
37
|
+
|
|
38
|
+
| 选项 | 默认值 | 含义 |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `--dir <dir>` | `.` | 要检查 `node_modules` 的目录(`init` 时是 `gate/` 写入的位置)。必须是**独立的两个 token**:`--dir path`,不是 `--dir=path` |
|
|
41
|
+
| `--help`、`-h`、`help` | — | 打印用法并退出 `0` |
|
|
42
|
+
|
|
43
|
+
子命令必须写在**最前面**。`dsh-plugin-gate-kit --dir .` 会被当成 `未知子命令: --dir`(退出 `2`)。
|
|
44
|
+
|
|
45
|
+
### 退出码
|
|
46
|
+
|
|
47
|
+
| 码 | 含义 |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `0` | `preflight` 干净 / `init` 成功 / `check` 未发现平台包问题 |
|
|
50
|
+
| `1` | 发现平台原生包问题(`preflight` 与 `check` 都是) |
|
|
51
|
+
| `2` | 用法错误:未知子命令 |
|
|
52
|
+
|
|
53
|
+
### 编程接口
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { checkNative, assertNativePlatform } from 'dsh-plugin-gate-kit'
|
|
57
|
+
|
|
58
|
+
const problems = checkNative(process.cwd()) // [] 表示干净
|
|
59
|
+
assertNativePlatform(process.cwd()) // 有问题时抛错
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
| 导出 | 签名 | 返回 / 行为 |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| `checkNative` | `checkNative(dir)` | `{ type, pkg, detail }` 数组;空数组表示干净 |
|
|
65
|
+
| `assertNativePlatform` | `assertNativePlatform(dir = process.cwd())` | 有问题时抛 `Error`,逐条列出并带修复提示 |
|
|
66
|
+
|
|
67
|
+
`type` 取 `'wrong-os'`(别的平台的原生包)或 `'missing-current'`(装了 jscpd 但缺当前平台的原生包)。
|
|
68
|
+
|
|
69
|
+
## preflight 检测什么
|
|
70
|
+
|
|
71
|
+
读 `<dir>/node_modules`(含 scoped 包),用这条正则匹配目录名:
|
|
72
|
+
|
|
73
|
+
```text
|
|
74
|
+
(?:@scope/)?<name>-(windows|linux|darwin)-(x64|arm64|ia32)(-|$)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
| 规则 | 例子 | 记为 |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| 属于其他 OS 的原生包 | Windows 上有 `jscpd-linux-x64-gnu` | `wrong-os` |
|
|
80
|
+
| 装了 jscpd 但缺 `jscpd-<当前OS>-*` | 有 `jscpd`,没有 `jscpd-windows-*` | `missing-current` |
|
|
81
|
+
|
|
82
|
+
失败信息会直接给出修复方向:**在当前系统上 `npm ci` 重装**——不要改 `package.json` / `package-lock.json`。Windows 与 WSL 共用同一份 `node_modules` 正是错位来源,因为 npm 会按「谁在装」替换 `optionalDependencies` 里的原生包。
|
|
83
|
+
|
|
84
|
+
没有 `node_modules` 不算错误——返回空数组。
|
|
85
|
+
|
|
86
|
+
### `init` 产出什么
|
|
87
|
+
|
|
88
|
+
`init` 写入 `gate/preflight.mjs`,随后打印两种接入方式:
|
|
89
|
+
|
|
90
|
+
1. 在 dup 门禁脚本开头:`import { assertNativePlatform } from './preflight.mjs'; assertNativePlatform()`
|
|
91
|
+
2. 或在 `package.json` 里:`scripts: { "preflight": "node gate/preflight.mjs" }`
|
|
92
|
+
|
|
93
|
+
重复跑 `init` 会打印 `已存在:… (跳过)`,不改任何东西。
|
|
94
|
+
|
|
95
|
+
### `check` 输出
|
|
96
|
+
|
|
97
|
+
```text
|
|
98
|
+
gate check: .
|
|
99
|
+
✓ preflight: ok
|
|
100
|
+
gate 组件已存在: dup_check.mjs, lint_runner.mjs, format_check.mjs
|
|
101
|
+
package.json 相关 scripts: check, check:fast, lint, format, dup:check
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
它会找 `gate/dup_check.mjs`、`gate/lint_runner.mjs`、`gate/format_check.mjs`,以及名字匹配 `check|dup|lint|format` 的 `package.json` scripts。这只是一份**报告**,不是门禁:缺组件只列出来,不判失败。
|
|
105
|
+
|
|
106
|
+
## 范围——本包**不做**什么
|
|
107
|
+
|
|
108
|
+
本包刻意只做**最小**引导。完整四层门禁(四个 Git 钩子 + lint / 重复代码 / 格式三棘轮)**不在本包范围**。`check` 只报告缺哪些组件,既不安装也不强制。门禁方案请按所在项目自备,或把这个脚手架当作方案里的 preflight 环节。
|
|
109
|
+
|
|
110
|
+
## 注意事项
|
|
111
|
+
|
|
112
|
+
- `init` 铺出来的 `gate/preflight.mjs` 是一份**简化副本**,不是本包的实现。两处真实差异:
|
|
113
|
+
- 只实现了 `wrong-os` 规则(没有 `missing-current` / jscpd 检查);
|
|
114
|
+
- 那里的 `checkNative` 返回**字符串**数组,而本包导出返回的是 `{ type, pkg, detail }` 对象。
|
|
115
|
+
如果依赖返回结构或 jscpd 规则,请用本包的 `checkNative` / `assertNativePlatform`,不要用生成出来的文件。
|
|
116
|
+
- 生成脚本的 CLI 分支以**无参**方式调 `checkNative()`,所以 `node gate/preflight.mjs` 检查的是**当前工作目录**,不是你传给 `init` 的 `--dir`。请在仓库根跑,或在 import 时显式传目录。
|
|
117
|
+
- `--dir` 是按位置读的;`--dir=value` 会静默退回 `.`。
|
|
118
|
+
- `init` 永不覆盖已存在的 `gate/preflight.mjs`——想要新副本请先删掉旧文件。
|
|
119
|
+
- 要求 **Node >= 22.15.0**(全仓统一基线)。
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT
|
|
@@ -3,42 +3,20 @@
|
|
|
3
3
|
// 完整四层门禁(lint/dup/format/husky 棘轮)不在本包;本包只做"跨 OS 安全"这件刚需。
|
|
4
4
|
import { readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'
|
|
5
5
|
import { join } from 'node:path'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
6
7
|
import { checkNative } from '../src/preflight.mjs'
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const names = []
|
|
19
|
-
for (const n of readdirSync(nm)) {
|
|
20
|
-
if (n.startsWith('@')) { try { for (const s of readdirSync(join(nm, n))) names.push(n + '/' + s) } catch {} }
|
|
21
|
-
else names.push(n)
|
|
22
|
-
}
|
|
23
|
-
const problems = []
|
|
24
|
-
for (const n of names) {
|
|
25
|
-
const base = n.includes('/') ? n.split('/').pop() : n
|
|
26
|
-
const m = NATIVE_RE.exec(base)
|
|
27
|
-
if (m && m[1] !== OS) problems.push(n + ' 属于 ' + m[1] + ',当前 ' + OS)
|
|
28
|
-
}
|
|
29
|
-
return problems
|
|
30
|
-
}
|
|
31
|
-
export function assertNativePlatform(dir = process.cwd()) {
|
|
32
|
-
const p = checkNative(dir)
|
|
33
|
-
if (!p.length) return
|
|
34
|
-
throw new Error('平台原生包不匹配:\\n - ' + p.join('\\n - ') + '\\n请在本系统 npm ci 重装。')
|
|
35
|
-
}
|
|
36
|
-
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
37
|
-
const p = checkNative()
|
|
38
|
-
if (p.length) { console.error('[preflight] ' + p.join('; ')); process.exit(1) }
|
|
39
|
-
console.log('[preflight] ok(' + process.platform + ')')
|
|
9
|
+
/**
|
|
10
|
+
* `init` 生成的 preflight 直接**读取本包实现源码**,而不是手抄一份副本。
|
|
11
|
+
* 原因:手抄的模板曾漂移——只有 wrong-os、缺 missing-current、返回结构也不同(字符串数组 vs 对象数组),
|
|
12
|
+
* 导致"生成的脚本"和"本包实际行为"不一致。现在生成物 = 实现,永远不会漂移。
|
|
13
|
+
*/
|
|
14
|
+
function buildPreflightTemplate() {
|
|
15
|
+
const srcPath = fileURLToPath(new URL('../src/preflight.mjs', import.meta.url))
|
|
16
|
+
const body = readFileSync(srcPath, 'utf8')
|
|
17
|
+
return `// 平台原生包 preflight(由 dsh-plugin-gate-kit init 生成;内容直接来自 dsh-plugin-gate-kit 的实现,不会漂移)。
|
|
18
|
+
${body}`
|
|
40
19
|
}
|
|
41
|
-
`
|
|
42
20
|
|
|
43
21
|
const args = process.argv.slice(2)
|
|
44
22
|
const sub = args[0] || 'preflight'
|
|
@@ -51,6 +29,9 @@ if (args.includes('--help') || args.includes('-h') || sub === 'help') {
|
|
|
51
29
|
process.exit(0)
|
|
52
30
|
}
|
|
53
31
|
const opt = (n, d) => {
|
|
32
|
+
// 同时支持 `--dir X` 和 `--dir=X`:此前只认空格写法,等号写法会静默退回默认值。
|
|
33
|
+
const eq = args.find((a) => a.startsWith('--' + n + '='))
|
|
34
|
+
if (eq !== undefined) return eq.slice(n.length + 3)
|
|
54
35
|
const i = args.indexOf('--' + n)
|
|
55
36
|
return i >= 0 ? args[i + 1] : d
|
|
56
37
|
}
|
|
@@ -80,20 +61,30 @@ if (sub === 'preflight') {
|
|
|
80
61
|
if (existsSync(target)) {
|
|
81
62
|
console.log('已存在:' + target + '(跳过)')
|
|
82
63
|
} else {
|
|
83
|
-
writeFileSync(target,
|
|
64
|
+
writeFileSync(target, buildPreflightTemplate())
|
|
84
65
|
console.log('已写入:' + target)
|
|
85
66
|
}
|
|
86
67
|
console.log('\n接入建议:')
|
|
87
|
-
console.log(
|
|
68
|
+
console.log(
|
|
69
|
+
"1) 在 dup 门禁脚本开头 `import { assertNativePlatform } from './preflight.mjs'; assertNativePlatform()`",
|
|
70
|
+
)
|
|
88
71
|
console.log('2) 或在 package.json 加 scripts: { "preflight": "node gate/preflight.mjs" }')
|
|
89
72
|
process.exit(0)
|
|
90
73
|
} else if (sub === 'check') {
|
|
91
74
|
const problems = checkNative(dir)
|
|
92
75
|
console.log('gate check: ' + dir)
|
|
93
|
-
console.log(
|
|
76
|
+
console.log(
|
|
77
|
+
' ' +
|
|
78
|
+
(problems.length ? '✗ preflight: ' + problems.length + ' 个平台包问题' : '✓ preflight: ok'),
|
|
79
|
+
)
|
|
94
80
|
const pieces = ['dup_check.mjs', 'lint_runner.mjs', 'format_check.mjs']
|
|
95
81
|
const present = pieces.filter((f) => existsSync(join(dir, 'gate', f)))
|
|
96
|
-
console.log(
|
|
82
|
+
console.log(
|
|
83
|
+
' gate 组件已存在: ' +
|
|
84
|
+
(present.length
|
|
85
|
+
? present.join(', ')
|
|
86
|
+
: '(无,可用 dsh-plugin-gate-kit init 起步,或按所在项目的门禁方案铺设全套)'),
|
|
87
|
+
)
|
|
97
88
|
const pkg = readJson(join(dir, 'package.json')) || {}
|
|
98
89
|
const scripts = Object.keys(pkg.scripts || {}).filter((s) => /check|dup|lint|format/.test(s))
|
|
99
90
|
console.log(' package.json 相关 scripts: ' + (scripts.length ? scripts.join(', ') : '(无)'))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-gate-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"bin/",
|
|
16
16
|
"src/",
|
|
17
|
-
"README.md"
|
|
17
|
+
"README.md",
|
|
18
|
+
"README.zh-CN.md"
|
|
18
19
|
],
|
|
19
20
|
"repository": {
|
|
20
21
|
"type": "git",
|
package/src/preflight.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// (如 jscpd-windows-* ↔ jscpd-linux-*),避免跨系统共用同一 node_modules 导致的迷之错误。
|
|
3
3
|
import { readdirSync, existsSync } from 'node:fs'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
|
+
import { pathToFileURL } from 'node:url'
|
|
5
6
|
|
|
6
7
|
const OSNAME = { win32: 'windows', darwin: 'darwin', linux: 'linux' }
|
|
7
8
|
const NATIVE_RE = /^(?:@[^/]+\/)?[^/]+-(windows|linux|darwin)-(x64|arm64|ia32)(?:-|$)/
|
|
@@ -46,3 +47,17 @@ export function assertNativePlatform(dir = process.cwd()) {
|
|
|
46
47
|
const msg = problems.map((p) => ` - ${p.pkg}: ${p.detail}`).join('\n')
|
|
47
48
|
throw new Error(`平台原生包不匹配(node_modules 被另一个 OS 装过):\n${msg}\n修复:在【当前系统】npm ci 重装;不要改 package.json。`)
|
|
48
49
|
}
|
|
50
|
+
|
|
51
|
+
// CLI 入口:`node gate/preflight.mjs [dir]`。检查目录取第一个参数,默认当前目录
|
|
52
|
+
// (此前生成的脚本无参调用,总是查 cwd,导致 `init --dir X` 生成后在不同目录跑结果不一致)。
|
|
53
|
+
if (process.argv[1] !== undefined && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
54
|
+
const target = process.argv[2] || process.cwd()
|
|
55
|
+
const problems = checkNative(target)
|
|
56
|
+
if (problems.length) {
|
|
57
|
+
console.error('[preflight] 平台原生包不匹配(' + target + '):')
|
|
58
|
+
for (const p of problems) console.error(' - ' + p.pkg + ': ' + p.detail)
|
|
59
|
+
console.error(' 修复:在【当前系统】npm ci 重装;不要改 package.json / package-lock。')
|
|
60
|
+
process.exit(1)
|
|
61
|
+
}
|
|
62
|
+
console.log('[preflight] ok(' + process.platform + ')')
|
|
63
|
+
}
|