|
|
const os = require('os') |
|
|
const path = require('path') |
|
|
const fs = require('fs/promises') |
|
|
|
|
|
const errorsDir = path.join(__dirname, '.errors') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function main() { |
|
|
|
|
|
try { |
|
|
await fs.access(errorsDir) |
|
|
} catch { |
|
|
process.exit(0) |
|
|
} |
|
|
|
|
|
let existingErrors = {} |
|
|
|
|
|
|
|
|
try { |
|
|
existingErrors = JSON.parse( |
|
|
await fs.readFile(path.join(__dirname, 'errors.json'), 'utf8') |
|
|
) |
|
|
} catch { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const nextInitialCode = |
|
|
Object.keys(existingErrors).length > 0 |
|
|
? Math.max(...Object.keys(existingErrors).map(Number)) + 1 |
|
|
: 1 |
|
|
|
|
|
|
|
|
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')) |
|
|
|
|
|
|
|
|
const existingCode = Object.entries(existingErrors).find( |
|
|
([_, msg]) => msg === errorMessage |
|
|
)?.[0] |
|
|
|
|
|
if (!existingCode) { |
|
|
|
|
|
const code = nextInitialCode + newErrorCount |
|
|
existingErrors[code] = errorMessage |
|
|
newErrorCount++ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
await fs.writeFile( |
|
|
path.join(__dirname, 'errors.json'), |
|
|
JSON.stringify(existingErrors, null, 2) + |
|
|
|
|
|
os.EOL |
|
|
) |
|
|
|
|
|
await fs.rm(errorsDir, { recursive: true, force: true }) |
|
|
process.exit(1) |
|
|
} |
|
|
|
|
|
main().catch(console.error) |
|
|
|