Skip to content

Commit 90d02e6

Browse files
authored
Merge pull request #32 from lukeed/cli
Migrate CLI to Sade
2 parents d8162d5 + 1b315c7 commit 90d02e6

3 files changed

Lines changed: 43 additions & 64 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"bin": "dist/cli.js",
88
"scripts": {
99
"build": "npm run -s build:babel && npm run -s build:self",
10-
"build:babel": "babel-node --presets env src/cli.js --external all --format cjs src/*.js",
10+
"build:babel": "babel-node src/cli.js --external all --format cjs src/*.js --presets env",
1111
"build:self": "node dist/cli.js --external all --format cjs src/*.js",
1212
"prepare": "npm run -s build",
1313
"prepare:babel": "babel --presets env src/*.js -d dist && npm t",
@@ -56,7 +56,7 @@
5656
"rollup-plugin-preserve-shebang": "^0.1.3",
5757
"rollup-plugin-sizes": "^0.4.2",
5858
"rollup-plugin-uglify": "^2.0.1",
59-
"yargs": "^10.0.3"
59+
"sade": "^1.2.0"
6060
},
6161
"devDependencies": {
6262
"babel-cli": "^6.26.0",

src/cli.js

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,49 @@
11
#!/usr/bin/env node
22

3-
import yargs from 'yargs';
3+
import sade from 'sade';
44
import microbundle from '.';
55

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');
578

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)
6144
.then( output => {
6245
if (output!=null) process.stdout.write(output + '\n');
63-
if (!watch) {
64-
process.exit(0);
65-
}
46+
if (!opts.watch) process.exit(0);
6647
})
6748
.catch(err => {
6849
process.stderr.write(String(err) + '\n');

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ const isDir = name => stat(name).then( stats => stats.isDirectory() ).catch( ()
2626
const isFile = name => stat(name).then( stats => stats.isFile() ).catch( () => false );
2727
const safeVariableName = name => camelCase(name.toLowerCase().replace(/((^[^a-zA-Z]+)|[^\w.-])|([^a-zA-Z0-9]+$)/g, ''));
2828

29-
const FORMATS = ['es', 'cjs', 'umd'];
30-
3129
const WATCH_OPTS = {
3230
exclude: 'node_modules/**'
3331
};
3432

3533
export default async function microbundle(options) {
36-
let cwd = options.cwd = resolve(process.cwd(), options.cwd || '.');
34+
let cwd = options.cwd = resolve(process.cwd(), options.cwd);
3735

3836
try {
3937
options.pkg = JSON.parse(await readFile(resolve(cwd, 'package.json'), 'utf8'));
@@ -73,7 +71,7 @@ export default async function microbundle(options) {
7371

7472
options.multipleEntries = entries.length>1;
7573

76-
let formats = [].concat(options.format || options.formats || FORMATS);
74+
let formats = (options.format || options.formats).split(',');
7775

7876
let steps = [];
7977
for (let i=0; i<entries.length; i++) {

0 commit comments

Comments
 (0)