-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnpm.js
121 lines (100 loc) · 3.05 KB
/
npm.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module.exports = function(grunt) {
var exec = require('child_process').exec;
/**
* Show content of the package to double check before publishing
*/
grunt.registerTask('npm-show', 'Show files that would be published to NPM.', function() {
var done = this.async();
exec('npm pack', function(err, pkgFile) {
exec('tar -tf ' + pkgFile, function(err, pkgContent) {
console.log(pkgContent);
exec('rm ' + pkgFile, done);
});
});
});
/**
* Publish to NPM
*/
grunt.registerTask('npm-publish', 'Publish to NPM.', function() {
var opts = this.options({
requires: [],
abortIfDirty: true,
tag: null
});
this.requires(opts.requires);
var done = this.async();
var queue = [];
var next = function() {
if (!queue.length) {
return done();
}
queue.shift()();
};
var run = function(fn) {
queue.push(fn);
};
var runIf = function(condition, fn) {
if (condition) {
run(fn);
}
}
runIf(opts.abortIfDirty, function() {
exec('git status -s', function(err, stdout) {
if (stdout) {
return grunt.warn('Dirty workspace, cannot push to NPM.\n' + stdout + '\n');
}
next();
});
});
run(function() {
var tag = opts.tag;
if (typeof tag === 'function') {
tag = tag();
}
exec('npm publish' + (tag ? ' --tag ' + tag : ''), function(err) {
if (err) {
return grunt.fatal(err.message.replace(/\n$/, '.'));
}
grunt.log.ok('Published to NPM' + (tag ? ' @' + tag : ''));
next();
});
});
next();
});
/**
* Generate contributors, all developers who contributed, sorted by number of commits.
*/
grunt.registerTask('npm-contributors', 'Update contributors in package.json', function() {
var done = this.async();
var opts = this.options({
file: 'package.json',
commit: true,
commitMessage: 'Update contributors'
});
exec('git log --pretty=short | git shortlog -nse', function(err, stdout) {
var pkg = grunt.file.readJSON(opts.file);
pkg.contributors = stdout.toString().split('\n').slice(1, -1).map(function(line) {
return line.replace(/^[\W\d]+/, '');
});
grunt.file.write(opts.file, JSON.stringify(pkg, null, ' ') + '\n');
exec('git status -s ' + opts.file, function(err, stdout) {
if (!stdout) {
grunt.log.ok('The contributors list is already up to date.');
return done();
}
exec('git commit ' + opts.file + ' -m "' + opts.commitMessage + '"', function(err, stdout, stderr) {
if (err) {
grunt.log.error('Cannot commit contributors changes:\n ' + stderr);
} else {
grunt.log.ok('The contributors list has been updated.');
}
done();
});
});
});
});
// aliases
grunt.registerTask('contributors', 'npm-contributors');
grunt.registerTask('show', 'npm-show');
grunt.registerTask('publish', 'npm-publish');
};