@skillre/dsh-plugin-work-dashboard 0.12.0 → 0.13.0

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.
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env node
2
+ import { lstatSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs'
3
+ import { isAbsolute, join, relative, resolve } from 'node:path'
4
+ import { parseArgs } from 'node:util'
5
+ import YAML from 'yaml'
6
+ import { LEDGER_VERSION, MAX_REPORT_BYTES, readReport } from '../../lib/report-contract.js'
7
+ import { prepareReport } from '../../lib/report-import.js'
8
+
9
+ function readRegular(path, limit) {
10
+ const stat = lstatSync(path, { throwIfNoEntry: false })
11
+ if (!stat) return undefined
12
+ if (!stat.isFile() || stat.size > limit) throw new Error(`需要大小合规的常规文件:${path}`)
13
+ return readFileSync(path, 'utf8')
14
+ }
15
+
16
+ try {
17
+ const { values } = parseArgs({ options: { input: { type: 'string' }, ledger: { type: 'string' }, out: { type: 'string' } } })
18
+ if (!values.input || !values.ledger) throw new Error('用法:prepare-return.mjs --input <回执.html> --ledger <独立台账检出> [--out <新的候选目录>]')
19
+ const root = realpathSync(values.ledger)
20
+ const enums = YAML.parse(readRegular(join(root, 'schemas', 'enums.yaml'), MAX_REPORT_BYTES) ?? '')
21
+ if (enums?.schema_version !== LEDGER_VERSION) throw new Error('该台账尚未启用 3.2,未生成写入候选。')
22
+ const source = readRegular(values.input, MAX_REPORT_BYTES)
23
+ if (source === undefined) throw new Error('找不到回执文件。')
24
+ const report = readReport(source)
25
+ const projects = realpathSync(join(root, 'projects'))
26
+ if (projects !== join(root, 'projects')) throw new Error('台账 projects 不能是符号链接。')
27
+ const existing = new Map()
28
+ for (const { poc_id } of report.changes) {
29
+ const data = readRegular(join(projects, `${poc_id}.yaml`), MAX_REPORT_BYTES)
30
+ if (data !== undefined) existing.set(poc_id, data)
31
+ }
32
+ const candidates = prepareReport(report, existing)
33
+ if (values.out) {
34
+ // Resolve the parent before deciding scope, so a symlink cannot write a
35
+ // candidate into the checkout. Deliberately require a NEW directory.
36
+ const abs = resolve(values.out)
37
+ const parent = realpathSync(resolve(abs, '..'))
38
+ const out = join(parent, relative(resolve(abs, '..'), abs))
39
+ const rel = relative(root, out)
40
+ if (rel === '' || (!rel.startsWith('../') && rel !== '..' && !isAbsolute(rel))) throw new Error('候选目录必须在台账检出之外。')
41
+ mkdirSync(out, { mode: 0o700 })
42
+ for (const item of candidates) if (item.yaml !== undefined) writeFileSync(join(out, `${item.pocId}.yaml`), item.yaml, { flag: 'wx', mode: 0o600 })
43
+ }
44
+ process.stdout.write(JSON.stringify(candidates.map(({ yaml, ...item }) => values.out ? item : { ...item, yaml }), null, 2) + '\n')
45
+ } catch (error) {
46
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`)
47
+ process.exitCode = 1
48
+ }