|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -import yargs from 'yargs'; |
| 3 | +import sade from 'sade'; |
4 | 4 | import microbundle from '.'; |
5 | 5 |
|
6 | | -yargs |
7 | | - .option('entry', { |
8 | | - type: 'string', |
9 | | - alias: ['i', 'e', 'entries'], |
10 | | - description: 'Entry module(s)', |
11 | | - defaultDescription: '<package.module>' |
12 | | - }) |
13 | | - .option('output', { |
14 | | - type: 'string', |
15 | | - alias: ['o', 'd'], |
16 | | - description: 'Directory to place build files into', |
17 | | - defaultDescription: '<dirname(package.main), build/>' |
18 | | - }) |
19 | | - .option('cwd', { |
20 | | - type: 'string', |
21 | | - description: 'Use an alternative working directory', |
22 | | - defaultDescription: '.' |
23 | | - }) |
24 | | - .option('format', { |
25 | | - type: 'string', |
26 | | - alias: 'f', |
27 | | - description: 'Only build specified formats', |
28 | | - defaultDescription: 'es,cjs,umd' |
29 | | - }) |
30 | | - .option('compress', { |
31 | | - type: 'boolean', |
32 | | - description: 'Compress output using UglifyJS', |
33 | | - default: true |
34 | | - }) |
35 | | - .option('strict', { |
36 | | - description: 'Enforce undefined global context and add "use strict"', |
37 | | - default: false |
38 | | - }) |
39 | | - .option('name', { |
40 | | - description: 'Specify name exposed in UMD builds', |
41 | | - default: false |
42 | | - }) |
43 | | - .command( |
44 | | - ['build [entries..]', '$0 [entries..]'], |
45 | | - 'Build once and exit', |
46 | | - () => {}, |
47 | | - argv => run(argv, false) |
48 | | - ) |
49 | | - .command( |
50 | | - 'watch [entries..]', |
51 | | - 'Rebuilds on any change', |
52 | | - () => {}, |
53 | | - argv => run(argv, true) |
54 | | - ) |
55 | | - .help() |
56 | | - .argv; |
| 6 | +let { version } = require('../package'); |
| 7 | +let prog = sade('microbundle'); |
57 | 8 |
|
58 | | -function run(options, watch) { |
59 | | - options.watch = watch===true; |
60 | | - microbundle(options) |
| 9 | +let toArray = val => Array.isArray(val) ? val : val == null ? [] : [val]; |
| 10 | + |
| 11 | +prog |
| 12 | + .version(version) |
| 13 | + .option('--entry, -i', 'Entry module(s)') |
| 14 | + .option('--output, -o', 'Directory to place build files into') |
| 15 | + .option('--format, -f', 'Only build specified formats', 'es,cjs,umd') |
| 16 | + .option('--external', `Specify external dependencies, or 'all'`) |
| 17 | + .option('--compress', 'Compress output using UglifyJS', true) |
| 18 | + .option('--strict', 'Enforce undefined global context and add "use strict"') |
| 19 | + .option('--name', 'Specify name exposed in UMD builds') |
| 20 | + .option('--cwd', 'Use an alternative working directory', '.'); |
| 21 | + |
| 22 | +prog |
| 23 | + .command('build [...entries]', '', { default: true }) |
| 24 | + .describe('Build once and exit') |
| 25 | + .action(run); |
| 26 | + |
| 27 | +prog |
| 28 | + .command('watch [...entries]') |
| 29 | + .describe('Rebuilds on any change') |
| 30 | + .action((str, opts) => run(str, opts, true)); |
| 31 | + |
| 32 | +// Parse argv; add extra aliases |
| 33 | +prog.parse(process.argv, { |
| 34 | + alias: { |
| 35 | + o: ['output', 'd'], |
| 36 | + i: ['entry', 'entries', 'e'] |
| 37 | + } |
| 38 | +}); |
| 39 | + |
| 40 | +function run(str, opts, isWatch) { |
| 41 | + opts.watch = !!isWatch; |
| 42 | + opts.entries = toArray(str || opts.entry).concat(opts._); |
| 43 | + microbundle(opts) |
61 | 44 | .then( output => { |
62 | 45 | if (output!=null) process.stdout.write(output + '\n'); |
63 | | - if (!watch) { |
64 | | - process.exit(0); |
65 | | - } |
| 46 | + if (!opts.watch) process.exit(0); |
66 | 47 | }) |
67 | 48 | .catch(err => { |
68 | 49 | process.stderr.write(String(err) + '\n'); |
|
0 commit comments