|
1 |
| -'use strict'; |
2 |
| - |
3 |
| -// Node api |
4 |
| -var fs = require('fs'); |
5 |
| - |
6 |
| -// External libs. |
7 |
| -var getopt = require('posix-getopt'); |
8 |
| - |
9 |
| -// Local lib |
10 |
| -var plato = require('./plato'), |
11 |
| - info = require('./info'), |
12 |
| - util = require('./util'); |
13 |
| - |
14 |
| - |
15 |
| -function exec(options, done) { |
16 |
| - if (typeof options === 'function') { |
17 |
| - done = options; |
18 |
| - options = undefined; |
19 |
| - } |
20 |
| - |
21 |
| - if (options) { |
22 |
| - Object.keys(options).forEach(function(key) { |
23 |
| - if (!(key in exports.args)) { |
24 |
| - exports.args[key] = options[key]; |
25 |
| - } |
26 |
| - }); |
27 |
| - } |
28 |
| - |
29 |
| - var files = exports.args.files; |
30 |
| - var outputDir = exports.args.d.value; |
31 |
| - var platoOptions = { |
32 |
| - recurse: !!exports.args.r, |
33 |
| - q: !!exports.args.q, |
34 |
| - title: exports.args.t && exports.args.t.value, |
35 |
| - exclude: exports.args.x && new RegExp(exports.args.x.value), |
36 |
| - date: exports.args.D && exports.args.D.value, |
37 |
| - eslintrc: exports.args.e && exports.args.e.value |
38 |
| - }; |
39 |
| - |
40 |
| - if (exports.args.l) { |
41 |
| - var jshintrc = {}; |
42 |
| - if (typeof exports.args.l.value === 'string') { |
43 |
| - var json = fs.readFileSync(exports.args.l.value).toString(); |
44 |
| - |
45 |
| - jshintrc = JSON.parse(util.stripComments(json)); |
46 |
| - } |
47 |
| - platoOptions.jshint = { |
48 |
| - globals: jshintrc.globals || {} |
49 |
| - }; |
50 |
| - delete jshintrc.globals; |
51 |
| - platoOptions.jshint.options = jshintrc; |
52 |
| - } |
53 |
| - |
54 |
| - plato.inspect(files, outputDir, platoOptions, done); |
55 |
| -} |
56 |
| - |
57 |
| - |
58 |
| - |
59 |
| - |
60 |
| -function parseArgs(options) { // \/\\*(?:(?!\\*\/)|.|\\n)*?\\*\/ |
61 |
| - var optionString = '', |
62 |
| - required = [], |
63 |
| - modal = false; |
64 |
| - |
65 |
| - Object.keys(options).forEach(function(option) { |
66 |
| - var def = options[option]; |
67 |
| - optionString += option; |
68 |
| - if (def.type === 'String') { |
69 |
| - optionString += ':'; |
70 |
| - } |
71 |
| - if (def.long) { |
72 |
| - optionString += '(' + def.long + ')'; |
73 |
| - } |
74 |
| - if (def.required) { |
75 |
| - required.push(option); |
76 |
| - } |
77 |
| - }); |
78 |
| - |
79 |
| - var parser = new getopt.BasicParser(optionString, process.argv); |
80 |
| - var args = {}, |
81 |
| - option; |
82 |
| - |
83 |
| - while ((option = parser.getopt())) { |
84 |
| - var arg = args[option.option] || { |
85 |
| - count: 0 |
86 |
| - }; |
87 |
| - arg.count++; |
88 |
| - arg.value = option.optarg || true; |
89 |
| - |
90 |
| - args[option.option] = arg; |
91 |
| - |
92 |
| - if (options[option.option].modal) { |
93 |
| - modal = true; |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - if (!modal) { |
98 |
| - required.forEach(function(option) { |
99 |
| - if (!args[option] || !args[option].value) { |
100 |
| - console.log('Must specify a value for option %s (%s : %s)', option, options[option].long, options[option].desc); |
101 |
| - info.help(); |
102 |
| - process.exit(1); |
103 |
| - } |
104 |
| - }); |
105 |
| - } |
106 |
| - |
107 |
| - // what's left in argv |
108 |
| - args.files = process.argv.slice(parser.optind()); |
109 |
| - return args; |
110 |
| -} |
111 |
| - |
112 |
| -exports.exec = exec; |
113 |
| -exports.options = require('./cli/options'); |
114 |
| -exports.args = parseArgs(exports.options); |
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Node api |
| 4 | +var fs = require('fs'); |
| 5 | + |
| 6 | +// External libs. |
| 7 | +var getopt = require('posix-getopt'); |
| 8 | + |
| 9 | +// Local lib |
| 10 | +var plato = require('./plato'), |
| 11 | + info = require('./info'), |
| 12 | + util = require('./util'); |
| 13 | + |
| 14 | + |
| 15 | +function exec(options, done) { |
| 16 | + if (typeof options === 'function') { |
| 17 | + done = options; |
| 18 | + options = undefined; |
| 19 | + } |
| 20 | + |
| 21 | + if (options) { |
| 22 | + Object.keys(options).forEach(function decorateArgs(key) { |
| 23 | + if (!(key in exports.args)) { |
| 24 | + exports.args[key] = options[key]; |
| 25 | + } |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + var files = exports.args.files; |
| 30 | + var outputDir = exports.args.d.value; |
| 31 | + var platoOptions = { |
| 32 | + recurse: !!exports.args.r, |
| 33 | + q: !!exports.args.q, |
| 34 | + title: exports.args.t && exports.args.t.value, |
| 35 | + exclude: exports.args.x && new RegExp(exports.args.x.value), |
| 36 | + date: exports.args.D && exports.args.D.value, |
| 37 | + eslintrc: exports.args.e && exports.args.e.value |
| 38 | + }; |
| 39 | + |
| 40 | + if (exports.args.l) { |
| 41 | + var jshintrc = {}; |
| 42 | + if (typeof exports.args.l.value === 'string') { |
| 43 | + var json = fs.readFileSync(exports.args.l.value).toString(); |
| 44 | + |
| 45 | + jshintrc = JSON.parse(util.stripComments(json)); |
| 46 | + } |
| 47 | + platoOptions.jshint = { |
| 48 | + globals: jshintrc.globals || {} |
| 49 | + }; |
| 50 | + delete jshintrc.globals; |
| 51 | + platoOptions.jshint.options = jshintrc; |
| 52 | + } |
| 53 | + plato.inspect(files, outputDir, platoOptions, done); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +function parseArgs(options) { // \/\\*(?:(?!\\*\/)|.|\\n)*?\\*\/ |
| 60 | + var optionString = '', |
| 61 | + required = [], |
| 62 | + modal = false; |
| 63 | + |
| 64 | + |
| 65 | + function parseArg(option) { |
| 66 | + var def = options[option]; |
| 67 | + optionString += option; |
| 68 | + if (def.type === 'String') { |
| 69 | + optionString += ':'; |
| 70 | + } |
| 71 | + if (def.long) { |
| 72 | + optionString += '(' + def.long + ')'; |
| 73 | + } |
| 74 | + if (def.required) { |
| 75 | + required.push(option); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Object.keys(options).forEach(parseArg); |
| 80 | + |
| 81 | + var parser = new getopt.BasicParser(optionString, process.argv); |
| 82 | + var args = {}, |
| 83 | + option; |
| 84 | + |
| 85 | + while ((option = parser.getopt())) { |
| 86 | + var arg = args[option.option] || { |
| 87 | + count: 0 |
| 88 | + }; |
| 89 | + arg.count++; |
| 90 | + arg.value = option.optarg || true; |
| 91 | + |
| 92 | + args[option.option] = arg; |
| 93 | + |
| 94 | + if (options[option.option].modal) { |
| 95 | + modal = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (!modal) { |
| 100 | + required.forEach(function handleNonModal(option) { |
| 101 | + if (!args[option] || !args[option].value) { |
| 102 | + // eslint-disable-next-line no-console |
| 103 | + console.log('Must specify a value for option %s (%s : %s)', option, options[option].long, options[option].desc); |
| 104 | + info.help(); |
| 105 | + process.exit(1); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + // what's left in argv |
| 110 | + args.files = process.argv.slice(parser.optind()); |
| 111 | + |
| 112 | + return args; |
| 113 | +} |
| 114 | + |
| 115 | +exports.exec = exec; |
| 116 | +exports.options = require('./cli/options'); |
| 117 | +exports.args = parseArgs(exports.options); |
0 commit comments