main
js 74 lines 1.82 KB
Raw
1 'use strict';
2
3 const fs = require('fs');
4 const path = require('path');
5 const {execSync} = require('child_process');
6
7 async function main() {
8 const originalJSON = JSON.parse(
9 fs.readFileSync(path.resolve(__dirname, '../error-codes/codes.json'))
10 );
11 const existingMessages = new Set();
12 const codes = Object.keys(originalJSON);
13 let nextCode = 0;
14 for (let i = 0; i < codes.length; i++) {
15 const codeStr = codes[i];
16 const message = originalJSON[codeStr];
17 const code = parseInt(codeStr, 10);
18 existingMessages.add(message);
19 if (code >= nextCode) {
20 nextCode = code + 1;
21 }
22 }
23
24 console.log('Searching `build` directory for unminified errors...\n');
25
26 let out;
27 try {
28 out = execSync(
29 "git --no-pager grep -n --untracked --no-exclude-standard '/*! <expected-error-format>' -- build"
30 ).toString();
31 } catch (e) {
32 if (e.status === 1 && e.stdout.toString() === '') {
33 // No unminified errors found.
34 return;
35 }
36 throw e;
37 }
38
39 let newJSON = null;
40 const regex = /\<expected-error-format\>"(.+?)"\<\/expected-error-format\>/g;
41 do {
42 const match = regex.exec(out);
43 if (match === null) {
44 break;
45 } else {
46 const message = match[1].trim();
47 if (existingMessages.has(message)) {
48 // This probably means you ran the script twice.
49 continue;
50 }
51 existingMessages.add(message);
52
53 // Add to json map
54 if (newJSON === null) {
55 newJSON = Object.assign({}, originalJSON);
56 }
57 console.log(`"${nextCode}": "${message}"`);
58 newJSON[nextCode] = message;
59 nextCode += 1;
60 }
61 } while (true);
62
63 if (newJSON) {
64 fs.writeFileSync(
65 path.resolve(__dirname, '../error-codes/codes.json'),
66 JSON.stringify(newJSON, null, 2)
67 );
68 }
69 }
70
71 main().catch(error => {
72 console.error(error);
73 process.exit(1);
74 });