-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvalidate
executable file
·80 lines (69 loc) · 2.51 KB
/
validate
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
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
let program = require( 'commander' );
const pkg = require( './package' );
const fs = require( 'fs' );
const validator = require( './src/validator' );
const { closeBrowser } = require( './src/headless-browser' );
const _getFileContents = filePath => {
return new Promise( ( resolve, reject ) => {
fs.readFile( filePath, 'utf8', ( err, xform ) => {
if ( err ) {
if ( err.code === 'ENOENT' ) {
err = `File: ${filePath} does not exist.`;
}
reject( err );
} else {
resolve( xform );
}
} );
} );
};
const _output = ( issues = [], error = false ) => {
if ( issues.length ) {
console[ error ? 'error' : 'log' ]( `\n\n${issues.join( '\n\n' )}` );
}
};
program
.usage( '[options] <file>' )
.version( pkg.version )
.option( '-m, --me', 'get personal validation' )
.option( '-d, --debug', 'output raw errors' )
.option( '-oc, --openclinica', 'run additional custom OpenClinica validation rules and use a custom XPath evaluator' )
.parse( process.argv );
const xformFile = program.args[ 0 ];
const options = program.opts();
if ( options.me ) {
console.log( 'You are correct.\nYour feelings matter.\nYou are wonderful and the world is better with you in it.' );
process.exit( 0 );
} else if ( xformFile ) {
console.log( `Enketo validating ${xformFile}` );
let exceptions = null;
let result = null;
_getFileContents( xformFile )
.then( xformStr => validator.validate( xformStr, options ) )
.catch( ( errors = [] ) => {
exceptions = Array.isArray( errors ) ? errors : [ errors ];
} )
.then( ( res = {} ) => {
result = res;
return closeBrowser( );
} )
.finally( () => {
if ( exceptions ){
_output( exceptions, true );
process.exit( 1 );
}
_output( result.warnings );
_output( result.errors, true );
if ( result.errors.length ) {
_output( [ options.openclinica ? '' : 'Result: Invalid\n\n' ], true );
process.exit( 1 );
} else {
_output( [ options.openclinica ? '' : '>> XForm is valid! See above for any warnings.\n\n' ] );
process.exit( 0 );
}
} );
} else {
console.error( 'Nothing to do. No XForm File provided. Use --help flag to see manual.' );
process.exit( 1 );
}