|
| 1 | +/// <reference path="./cli.d.ts" /> |
| 2 | + |
| 3 | +import * as cli from "cli"; |
| 4 | + |
| 5 | +// ======================================================================== |
| 6 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/cat.js |
| 7 | +// ======================================================================== |
| 8 | + |
| 9 | +var output_file = function(file: string) { |
| 10 | + cli.withInput(file, function (line, sep, eof) { |
| 11 | + if (!eof) { |
| 12 | + cli.output(line + sep); |
| 13 | + } else if (cli.args.length) { |
| 14 | + output_file(cli.args.shift()); |
| 15 | + } |
| 16 | + }); |
| 17 | +}; |
| 18 | + |
| 19 | +if (cli.args.length) { |
| 20 | + output_file(cli.args.shift()); |
| 21 | +} |
| 22 | + |
| 23 | +// ============================================================================ |
| 24 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/command.js |
| 25 | +// ============================================================================ |
| 26 | + |
| 27 | +cli.parse(null, ['install', 'test', 'edit', 'remove', 'uninstall', 'ls']); |
| 28 | + |
| 29 | +console.log('Command is: ' + cli.command); |
| 30 | + |
| 31 | + |
| 32 | +// ============================================================================ |
| 33 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/echo.js |
| 34 | +// ============================================================================ |
| 35 | + |
| 36 | +cli.parse({ |
| 37 | + newline: ['n', 'Do not output the trailing newline'], |
| 38 | + escape: ['e', 'Enable interpretation of backslash escapes'], |
| 39 | + separator: ['s', 'Separate arguments using this value', 'string', ' '], |
| 40 | + output: [false, 'Write to FILE rather than the console', 'file'] |
| 41 | +}); |
| 42 | + |
| 43 | +cli.main(function (args, options) { |
| 44 | + var output = '', i: any, j: any, l: number, output_stream: NodeJS.WritableStream; |
| 45 | + |
| 46 | + if (this.argc) { |
| 47 | + if (options.escape) { |
| 48 | + var replace: any = {'\\n':'\n','\\r':'\r','\\t':'\t','\\e':'\e','\\v':'\v','\\f':'\f','\\c':'\c','\\b':'\b','\\a':'\a','\\\\':'\\'}; |
| 49 | + var escape = function (str: string) { |
| 50 | + str += ''; |
| 51 | + for (j in replace) { |
| 52 | + str = str.replace(i, replace[i]); |
| 53 | + } |
| 54 | + return str; |
| 55 | + } |
| 56 | + for (i = 0, l = this.argc; i < l; i++) { |
| 57 | + args[i] = escape(args[i]); |
| 58 | + } |
| 59 | + options.separator = escape(options.separator); |
| 60 | + } |
| 61 | + output += args.join(options.separator); |
| 62 | + } |
| 63 | + |
| 64 | + if (!options.newline) { |
| 65 | + output += '\n'; |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + if (options.output) { |
| 70 | + output_stream = this.native.fs.createWriteStream(options.output) |
| 71 | + } else { |
| 72 | + output_stream = process.stdout; |
| 73 | + } |
| 74 | + output_stream.write(output); |
| 75 | + } catch (e) { |
| 76 | + this.fatal('Could not write to output stream'); |
| 77 | + } |
| 78 | +}); |
| 79 | + |
| 80 | + |
| 81 | +// ========================================================================= |
| 82 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/glob.js |
| 83 | +// ========================================================================= |
| 84 | + |
| 85 | +cli.enable('glob'); |
| 86 | + |
| 87 | +//Running `./glob.js *.js` will output a list of .js files in this directory |
| 88 | +console.log(cli.args); |
| 89 | + |
| 90 | + |
| 91 | +// ============================================================================== |
| 92 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/long_desc.js |
| 93 | +// ============================================================================== |
| 94 | + |
| 95 | +//You can (optionally) boost the width of output with: |
| 96 | +cli.width = 120; |
| 97 | + |
| 98 | +//You can also adjust the width of the options/command definitions |
| 99 | +cli.option_width = 25; |
| 100 | + |
| 101 | +var long_desc = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s ' |
| 102 | + + 'standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make' |
| 103 | + + ' a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, ' |
| 104 | + + 'remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing ' |
| 105 | + + 'Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions' |
| 106 | + + ' of Lorem Ipsum.'; |
| 107 | + |
| 108 | +cli.parse({ |
| 109 | + foo: ['f', long_desc] |
| 110 | +}); |
| 111 | + |
| 112 | + |
| 113 | +// ============================================================================= |
| 114 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/progress.js |
| 115 | +// ============================================================================= |
| 116 | + |
| 117 | +var i = 0, interval = setInterval(function () { |
| 118 | + cli.progress(++i / 100); |
| 119 | + if (i === 100) { |
| 120 | + clearInterval(interval); |
| 121 | + cli.ok('Finished!'); |
| 122 | + } |
| 123 | +}, 50); |
| 124 | + |
| 125 | + |
| 126 | +// ========================================================================= |
| 127 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/sort.js |
| 128 | +// ========================================================================= |
| 129 | + |
| 130 | +var options = cli.parse({ |
| 131 | + numeric: ['n', 'Compare using a numeric sort'], |
| 132 | + reverse: ['r', 'Reverse the results'] |
| 133 | +}); |
| 134 | + |
| 135 | +cli.withStdinLines(function (lines, newline) { |
| 136 | + lines.sort(!options.numeric ? null : function (a, b) { |
| 137 | + return parseInt(a) - parseInt(b); |
| 138 | + }); |
| 139 | + if (options.reverse) { |
| 140 | + lines.reverse(); |
| 141 | + } |
| 142 | + this.output(lines.join(newline)); |
| 143 | +}); |
| 144 | + |
| 145 | + |
| 146 | +// ============================================================================ |
| 147 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/spinner.js |
| 148 | +// ============================================================================ |
| 149 | + |
| 150 | +cli.spinner('Working..'); |
| 151 | + |
| 152 | +setTimeout(function () { |
| 153 | + cli.spinner('Working.. done!', true); //End the spinner |
| 154 | +}, 3000); |
| 155 | + |
| 156 | + |
| 157 | +// =========================================================================== |
| 158 | +// Example: https://github.com/node-js-libs/cli/blob/master/examples/static.js |
| 159 | +// =========================================================================== |
| 160 | + |
| 161 | +cli.parse({ |
| 162 | + log: ['l', 'Enable logging'], |
| 163 | + port: ['p', 'Listen on this port', 'number', 8080], |
| 164 | + serve: [false, 'Serve static files from PATH', 'path', './public'] |
| 165 | +}); |
| 166 | + |
| 167 | +cli.main(function (args, options) { |
| 168 | + var server: any, middleware: any = []; |
| 169 | + |
| 170 | + if (options.log) { |
| 171 | + this.debug('Enabling logging'); |
| 172 | + middleware.push(require('creationix/log')()); |
| 173 | + } |
| 174 | + |
| 175 | + this.debug('Serving files from ' + options.serve); |
| 176 | + middleware.push(require('creationix/static')('/', options.serve, 'index.html')); |
| 177 | + |
| 178 | + server = this.createServer(middleware).listen(options.port); |
| 179 | + |
| 180 | + this.ok('Listening on port ' + options.port); |
| 181 | +}); |
0 commit comments