Skip to content

Commit ce6572d

Browse files
committed
feat: "global" commands, bake-<target>
for `bake foo ...`, if not defined in project Makefile, will attempt to run `bake-foo ...`
1 parent 0b0c078 commit ce6572d

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

bin/bake-init.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
const { CLI } = require('..');
4+
var init = new CLI({
5+
namespace: 'bake:init'
6+
});
7+
8+
console.log(init);

bin/bake.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
const fs = require('fs');
44
const path = require('path');
5-
const { spawn } = require('child_process');
6-
const { CLI } = require('..');
5+
const bake = require('..');
6+
const debug = require('debug')('bake');
7+
const which = require('which');
78

89
const { existsSync: exists } = fs;
10+
const { spawn } = require('child_process');
11+
const { format } = require('util');
12+
13+
const { CLI, Bake } = bake;
14+
const { verbose, info, warn, error } = bake.log;
15+
const { fail } = CLI;
16+
917
const assign = Object.assign || require('object-assign');
1018

1119
// Init
@@ -19,6 +27,28 @@ const bakefile = exists('Bakefile') ? 'Bakefile' :
1927
exists('Makefile') ? 'Makefile' :
2028
'';
2129

22-
let cli = new CLI(bakefile, {
30+
let build = new bake.Bake(bakefile, {
2331
env: env
2432
});
33+
34+
build.on(bake.Bake.UNKNOWN_TARGET, (target, targets) => {
35+
info('No target matching %s', target, build.argv);
36+
var cmd = 'bake-' + target;
37+
var filename = which.sync(cmd);
38+
39+
var args = build.argv._.slice(1);
40+
41+
info('Exec %s', cmd, args.join(' '));
42+
var sh = spawn(filename, args, {
43+
stdio: 'inherit',
44+
env: build.env
45+
});
46+
47+
sh.on('error', fail.bind(null));
48+
49+
sh.on('close', (code) => {
50+
if (code !== 0) {
51+
return fail(new Error(format('%s exited with code %d', cmd, code)));
52+
}
53+
});
54+
});

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
var bake = module.exports = require('./src/parser');
1+
var bake = module.exports;
2+
23
bake.log = require('./src/log');
34
bake.CLI = require('./src/cli');
5+
bake.Bake = require('./src/bake');
6+
bake.parser = require('./src/parser');

lib/init.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
import CLI from './cli';
3+
4+
export default class Init extends CLI {
5+
6+
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.0.4",
44
"description": "Make like Task runner",
55
"bin": {
6-
"bake": "bin/bake.js"
6+
"bake": "bin/bake.js",
7+
"bake-init": "bin/bake-init.js"
78
},
89
"main": "index.js",
910
"scripts": {
@@ -14,7 +15,8 @@
1415
"debug": "^2.2.0",
1516
"log-symbols": "^1.0.2",
1617
"minimist": "^1.2.0",
17-
"npmlog": "^2.0.3"
18+
"npmlog": "^2.0.3",
19+
"which": "^1.2.9"
1820
},
1921
"devDependencies": {
2022
"babel-cli": "^6.7.5",
@@ -39,4 +41,4 @@
3941
"cli",
4042
"runner"
4143
]
42-
}
44+
}

0 commit comments

Comments
 (0)