/
index.js
74 lines (58 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
/*!
* imports.
*/
var debug = require('debug')('gulp-watch-local-packages');
var exec = require('child_process').exec;
var extend = require('params').extend;
var pack = require('package.root').package;
var path = require('path');
var watch = require('gulp').watch;
/*!
* init.
*/
var dependencies = extend({}, pack.dependencies, pack.devDependencies);
/*!
* exports.
*/
module.exports = watcher;
/**
* Automatically update local packages when changed.
* @param {String} prefix
* @api public
*/
function watcher(prefix) {
return watch(prefix + '/**/*.js', install.bind(null, prefix));
}
/**
* Install dependency.
* @param {String} prefix
* @param {String} event
*/
function install(prefix, event) {
var pkg = path.dirname(path.relative(path.resolve(prefix), event.path));
var dep = dependencies[pkg];
var cmd = 'npm install ' + dep;
debug('event: %s', event.type);
debug('command: %s', cmd);
if (dep) exec(cmd, onExec);
}
/**
* Exec handler.
* @param {Object} error
* @param {String} stdout
* @param {String} stderr
*/
function onExec(error, stdout, stderr) {
if (error) errorLog(error);
debug('STDOUT: %s', stdout);
debug('STDERR: %s', stderr);
}
/**
* Log errors.
* @param {Object} error
*/
function errorLog(error) {
debug('error code: %s', error.code);
debug('signal: %s', error.signal);
}