/
make-validation-function
executable file
·51 lines (38 loc) · 1.61 KB
/
make-validation-function
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
#!/usr/bin/env node
const commander = require('./lib/commander/index');
const validationFunctionLoader = require('./src/loading/validation-function-loader');
const validationFunctionWriter = require('./src/saving/validation-function-writer');
const errorStatus = 1;
const { version } = require('./package');
// Parse the commandline arguments
commander.arguments('<document_definitions_file> <output_file>')
.version(version, '-v, --version')
.description('A utility for generating document validation functions for Apache CouchDB.')
.option('-j, --json-string', 'enclose the validation function contents in a JSON-compatible string');
commander.on('--help', () => {
// Add some extra information after the main body of the usage/help text
console.info(`
For example: ${commander.name()} /path/to/my-doc-definitions.js /path/to/my-new-validation-function.js
See the README for more information.`);
});
commander.parse(process.argv);
if (commander.args.length !== 2) {
commander.outputHelp();
process.exit(errorStatus);
}
const docDefnFilename = commander.args[0];
const outputFilename = commander.args[1];
let validationFuncString;
try {
validationFuncString = validationFunctionLoader.load(docDefnFilename);
} catch (ex) {
process.exit(errorStatus);
}
try {
const formatOptions = { jsonString: commander.jsonString };
validationFunctionWriter.save(outputFilename, validationFuncString, formatOptions);
} catch (ex) {
console.error(`ERROR: Unable to write the validation function to the output file: ${ex}`);
process.exit(errorStatus);
}
console.info(`Validation function written to ${outputFilename}`);