dsh-plugin-gate-kit 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +11 -2
- package/bin/dsh-plugin-gate-kit.mjs +104 -0
- package/package.json +34 -4
- package/src/index.mjs +1 -0
- package/src/preflight.mjs +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dsh-plugin-e2e contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-plugin-gate-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
DSH 插件仓库的**最小门禁引导**:跨 OS 原生包 preflight + `init` 铺 `gate/preflight.mjs`。
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
dsh-plugin-gate-kit preflight --dir <pluginDir> # 检查 node_modules 是否被另一个 OS 装过(jscpd 等原生包)
|
|
7
|
+
dsh-plugin-gate-kit init --dir <pluginDir> # 写入 gate/preflight.mjs(幂等)
|
|
8
|
+
dsh-plugin-gate-kit check --dir <pluginDir> # preflight + 报告 gate 组件/脚本现状
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
- **preflight**:检测 `*-windows-*`/`*-linux-*`/`*-darwin-*` 平台原生包与当前 OS 不匹配(跨系统共用 node_modules 的经典坑),以及 jscpd 缺当前平台原包。有问题退出码 1,并提示"在当前系统 npm ci"。
|
|
12
|
+
- 完整**四层门禁(四钩子 + 三棘轮:lint/dup/format/husky)**不在本包,按所在项目的门禁方案铺设;本包只解决"跨 OS 安全"这件刚需,并给 `init` 起步。
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// dsh-plugin-gate-kit:最小门禁引导(跨 OS 原生包 preflight + `init` 铺 gate/preflight.mjs)。
|
|
3
|
+
// 完整四层门禁(lint/dup/format/husky 棘轮)不在本包;本包只做"跨 OS 安全"这件刚需。
|
|
4
|
+
import { readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { checkNative } from '../src/preflight.mjs'
|
|
7
|
+
|
|
8
|
+
const PREFLIGHT_TEMPLATE = `// 平台原生包 preflight(由 dsh-plugin-gate-kit init 生成)。
|
|
9
|
+
import { readdirSync, existsSync } from 'node:fs'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
import { pathToFileURL } from 'node:url'
|
|
12
|
+
const OSNAME = { win32: 'windows', darwin: 'darwin', linux: 'linux' }
|
|
13
|
+
const NATIVE_RE = /^(?:@[^/]+\\/)?[^/]+-(windows|linux|darwin)-(x64|arm64|ia32)(?:-|$)/
|
|
14
|
+
const OS = OSNAME[process.platform] || process.platform
|
|
15
|
+
export function checkNative(dir = process.cwd()) {
|
|
16
|
+
const nm = join(dir, 'node_modules')
|
|
17
|
+
if (!existsSync(nm)) return []
|
|
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 + ')')
|
|
40
|
+
}
|
|
41
|
+
`
|
|
42
|
+
|
|
43
|
+
const args = process.argv.slice(2)
|
|
44
|
+
const sub = args[0] || 'preflight'
|
|
45
|
+
const USAGE = `用法: dsh-plugin-gate-kit <preflight|init|check> [--dir <dir>]
|
|
46
|
+
preflight 检测跨 OS 原生包错位(默认)
|
|
47
|
+
init 写入 gate/preflight.mjs
|
|
48
|
+
check 报告 preflight 与 gate 现状`
|
|
49
|
+
if (args.includes('--help') || args.includes('-h') || sub === 'help') {
|
|
50
|
+
console.log(USAGE)
|
|
51
|
+
process.exit(0)
|
|
52
|
+
}
|
|
53
|
+
const opt = (n, d) => {
|
|
54
|
+
const i = args.indexOf('--' + n)
|
|
55
|
+
return i >= 0 ? args[i + 1] : d
|
|
56
|
+
}
|
|
57
|
+
const dir = opt('dir', '.')
|
|
58
|
+
const readJson = (p) => {
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(readFileSync(p, 'utf8'))
|
|
61
|
+
} catch {
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (sub === 'preflight') {
|
|
67
|
+
const problems = checkNative(dir)
|
|
68
|
+
if (!problems.length) {
|
|
69
|
+
console.log('[preflight] ok(' + process.platform + ')')
|
|
70
|
+
process.exit(0)
|
|
71
|
+
}
|
|
72
|
+
console.log('[preflight] 平台原生包不匹配:')
|
|
73
|
+
problems.forEach((p) => console.log(' - ' + p.pkg + ': ' + p.detail))
|
|
74
|
+
console.log('修复:在【当前系统】npm ci 重装;不要改 package.json/lock。')
|
|
75
|
+
process.exit(1)
|
|
76
|
+
} else if (sub === 'init') {
|
|
77
|
+
const gateDir = join(dir, 'gate')
|
|
78
|
+
mkdirSync(gateDir, { recursive: true })
|
|
79
|
+
const target = join(gateDir, 'preflight.mjs')
|
|
80
|
+
if (existsSync(target)) {
|
|
81
|
+
console.log('已存在:' + target + '(跳过)')
|
|
82
|
+
} else {
|
|
83
|
+
writeFileSync(target, PREFLIGHT_TEMPLATE)
|
|
84
|
+
console.log('已写入:' + target)
|
|
85
|
+
}
|
|
86
|
+
console.log('\n接入建议:')
|
|
87
|
+
console.log("1) 在 dup 门禁脚本开头 `import { assertNativePlatform } from './preflight.mjs'; assertNativePlatform()`")
|
|
88
|
+
console.log('2) 或在 package.json 加 scripts: { "preflight": "node gate/preflight.mjs" }')
|
|
89
|
+
process.exit(0)
|
|
90
|
+
} else if (sub === 'check') {
|
|
91
|
+
const problems = checkNative(dir)
|
|
92
|
+
console.log('gate check: ' + dir)
|
|
93
|
+
console.log(' ' + (problems.length ? '✗ preflight: ' + problems.length + ' 个平台包问题' : '✓ preflight: ok'))
|
|
94
|
+
const pieces = ['dup_check.mjs', 'lint_runner.mjs', 'format_check.mjs']
|
|
95
|
+
const present = pieces.filter((f) => existsSync(join(dir, 'gate', f)))
|
|
96
|
+
console.log(' gate 组件已存在: ' + (present.length ? present.join(', ') : '(无,可用 dsh-plugin-gate-kit init 起步,或按所在项目的门禁方案铺设全套)'))
|
|
97
|
+
const pkg = readJson(join(dir, 'package.json')) || {}
|
|
98
|
+
const scripts = Object.keys(pkg.scripts || {}).filter((s) => /check|dup|lint|format/.test(s))
|
|
99
|
+
console.log(' package.json 相关 scripts: ' + (scripts.length ? scripts.join(', ') : '(无)'))
|
|
100
|
+
process.exit(problems.length ? 1 : 0)
|
|
101
|
+
} else {
|
|
102
|
+
console.error('未知子命令: ' + sub + '(preflight | init | check)')
|
|
103
|
+
process.exit(2)
|
|
104
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-gate-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"description": "Minimum gate bootstrap for DSH plugin repos: cross-OS native-package preflight (jscpd etc. platform mismatch) plus `init` to scaffold a gate/ preflight into a repo. Full four-layer gate (four hooks + three ratchets) is out of this package's scope.",
|
|
8
|
+
"bin": {
|
|
9
|
+
"dsh-plugin-gate-kit": "bin/dsh-plugin-gate-kit.mjs"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./src/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"bin/",
|
|
16
|
+
"src/",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/metal-huang/dsh-devtools.git",
|
|
22
|
+
"directory": "packages/dsh-plugin-gate-kit"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"dsh": ">=0.2.2-rc.5",
|
|
30
|
+
"node": ">=22.15.0"
|
|
31
|
+
},
|
|
32
|
+
"dshTestedOn": [
|
|
33
|
+
"0.2.2-rc.5",
|
|
34
|
+
"0.2.3-alpha.2"
|
|
35
|
+
]
|
|
36
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { checkNative, assertNativePlatform } from './preflight.mjs'
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// 平台原生包 preflight:检测 node_modules 里被"另一个 OS"装过的原生可选包
|
|
2
|
+
// (如 jscpd-windows-* ↔ jscpd-linux-*),避免跨系统共用同一 node_modules 导致的迷之错误。
|
|
3
|
+
import { readdirSync, existsSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
const OSNAME = { win32: 'windows', darwin: 'darwin', linux: 'linux' }
|
|
7
|
+
const NATIVE_RE = /^(?:@[^/]+\/)?[^/]+-(windows|linux|darwin)-(x64|arm64|ia32)(?:-|$)/
|
|
8
|
+
|
|
9
|
+
function listEntries(nm) {
|
|
10
|
+
const out = []
|
|
11
|
+
for (const name of readdirSync(nm)) {
|
|
12
|
+
if (name.startsWith('@')) {
|
|
13
|
+
try {
|
|
14
|
+
for (const s of readdirSync(join(nm, name))) out.push(name + '/' + s)
|
|
15
|
+
} catch {}
|
|
16
|
+
} else out.push(name)
|
|
17
|
+
}
|
|
18
|
+
return out
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 返回问题数组(空=无问题)。 */
|
|
22
|
+
export function checkNative(dir) {
|
|
23
|
+
const nm = join(dir, 'node_modules')
|
|
24
|
+
if (!existsSync(nm)) return []
|
|
25
|
+
const cur = OSNAME[process.platform] || process.platform
|
|
26
|
+
const names = listEntries(nm)
|
|
27
|
+
const problems = []
|
|
28
|
+
for (const name of names) {
|
|
29
|
+
const base = name.includes('/') ? name.split('/').pop() : name
|
|
30
|
+
const m = NATIVE_RE.exec(base)
|
|
31
|
+
if (m && m[1] !== cur) {
|
|
32
|
+
problems.push({ type: 'wrong-os', pkg: name, detail: `${base} 属于 ${m[1]},当前 OS=${cur}` })
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// jscpd 特例:已安装但缺当前平台原包
|
|
36
|
+
if (names.includes('jscpd') && !names.some((n) => n.startsWith('jscpd-' + cur + '-'))) {
|
|
37
|
+
problems.push({ type: 'missing-current', pkg: 'jscpd', detail: `缺 jscpd-${cur}-*(当前平台原生包缺失)` })
|
|
38
|
+
}
|
|
39
|
+
return problems
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 有问题则抛错;供 gate 脚本调用。 */
|
|
43
|
+
export function assertNativePlatform(dir = process.cwd()) {
|
|
44
|
+
const problems = checkNative(dir)
|
|
45
|
+
if (!problems.length) return
|
|
46
|
+
const msg = problems.map((p) => ` - ${p.pkg}: ${p.detail}`).join('\n')
|
|
47
|
+
throw new Error(`平台原生包不匹配(node_modules 被另一个 OS 装过):\n${msg}\n修复:在【当前系统】npm ci 重装;不要改 package.json。`)
|
|
48
|
+
}
|