-
Notifications
You must be signed in to change notification settings - Fork 0
/
jyt
executable file
·111 lines (98 loc) · 3.82 KB
/
jyt
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env node
'use strict';
var constants = require('./lib/constants.js');
var cli = require('cli');
var path = require('path');
var Transformer = require('./lib/transformer.js');
var transformer = new Transformer(cli);
///////////////////////////////////////////////////////////////////////////////
// CLI INIT
///////////////////////////////////////////////////////////////////////////////
/**
* How to use the CLI.
*
* @type {string}
* @private
*/
var usage = path.basename(__filename) + ' INPUT-FILE [OUTPUT-FILE] [OPTIONS]';
/**
* The path to package.json.
*
* @type {string}
* @private
*/
var packagePath = __dirname + '/package.json';
/**
* The options description for parsing the command line input, must be an object with opts defined like:
* ```
* long_tag: [short_tag, description, value_type, default_value];
* ```
* @type {{origin: *[], target: *[], src: string[], dest: *[], indent: *[], force: string[], imports: string, exports: string}}
* @private
*/
var options = {
origin: [ 'o', 'The origin type of INPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ]', 'string', constants.DEFAULT_OPTIONS.origin ],
target: [ 't', 'The target type of OUTPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ]', 'string', constants.DEFAULT_OPTIONS.target ],
indent: [ 'i', 'The indention for pretty-print: 1 - 8', 'int', constants.DEFAULT_INDENT ],
force: [ 'f', 'Force overwriting of existing output files on write phase: when files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml' ],
imports: [ 'm', 'Define a \'module.exports[.identifier] = \' identifier (to read from JS _source_ file only, must be a valid JS identifier!)', 'string', constants.DEFAULT_OPTIONS.imports ],
exports: [ 'x', 'Define a \'module.exports[.identifier] = \' identifier (for usage in JS destination file only, must be a valid JS identifier!)', 'string', constants.DEFAULT_OPTIONS.exports ]
};
/**
* Prints the error to console and exit with 1.
*
* @param {string|Error} err - The error to print.
* @private
*/
function error(err) {
cli.error('////////////////////////////////////////////////////////////////////////////////');
cli.error(err);
if (err.stack) {
cli.debug(err.stack);
}
cli.error('////////////////////////////////////////////////////////////////////////////////');
cli.getUsage(1);
}
/**
* The main entry callback. When calling `cli.main()` this receives the `options`
* given on CLI, then does the transformation with these options and finally, it
* prints the result to the CLI.
*
* @param {array} args - The first mandatory argument is the input file
* (`args[0]`), the second (optional) argument is the
* output file (`args[0]`).
* @param {object} options - The options set on CLI.
* @private
*/
function main(args, options) {
// read file args and set to options
if (args.length > 0) {
cli.debug('input file: ' + args[0]);
options.src = args[0];
} else {
error('please specify an input file as first argument!');
}
if (args.length > 1) {
cli.debug('output file: ' + args[1]);
options.dest = args[1];
} else {
cli.debug('output file not specified, using default');
}
// transform with options
return transformer.transform(options)
.then(function (msg) {
cli.info(msg);
})
.catch(function (err) {
error(err);
});
}
/**
* Init the CLI instance.
*/
cli.width = 120;
cli.setUsage(usage);
cli.setApp(packagePath);
cli.enable('version', 'status', 'timeout');
cli.parse(options);
cli.main(main);