-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (34 loc) · 1.09 KB
/
index.js
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
const debug = require('debug')('main');
const {
existsSync, mkdirSync, readFileSync, writeFileSync,
} = require('fs');
const removeDuplicates = require('./lib/removeDuplicates');
const config = require('./config');
// Get input file from CLI
const [inputFile = config.inputFile] = process.argv.slice(2);
if (!inputFile) {
console.error('Usage: npm start <path-to-file>');
process.exit(1);
}
try {
debug('start');
// Attempt to read / parse application schema
const inputStr = readFileSync(inputFile, 'utf-8');
const schema = JSON.parse(inputStr);
debug('schema:', Buffer.byteLength(inputStr));
// Remove duplicates
const parsedSchema = removeDuplicates(schema);
const outputStr = JSON.stringify(parsedSchema, 0, 2);
debug('parsedSchema:', Buffer.byteLength(outputStr));
// Ensure output directory exists
if (!existsSync(config.outputDir)) {
mkdirSync(config.outputDir);
}
// Save schema to file
writeFileSync(config.outputFile, outputStr);
} catch (err) {
console.error(`ERROR: unable to parse schema\n- ${err.message}`);
process.exit(1);
} finally {
debug('end');
}