| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | import { readFileSync, writeFileSync } from 'node:fs' |
| |
|
| | function main() { |
| | const args = process.argv.slice(2) |
| |
|
| | if (args.length < 3) { |
| | console.error( |
| | 'Usage: node merge-errors-json.mjs <current> <base> <other> [<marker-size>]' |
| | ) |
| | process.exit(1) |
| | } |
| |
|
| | const [currentPath, basePath, otherPath] = args |
| |
|
| | try { |
| | const base = readJsonSync(basePath) |
| | const current = readJsonSync(currentPath) |
| | const other = readJsonSync(otherPath) |
| |
|
| | const merged = mergeErrors(base, current, other) |
| |
|
| | |
| | writeJsonSync(currentPath, merged) |
| |
|
| | const addedCount = Object.keys(merged).length - Object.keys(current).length |
| | console.error( |
| | `merge-errors-json: added ${addedCount === 1 ? '1 new message' : `${addedCount} new messages`} to errors.json` |
| | ) |
| | process.exit(0) |
| | } catch (error) { |
| | console.error('merge-errors-json: merge failed:', error.message) |
| | console.error() |
| | console.error( |
| | [ |
| | 'if this error persists, you can disable the merge driver by running', |
| | '', |
| | ' scripts/merge-errors-json/uninstall', |
| | '', |
| | 'or by manually removing the `[merge "errors-json"]` section from your .git/config.', |
| | ].join('\n') |
| | ) |
| | process.exit(1) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function mergeErrors(base, current, other) { |
| | |
| | const result = { ...current } |
| |
|
| | |
| | const existingMessages = new Set(Object.values(result)) |
| |
|
| | let nextKey = Object.keys(result).length + 1 |
| |
|
| | for (const message of getNewMessages(base, other)) { |
| | if (existingMessages.has(message)) { |
| | continue |
| | } |
| |
|
| | const key = nextKey++ |
| | result[key] = message |
| | existingMessages.add(message) |
| | } |
| |
|
| | return result |
| | } |
| |
|
| | function getNewMessages( |
| | prev, |
| | current |
| | ) { |
| | const existing = new Set(Object.values(prev)) |
| | return Object.values(current).filter((msg) => !existing.has(msg)) |
| | } |
| |
|
| | function readJsonSync( filePath) { |
| | const content = readFileSync(filePath, 'utf8') |
| | return JSON.parse(content) |
| | } |
| |
|
| | function writeJsonSync( |
| | filePath, |
| | value |
| | ) { |
| | const content = JSON.stringify(value, null, 2) + '\n' |
| | writeFileSync(filePath, content, 'utf8') |
| | } |
| |
|
| | main() |
| |
|