/
index.js
89 lines (74 loc) · 2.43 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @author Ivan Slobodnichenko <i.slobodnichenko@infomir.com>
*/
'use strict';
const
path = require('path'),
exec = require('child_process').exec,
name = 'npm',
tools = require('runner-tools'),
log = require('runner-logger').wrap(name);
function publish ( config, done ) {
if ( typeof config.onPublish === 'function' ) {
config.onPublish(function ( error, data ) {
if ( error ) {
log.warn(error.toString);
done(error);
} else {
tools.write([
{
name: path.join(config.target, 'package.json'),
data: JSON.stringify(data, null, 4)
}
], log, function ( error ) {
if ( error ) {
done(error);
} else {
// package publish execution
exec(config.command, {cwd: config.target}, function ( error, stdout, stderr ) {
if ( error ) {
log.warn(error.toString());
done(error);
} else {
(stdout + stderr).trim().split('\n').forEach(function ( line ) {
if ( line.length !== 0 ) {
log.info(line);
}
});
log.info('package %s is published', log.colors.bold(data.name));
done();
}
});
}
});
}
});
} else {
log.warn('no valid onPublish method');
done();
}
}
function generator ( config = {}, options = {} ) {
const
tasks = {},
{prefix = name + ':', suffix = ''} = options;
// sanitize and extend defaults
config = Object.assign({
onPublish: null,
target: '.',
command: 'npm publish'
}, config);
tasks[prefix + 'config' + suffix] = function () {
log.inspect(config, log);
};
tasks[prefix + 'publish' + suffix] = function ( done ) {
publish(config, done);
};
return tasks;
}
// export main actions
generator.methods = {
publish: publish
};
// public
module.exports = generator;