corrade-nucleus-nucleons – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | office | 1 | #!/usr/bin/env node |
2 | |||
3 | /* |
||
4 | The MIT License (MIT) |
||
5 | |||
6 | Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. |
||
7 | |||
8 | Permission is hereby granted, free of charge, to any person |
||
9 | obtaining a copy of this software and associated documentation files |
||
10 | (the "Software"), to deal in the Software without restriction, |
||
11 | including without limitation the rights to use, copy, modify, merge, |
||
12 | publish, distribute, sublicense, and/or sell copies of the Software, |
||
13 | and to permit persons to whom the Software is furnished to do so, |
||
14 | subject to the following conditions: |
||
15 | |||
16 | The above copyright notice and this permission notice shall be |
||
17 | included in all copies or substantial portions of the Software. |
||
18 | |||
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
||
23 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
||
24 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
||
25 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
26 | SOFTWARE. |
||
27 | |||
28 | Js-Beautify Command-line for node.js |
||
29 | ------------------------------------- |
||
30 | |||
31 | Written by Daniel Stockman (daniel.stockman@gmail.com) |
||
32 | |||
33 | */ |
||
34 | |||
35 | var debug = process.env.DEBUG_JSBEAUTIFY || process.env.JSBEAUTIFY_DEBUG ? function() { |
||
36 | console.error.apply(console, arguments); |
||
37 | } : function() {}; |
||
38 | |||
39 | var fs = require('fs'), |
||
40 | cc = require('config-chain'), |
||
41 | beautify = require('../index'), |
||
42 | mkdirp = require('mkdirp'), |
||
43 | nopt = require('nopt'); |
||
44 | nopt.typeDefs.brace_style = { |
||
45 | type: "brace_style", |
||
46 | validate: function(data, key, val) { |
||
47 | data[key] = val; |
||
48 | // TODO: expand-strict is obsolete, now identical to expand. Remove in future version |
||
49 | // TODO: collapse-preserve-inline is obselete, now identical to collapse,preserve-inline = true. Remove in future version |
||
50 | var validVals = ["collapse", "collapse-preserve-inline", "expand", "end-expand", "expand-strict", "none"]; |
||
51 | var valSplit = val.split(/[^a-zA-Z0-9_\-]+/); |
||
52 | for (var i = 0; i < validVals.length; i++) { |
||
53 | if (validVals[i] === val || validVals[i] === valSplit[0] && valSplit[1] === "preserve-inline") { |
||
54 | return true; |
||
55 | } |
||
56 | } |
||
57 | return false; |
||
58 | } |
||
59 | }; |
||
60 | var path = require('path'), |
||
61 | editorconfig = require('editorconfig'), |
||
62 | knownOpts = { |
||
63 | // Beautifier |
||
64 | "indent_size": Number, |
||
65 | "indent_char": String, |
||
66 | "eol": String, |
||
67 | "indent_level": Number, |
||
68 | "indent_with_tabs": Boolean, |
||
69 | "preserve_newlines": Boolean, |
||
70 | "max_preserve_newlines": Number, |
||
71 | "space_in_paren": Boolean, |
||
72 | "space_in_empty_paren": Boolean, |
||
73 | "jslint_happy": Boolean, |
||
74 | "space_after_anon_function": Boolean, |
||
75 | "brace_style": "brace_style", //See above for validation |
||
76 | "break_chained_methods": Boolean, |
||
77 | "keep_array_indentation": Boolean, |
||
78 | "unescape_strings": Boolean, |
||
79 | "wrap_line_length": Number, |
||
80 | "wrap_attributes": ["auto", "force", "force-aligned"], |
||
81 | "wrap_attributes_indent_size": Number, |
||
82 | "e4x": Boolean, |
||
83 | "end_with_newline": Boolean, |
||
84 | "comma_first": Boolean, |
||
85 | "operator_position": ["before-newline", "after-newline", "preserve-newline"], |
||
86 | // CSS-only |
||
87 | "selector_separator_newline": Boolean, |
||
88 | "newline_between_rules": Boolean, |
||
89 | "space_around_combinator": Boolean, |
||
90 | //deprecated - replaced with space_around_combinator, remove in future version |
||
91 | "space_around_selector_separator": Boolean, |
||
92 | // HTML-only |
||
93 | "max_char": Number, // obsolete since 1.3.5 |
||
94 | "unformatted": [String, Array], |
||
95 | "content_unformatted": [String, Array], |
||
96 | "indent_inner_html": [Boolean], |
||
97 | "indent_handlebars": [Boolean], |
||
98 | "indent_scripts": ["keep", "separate", "normal"], |
||
99 | "extra_liners": [String, Array], |
||
100 | // CLI |
||
101 | "version": Boolean, |
||
102 | "help": Boolean, |
||
103 | "files": [path, Array], |
||
104 | "outfile": path, |
||
105 | "replace": Boolean, |
||
106 | "quiet": Boolean, |
||
107 | "type": ["js", "css", "html"], |
||
108 | "config": path, |
||
109 | "editorconfig": Boolean |
||
110 | }, |
||
111 | // dasherizeShorthands provides { "indent-size": ["--indent_size"] } |
||
112 | // translation, allowing more convenient dashes in CLI arguments |
||
113 | shortHands = dasherizeShorthands({ |
||
114 | // Beautifier |
||
115 | "s": ["--indent_size"], |
||
116 | "c": ["--indent_char"], |
||
117 | "e": ["--eol"], |
||
118 | "l": ["--indent_level"], |
||
119 | "t": ["--indent_with_tabs"], |
||
120 | "p": ["--preserve_newlines"], |
||
121 | "m": ["--max_preserve_newlines"], |
||
122 | "P": ["--space_in_paren"], |
||
123 | "Q": ["--space_in_empty_paren"], |
||
124 | "j": ["--jslint_happy"], |
||
125 | "a": ["--space_after_anon_function"], |
||
126 | "b": ["--brace_style"], |
||
127 | "B": ["--break_chained_methods"], |
||
128 | "k": ["--keep_array_indentation"], |
||
129 | "x": ["--unescape_strings"], |
||
130 | "w": ["--wrap_line_length"], |
||
131 | "X": ["--e4x"], |
||
132 | "n": ["--end_with_newline"], |
||
133 | "C": ["--comma_first"], |
||
134 | "O": ["--operator_position"], |
||
135 | // CSS-only |
||
136 | "L": ["--selector_separator_newline"], |
||
137 | "N": ["--newline_between_rules"], |
||
138 | // HTML-only |
||
139 | "A": ["--wrap_attributes"], |
||
140 | "i": ["--wrap_attributes_indent_size"], |
||
141 | "W": ["--max_char"], // obsolete since 1.3.5 |
||
142 | "U": ["--unformatted"], |
||
143 | "T": ["--content_unformatted"], |
||
144 | "I": ["--indent_inner_html"], |
||
145 | "H": ["--indent_handlebars"], |
||
146 | "S": ["--indent_scripts"], |
||
147 | "E": ["--extra_liners"], |
||
148 | // non-dasherized hybrid shortcuts |
||
149 | "good-stuff": [ |
||
150 | "--keep_array_indentation", |
||
151 | "--keep_function_indentation", |
||
152 | "--jslint_happy" |
||
153 | ], |
||
154 | "js": ["--type", "js"], |
||
155 | "css": ["--type", "css"], |
||
156 | "html": ["--type", "html"], |
||
157 | // CLI |
||
158 | "v": ["--version"], |
||
159 | "h": ["--help"], |
||
160 | "f": ["--files"], |
||
161 | "o": ["--outfile"], |
||
162 | "r": ["--replace"], |
||
163 | "q": ["--quiet"] |
||
164 | // no shorthand for "config" |
||
165 | // no shorthand for "editorconfig" |
||
166 | }); |
||
167 | |||
168 | function verifyExists(fullPath) { |
||
169 | return fs.existsSync(fullPath) ? fullPath : null; |
||
170 | } |
||
171 | |||
172 | function findRecursive(dir, fileName) { |
||
173 | var fullPath = path.join(dir, fileName); |
||
174 | var nextDir = path.dirname(dir); |
||
175 | var result = verifyExists(fullPath); |
||
176 | |||
177 | if (!result && (nextDir !== dir)) { |
||
178 | result = findRecursive(nextDir, fileName); |
||
179 | } |
||
180 | |||
181 | return result; |
||
182 | } |
||
183 | |||
184 | function getUserHome() { |
||
185 | var user_home = ''; |
||
186 | try { |
||
187 | user_home = process.env.USERPROFILE || process.env.HOME || ''; |
||
188 | } catch (ex) {} |
||
189 | return user_home; |
||
190 | } |
||
191 | |||
192 | function set_file_editorconfig_opts(file, config) { |
||
193 | try { |
||
194 | var eConfigs = editorconfig.parseSync(file); |
||
195 | |||
196 | if (eConfigs.indent_style === "tab") { |
||
197 | config.indent_with_tabs = true; |
||
198 | } else if (eConfigs.indent_style === "space") { |
||
199 | config.indent_with_tabs = false; |
||
200 | } |
||
201 | |||
202 | if (eConfigs.indent_size) { |
||
203 | config.indent_size = eConfigs.indent_size; |
||
204 | } |
||
205 | |||
206 | if (eConfigs.max_line_length) { |
||
207 | if (eConfigs.max_line_length === "off") { |
||
208 | config.wrap_line_length = 0; |
||
209 | } else { |
||
210 | config.wrap_line_length = parseInt(eConfigs.max_line_length); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | if (eConfigs.insert_final_newline === true) { |
||
215 | config.end_with_newline = true; |
||
216 | } else if (eConfigs.insert_final_newline === false) { |
||
217 | config.end_with_newline = false; |
||
218 | } |
||
219 | |||
220 | if (eConfigs.end_of_line) { |
||
221 | if (eConfigs.end_of_line === 'cr') { |
||
222 | config.eol = '\r'; |
||
223 | } else if (eConfigs.end_of_line === 'lf') { |
||
224 | config.eol = '\n'; |
||
225 | } else if (eConfigs.end_of_line === 'crlf') { |
||
226 | config.eol = '\r\n'; |
||
227 | } |
||
228 | } |
||
229 | } catch (e) { |
||
230 | debug(e); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | // var cli = require('js-beautify/cli'); cli.interpret(); |
||
235 | var interpret = exports.interpret = function(argv, slice) { |
||
236 | var parsed = nopt(knownOpts, shortHands, argv, slice); |
||
237 | |||
238 | if (parsed.version) { |
||
239 | console.log(require('../../package.json').version); |
||
240 | process.exit(0); |
||
241 | } else if (parsed.help) { |
||
242 | usage(); |
||
243 | process.exit(0); |
||
244 | } |
||
245 | |||
246 | var cfg; |
||
247 | var configRecursive = findRecursive(process.cwd(), '.jsbeautifyrc'); |
||
248 | var configHome = verifyExists(path.join(getUserHome() || "", ".jsbeautifyrc")); |
||
249 | var configDefault = __dirname + '/../config/defaults.json'; |
||
250 | |||
251 | try { |
||
252 | cfg = cc( |
||
253 | parsed, |
||
254 | cleanOptions(cc.env('jsbeautify_'), knownOpts), |
||
255 | parsed.config, |
||
256 | configRecursive, |
||
257 | configHome, |
||
258 | configDefault |
||
259 | ).snapshot; |
||
260 | } catch (ex) { |
||
261 | debug(cfg); |
||
262 | // usage(ex); |
||
263 | console.error(ex); |
||
264 | console.error('Error while loading beautifier configuration.'); |
||
265 | console.error('Configuration file chain included:'); |
||
266 | if (parsed.config) { |
||
267 | console.error(parsed.config); |
||
268 | } |
||
269 | if (configRecursive) { |
||
270 | console.error(configRecursive); |
||
271 | } |
||
272 | if (configHome) { |
||
273 | console.error(configHome); |
||
274 | } |
||
275 | console.error(configDefault); |
||
276 | console.error('Run `' + getScriptName() + ' -h` for help.'); |
||
277 | process.exit(1); |
||
278 | } |
||
279 | |||
280 | try { |
||
281 | // Verify arguments |
||
282 | checkType(cfg); |
||
283 | checkFiles(cfg); |
||
284 | debug(cfg); |
||
285 | |||
286 | // Process files synchronously to avoid EMFILE error |
||
287 | cfg.files.forEach(processInputSync, { |
||
288 | cfg: cfg |
||
289 | }); |
||
290 | } catch (ex) { |
||
291 | debug(cfg); |
||
292 | // usage(ex); |
||
293 | console.error(ex); |
||
294 | console.error('Run `' + getScriptName() + ' -h` for help.'); |
||
295 | process.exit(1); |
||
296 | } |
||
297 | }; |
||
298 | |||
299 | // interpret args immediately when called as executable |
||
300 | if (require.main === module) { |
||
301 | interpret(); |
||
302 | } |
||
303 | |||
304 | function usage(err) { |
||
305 | var scriptName = getScriptName(); |
||
306 | var msg = [ |
||
307 | scriptName + '@' + require('../../package.json').version, |
||
308 | '', |
||
309 | 'CLI Options:', |
||
310 | ' -f, --file Input file(s) (Pass \'-\' for stdin)', |
||
311 | ' -r, --replace Write output in-place, replacing input', |
||
312 | ' -o, --outfile Write output to file (default stdout)', |
||
313 | ' --config Path to config file', |
||
314 | ' --type [js|css|html] ["js"]', |
||
315 | ' -q, --quiet Suppress logging to stdout', |
||
316 | ' -h, --help Show this help', |
||
317 | ' -v, --version Show the version', |
||
318 | '', |
||
319 | 'Beautifier Options:', |
||
320 | ' -s, --indent-size Indentation size [4]', |
||
321 | ' -c, --indent-char Indentation character [" "]', |
||
322 | ' -t, --indent-with-tabs Indent with tabs, overrides -s and -c', |
||
323 | ' -e, --eol Character(s) to use as line terminators.', |
||
324 | ' [first newline in file, otherwise "\\n]', |
||
325 | ' -n, --end-with-newline End output with newline', |
||
326 | ' --editorconfig Use EditorConfig to set up the options' |
||
327 | ]; |
||
328 | |||
329 | switch (scriptName.split('-').shift()) { |
||
330 | case "js": |
||
331 | msg.push(' -l, --indent-level Initial indentation level [0]'); |
||
332 | msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)'); |
||
333 | msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]'); |
||
334 | msg.push(' -P, --space-in-paren Add padding spaces within paren, ie. f( a, b )'); |
||
335 | msg.push(' -E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )'); |
||
336 | msg.push(' -j, --jslint-happy Enable jslint-stricter mode'); |
||
337 | msg.push(' -a, --space-after-anon-function Add a space before an anonymous function\'s parens, ie. function ()'); |
||
338 | msg.push(' -b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]'); |
||
339 | msg.push(' -B, --break-chained-methods Break chained method calls across subsequent lines'); |
||
340 | msg.push(' -k, --keep-array-indentation Preserve array indentation'); |
||
341 | msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation'); |
||
342 | msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]'); |
||
343 | msg.push(' -X, --e4x Pass E4X xml literals through untouched'); |
||
344 | msg.push(' --good-stuff Warm the cockles of Crockford\'s heart'); |
||
345 | msg.push(' -C, --comma-first Put commas at the beginning of new line instead of end'); |
||
346 | msg.push(' -O, --operator-position Set operator position (before-newline|after-newline|preserve-newline) [before-newline]'); |
||
347 | break; |
||
348 | case "html": |
||
349 | msg.push(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]'); |
||
350 | msg.push(' -I, --indent-inner-html Indent body and head sections. Default is false.'); |
||
351 | msg.push(' -H, --indent-handlebars Indent handlebars. Default is false.'); |
||
352 | msg.push(' -S, --indent-scripts [keep|separate|normal] ["normal"]'); |
||
353 | msg.push(' -w, --wrap-line-length Wrap lines at next opportunity after N characters [0]'); |
||
354 | msg.push(' -A, --wrap-attributes Wrap html tag attributes to new lines [auto|force] ["auto"]'); |
||
355 | msg.push(' -i, --wrap-attributes-indent-size Indent wrapped tags to after N characters [indent-level]'); |
||
356 | msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)'); |
||
357 | msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]'); |
||
358 | msg.push(' -U, --unformatted List of tags (defaults to inline) that should not be reformatted'); |
||
359 | msg.push(' -T, --content_unformatted List of tags (defaults to pre) that its content should not be reformatted'); |
||
360 | msg.push(' -E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline'); |
||
361 | break; |
||
362 | case "css": |
||
363 | msg.push(' -L, --selector-separator-newline Add a newline between multiple selectors.'); |
||
364 | msg.push(' -N, --newline-between-rules Add a newline between CSS rules.'); |
||
365 | } |
||
366 | |||
367 | if (err) { |
||
368 | msg.push(err); |
||
369 | msg.push(''); |
||
370 | console.error(msg.join('\n')); |
||
371 | } else { |
||
372 | console.log(msg.join('\n')); |
||
373 | } |
||
374 | } |
||
375 | |||
376 | // main iterator, {cfg} passed as thisArg of forEach call |
||
377 | |||
378 | function processInputSync(filepath) { |
||
379 | var data = '', |
||
380 | config = this.cfg, |
||
381 | outfile = config.outfile, |
||
382 | input; |
||
383 | |||
384 | // -o passed with no value overwrites |
||
385 | if (outfile === true || config.replace) { |
||
386 | outfile = filepath; |
||
387 | } |
||
388 | |||
389 | var fileType = getOutputType(outfile, filepath, config.type); |
||
390 | |||
391 | if (config.editorconfig) { |
||
392 | var editorconfig_filepath = filepath; |
||
393 | |||
394 | if (editorconfig_filepath === '-') { |
||
395 | if (outfile) { |
||
396 | editorconfig_filepath = outfile; |
||
397 | } else { |
||
398 | editorconfig_filepath = 'stdin.' + fileType; |
||
399 | } |
||
400 | } |
||
401 | |||
402 | debug("EditorConfig is enabled for ", editorconfig_filepath); |
||
403 | config = cc(config).snapshot; |
||
404 | set_file_editorconfig_opts(editorconfig_filepath, config); |
||
405 | debug(config); |
||
406 | } |
||
407 | |||
408 | if (filepath === '-') { |
||
409 | input = process.stdin; |
||
410 | input.resume(); |
||
411 | |||
412 | input.setEncoding('utf8'); |
||
413 | |||
414 | input.on('data', function(chunk) { |
||
415 | data += chunk; |
||
416 | }); |
||
417 | |||
418 | input.on('end', function() { |
||
419 | makePretty(fileType, data, config, outfile, writePretty); // Where things get beautified |
||
420 | }); |
||
421 | } else { |
||
422 | data = fs.readFileSync(filepath, 'utf8'); |
||
423 | makePretty(fileType, data, config, outfile, writePretty); |
||
424 | } |
||
425 | } |
||
426 | |||
427 | function makePretty(fileType, code, config, outfile, callback) { |
||
428 | try { |
||
429 | var pretty = beautify[fileType](code, config); |
||
430 | |||
431 | callback(null, pretty, outfile, config); |
||
432 | } catch (ex) { |
||
433 | callback(ex); |
||
434 | } |
||
435 | } |
||
436 | |||
437 | function writePretty(err, pretty, outfile, config) { |
||
438 | debug('writing ' + outfile); |
||
439 | if (err) { |
||
440 | console.error(err); |
||
441 | process.exit(1); |
||
442 | } |
||
443 | |||
444 | if (outfile) { |
||
445 | mkdirp.sync(path.dirname(outfile)); |
||
446 | |||
447 | if (isFileDifferent(outfile, pretty)) { |
||
448 | try { |
||
449 | fs.writeFileSync(outfile, pretty, 'utf8'); |
||
450 | logToStdout('beautified ' + path.relative(process.cwd(), outfile), config); |
||
451 | } catch (ex) { |
||
452 | onOutputError(ex); |
||
453 | } |
||
454 | } else { |
||
455 | logToStdout('beautified ' + path.relative(process.cwd(), outfile) + ' - unchanged', config); |
||
456 | } |
||
457 | } else { |
||
458 | process.stdout.write(pretty); |
||
459 | } |
||
460 | } |
||
461 | |||
462 | function isFileDifferent(filePath, expected) { |
||
463 | try { |
||
464 | return fs.readFileSync(filePath, 'utf8') !== expected; |
||
465 | } catch (ex) { |
||
466 | // failing to read is the same as different |
||
467 | return true; |
||
468 | } |
||
469 | } |
||
470 | |||
471 | // workaround the fact that nopt.clean doesn't return the object passed in :P |
||
472 | |||
473 | function cleanOptions(data, types) { |
||
474 | nopt.clean(data, types); |
||
475 | return data; |
||
476 | } |
||
477 | |||
478 | // error handler for output stream that swallows errors silently, |
||
479 | // allowing the loop to continue over unwritable files. |
||
480 | |||
481 | function onOutputError(err) { |
||
482 | if (err.code === 'EACCES') { |
||
483 | console.error(err.path + " is not writable. Skipping!"); |
||
484 | } else { |
||
485 | console.error(err); |
||
486 | process.exit(0); |
||
487 | } |
||
488 | } |
||
489 | |||
490 | // turn "--foo_bar" into "foo-bar" |
||
491 | |||
492 | function dasherizeFlag(str) { |
||
493 | return str.replace(/^\-+/, '').replace(/_/g, '-'); |
||
494 | } |
||
495 | |||
496 | // translate weird python underscored keys into dashed argv, |
||
497 | // avoiding single character aliases. |
||
498 | |||
499 | function dasherizeShorthands(hash) { |
||
500 | // operate in-place |
||
501 | Object.keys(hash).forEach(function(key) { |
||
502 | // each key value is an array |
||
503 | var val = hash[key][0]; |
||
504 | // only dasherize one-character shorthands |
||
505 | if (key.length === 1 && val.indexOf('_') > -1) { |
||
506 | hash[dasherizeFlag(val)] = val; |
||
507 | } |
||
508 | }); |
||
509 | |||
510 | return hash; |
||
511 | } |
||
512 | |||
513 | function getOutputType(outfile, filepath, configType) { |
||
514 | if (outfile && /\.(js|css|html)$/.test(outfile)) { |
||
515 | return outfile.split('.').pop(); |
||
516 | } else if (filepath !== '-' && /\.(js|css|html)$/.test(filepath)) { |
||
517 | return filepath.split('.').pop(); |
||
518 | } else if (configType) { |
||
519 | return configType; |
||
520 | } else { |
||
521 | throw 'Could not determine appropriate beautifier from file paths: ' + filepath; |
||
522 | } |
||
523 | } |
||
524 | |||
525 | function getScriptName() { |
||
526 | return path.basename(process.argv[1]); |
||
527 | } |
||
528 | |||
529 | function checkType(parsed) { |
||
530 | var scriptType = getScriptName().split('-').shift(); |
||
531 | if (!/^(js|css|html)$/.test(scriptType)) { |
||
532 | scriptType = null; |
||
533 | } |
||
534 | |||
535 | debug("executable type:", scriptType); |
||
536 | |||
537 | var parsedType = parsed.type; |
||
538 | debug("parsed type:", parsedType); |
||
539 | |||
540 | if (!parsedType) { |
||
541 | debug("type defaulted:", scriptType); |
||
542 | parsed.type = scriptType; |
||
543 | } |
||
544 | } |
||
545 | |||
546 | function checkFiles(parsed) { |
||
547 | var argv = parsed.argv; |
||
548 | var isTTY = true; |
||
549 | |||
550 | try { |
||
551 | isTTY = process.stdin.isTTY; |
||
552 | } catch (ex) { |
||
553 | debug("error querying for isTTY:", ex); |
||
554 | } |
||
555 | |||
556 | debug('isTTY: ' + isTTY); |
||
557 | |||
558 | if (!parsed.files) { |
||
559 | parsed.files = []; |
||
560 | } else { |
||
561 | if (argv.cooked.indexOf('-') > -1) { |
||
562 | // strip stdin path eagerly added by nopt in '-f -' case |
||
563 | parsed.files.some(removeDashedPath); |
||
564 | } |
||
565 | } |
||
566 | |||
567 | if (argv.remain.length) { |
||
568 | // assume any remaining args are files |
||
569 | argv.remain.forEach(function(f) { |
||
570 | if (f !== '-') { |
||
571 | parsed.files.push(path.resolve(f)); |
||
572 | } |
||
573 | }); |
||
574 | } |
||
575 | |||
576 | if ('string' === typeof parsed.outfile && isTTY && !parsed.files.length) { |
||
577 | // use outfile as input when no other files passed in args |
||
578 | parsed.files.push(parsed.outfile); |
||
579 | // operation is now an implicit overwrite |
||
580 | parsed.replace = true; |
||
581 | } |
||
582 | |||
583 | if (!parsed.files.length) { |
||
584 | // read stdin by default |
||
585 | parsed.files.push('-'); |
||
586 | } |
||
587 | debug('files.length ' + parsed.files.length); |
||
588 | |||
589 | if (parsed.files.indexOf('-') > -1 && isTTY) { |
||
590 | throw 'Must pipe input or define at least one file.'; |
||
591 | } |
||
592 | |||
593 | parsed.files.forEach(testFilePath); |
||
594 | |||
595 | return parsed; |
||
596 | } |
||
597 | |||
598 | function removeDashedPath(filepath, i, arr) { |
||
599 | var found = filepath.lastIndexOf('-') === (filepath.length - 1); |
||
600 | if (found) { |
||
601 | arr.splice(i, 1); |
||
602 | } |
||
603 | return found; |
||
604 | } |
||
605 | |||
606 | function testFilePath(filepath) { |
||
607 | try { |
||
608 | if (filepath !== "-") { |
||
609 | fs.statSync(filepath); |
||
610 | } |
||
611 | } catch (err) { |
||
612 | throw 'Unable to open path "' + filepath + '"'; |
||
613 | } |
||
614 | } |
||
615 | |||
616 | function logToStdout(str, config) { |
||
617 | if (typeof config.quiet === "undefined" || !config.quiet) { |
||
618 | console.log(str); |
||
619 | } |
||
620 | } |