File size: 1,869 Bytes
1e92f2d |
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 |
const os = require('os')
const path = require('path')
const fs = require('fs/promises')
const errorsDir = path.join(__dirname, '.errors')
// This script checks for new error codes in .errors directory and consolidates them into errors.json.
// It will fail if new error codes are found, after consolidating them, to ensure error codes are
// properly reviewed and committed.
async function main() {
// Check if .errors directory exists
try {
await fs.access(errorsDir)
} catch {
process.exit(0)
}
let existingErrors = {}
// Load existing errors.json if it exists
try {
existingErrors = JSON.parse(
await fs.readFile(path.join(__dirname, 'errors.json'), 'utf8')
)
} catch {
// Start fresh if errors.json doesn't exist
}
// Calculate next error code
const nextInitialCode =
Object.keys(existingErrors).length > 0
? Math.max(...Object.keys(existingErrors).map(Number)) + 1
: 1
// Process new error files
const errorFiles = [...(await fs.readdir(errorsDir))].map((file) =>
path.join(errorsDir, file)
)
let newErrorCount = 0
for (const file of errorFiles) {
const { errorMessage } = JSON.parse(await fs.readFile(file, 'utf8'))
// Check if message already exists
const existingCode = Object.entries(existingErrors).find(
([_, msg]) => msg === errorMessage
)?.[0]
if (!existingCode) {
// Only add if message is new
const code = nextInitialCode + newErrorCount
existingErrors[code] = errorMessage
newErrorCount++
}
}
// Write updated errors and cleanup
await fs.writeFile(
path.join(__dirname, 'errors.json'),
JSON.stringify(existingErrors, null, 2) +
// Formatters would add these anyway
os.EOL
)
await fs.rm(errorsDir, { recursive: true, force: true })
process.exit(1)
}
main().catch(console.error)
|