remap-schema-brand-ids.mjs
3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
/**
* @file 将 V2.1.1 daily fixture 的 brandId/filterBrandId 映射为生产环境 ID,输出 .prod.js / .prod.json。
* 结构与其余字段保持不变,供配置中心生产粘贴。
*
* 用法:
* node packageDynamicForm/scripts/remap-schema-brand-ids.mjs --sheet staff
* node packageDynamicForm/scripts/remap-schema-brand-ids.mjs --sheet store
*/
import { readFileSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import {
buildBrandIdRemap,
buildFileHeaderComment,
remapSchemaBrandIds
} from '../fixtures/brand-id-env-maps.mjs'
const __dirname = dirname(fileURLToPath(import.meta.url))
const PKG_ROOT = join(__dirname, '..')
const FIXTURES = join(PKG_ROOT, 'fixtures')
const SHEET_CONFIG = {
staff: {
dailyJson: 'schema-staff-daily-v2.1.1.json',
dailyJs: 'schema-staff-daily-v2.1.1.js',
prodJson: 'schema-staff-daily-v2.1.1.prod.json',
prodJs: 'schema-staff-daily-v2.1.1.prod.js'
},
store: {
dailyJson: 'schema-store-daily-v2.1.1.json',
dailyJs: 'schema-store-daily-v2.1.1.js',
prodJson: 'schema-store-daily-v2.1.1.prod.json',
prodJs: 'schema-store-daily-v2.1.1.prod.js'
}
}
/**
* 在 JS fixture 文本中替换 fileHeader 与数值 brandId(daily → production)。
* @param {string} jsText
* @param {Record<number, number>} remap
* @returns {string}
*/
function remapJsFixtureText(jsText, remap) {
const prodHeader = buildFileHeaderComment('production')
const headerEnd = jsText.indexOf('/**')
if (headerEnd === -1) {
throw new Error('JS fixture missing jsDoc block')
}
let out = prodHeader + '\n' + jsText.slice(headerEnd)
// 按 ID 降序替换,避免子串误伤(当前 ID 无重叠,仍做防御)
const entries = Object.entries(remap).sort((a, b) => Number(b[0]) - Number(a[0]))
for (const [from, to] of entries) {
const pattern = new RegExp(`\\b${from}\\b`, 'g')
out = out.replace(pattern, String(to))
}
return out
}
/**
* @param {'staff' | 'store'} sheet
*/
function remapSheet(sheet) {
const cfg = SHEET_CONFIG[sheet]
if (!cfg) {
throw new Error(`Unknown sheet: ${sheet}`)
}
const dailyJsonPath = join(FIXTURES, cfg.dailyJson)
const dailyJsPath = join(FIXTURES, cfg.dailyJs)
const prodJsonPath = join(FIXTURES, cfg.prodJson)
const prodJsPath = join(FIXTURES, cfg.prodJs)
const dailyPayload = JSON.parse(readFileSync(dailyJsonPath, 'utf8'))
const prodPayload = structuredClone(dailyPayload)
const remap = buildBrandIdRemap('daily', 'production')
remapSchemaBrandIds(prodPayload.list, remap)
writeFileSync(prodJsonPath, `${JSON.stringify(prodPayload, null, 2)}\n`, 'utf8')
const dailyJs = readFileSync(dailyJsPath, 'utf8')
const prodJs = remapJsFixtureText(dailyJs, remap)
writeFileSync(prodJsPath, prodJs, 'utf8')
console.log(`written ${prodJsonPath}`)
console.log(`written ${prodJsPath}`)
}
function main() {
const sheetArg = process.argv.find((_, i, arr) => arr[i - 1] === '--sheet')
const sheet = sheetArg || process.argv[2]
if (!sheet || !SHEET_CONFIG[sheet]) {
console.error('Usage: node remap-schema-brand-ids.mjs --sheet staff|store')
process.exit(1)
}
remapSheet(sheet)
}
main()