-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatefile.js
219 lines (194 loc) · 5.98 KB
/
updatefile.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
var path = require('path');
var extend = require('extend-shallow');
var stringify = require('stringify-author');
var normalize = require('gulp-normalize-pkg');
var parse = require('parse-author');
var utils = require('./utils');
module.exports = function(app) {
if (!utils.isValid(app, 'updater-package')) return;
var contributors = {};
app.use(utils.pkg());
app.option(app.base.options);
app.onLoad(/package\.json/, function(file, next) {
var pkg = JSON.parse(file.content);
if (Array.isArray(pkg.contributors)) {
contributors = parsePersons(pkg.contributors);
}
next();
});
app.preWrite(/package\.json/, formatPreWrite(app, contributors));
app.preWrite(/package\.json/, function(file, next) {
file.content = file.content.replace(/\s+$/, '') + '\n';
next();
});
app.task('default', ['package-update']);
/**
* Normalizes and updates fields in package.json to use the latest [npm]() conventions,
* and baseline defaults defined in [normalize-pkg][]. Override any of the defaults
* by defining custom fields on `options.fields`. See [customization](#customization)
* for more details.
*
* ```sh
* update package
* ```
* @name package
* @api public
*/
app.task('package-update', {silent: true}, function() {
var opts = extend({}, app.option('pkg'));
return app.src('package.json', {cwd: app.cwd})
.pipe(createIndex(app))
.pipe(normalize(opts))
.pipe(owner(app))
.pipe(app.dest(app.cwd));
});
/**
* Ensures the `files` field in package.json has the value defined on the `main` property.
* Also aliased as `package:package-normalize` to prevent task naming conflicts when this is used
* as a plugin.
*
* ```sh
* update package:normalize
* ```
* @name package:normalize
* @api public
*/
app.task('normalize', ['package-normalize']);
app.task('package-normalize', {silent: true}, function() {
var opts = extend({}, app.option('pkg'));
return app.src('package.json', {cwd: app.cwd})
.pipe(normalize(opts))
.pipe(app.dest(app.cwd));
});
/**
* Updates the `files` and `main` fields in package.json to ensure it has the value defined on
* the `main` property. Also aliased as `package:package-files` to provide a semantic task
* name to use when this updater is used as a plugin.
*
* ```sh
* update package:index
* ```
* @name package:index
*/
app.task('index', ['package-index']);
app.task('package-index', {silent: true}, function() {
return app.src('package.json', {cwd: app.cwd})
.pipe(createIndex(app))
.pipe(app.dest(app.cwd));
});
/**
* Runs [generate-package][] to create a new package.json in the current working directory
* or specified `--dest`.
*
* ```sh
* update package
* ```
* @name package
* @api public
*/
app.task('new', function(cb) {
app.register('generate-package', require('generate-package'));
app.generate('generate-package', cb);
});
};
function createIndex(app) {
return utils.through.obj(function(file, enc, next) {
var pkg = JSON.parse(file.contents.toString());
if (pkg.license && /Released/i.test(pkg.license)) {
pkg.license = 'MIT';
}
var indexPath = path.resolve(app.cwd, 'index.js');
var re = /^(updater|generate|assemble|verb)-/;
if (!pkg.hasOwnProperty('main') && !utils.exists(indexPath)) {
var configfiles = utils.getFiles(app.cwd, app.options.configfiles);
if (re.test(pkg.name) && configfiles.length) {
var contents = `'use strict';\n\nmodule.exports = require('./${configfile}');`;
pkg.files = pkg.files || [];
if (pkg.files.indexOf('index.js') === -1) {
pkg.files.push('index.js');
}
pkg.main = 'index.js';
pkg.files.sort();
var index = new utils.File({path: indexPath, contents: new Buffer(contents)});
this.push(index);
}
}
file.contents = new Buffer(JSON.stringify(pkg, null, 2));
next(null, file);
});
}
function formatPreWrite(app, contributors) {
return function(file, next) {
var pkg = JSON.parse(file.contents);
if (Array.isArray(pkg.files)) {
pkg.files = utils.compact(pkg.files).filter(function(name) {
return !/license|readme\.md/i.test(name);
});
}
if (Array.isArray(pkg.contributors)) {
pkg.contributors = updateContributors(pkg, contributors);
}
// add user defined files to `files`
addFiles(app, pkg);
var deps = pkg.devDependencies;
if (deps && deps['verb-tag-jscomments']) {
delete deps['verb-tag-jscomments'];
delete deps['verb'];
pkg.devDependencies = deps;
}
var str = JSON.stringify(pkg, null, 2).trim();
str += '\n';
file.contents = new Buffer(str);
next();
};
}
/**
* Add user defined files to `files`, but only if they exist
* @param {Object} app
* @param {Object} pkg
*/
function addFiles(app, pkg) {
utils.addFiles(app.cwd, pkg, app.options.addFiles);
}
function updateContributors(pkg, origContribs) {
var res = [];
var obj = parsePersons(utils.compact(pkg.contributors));
var contributors = extend({}, obj, origContribs);
var keys = Object.keys(contributors);
keys.sort(function(a, b) {
return a.localeCompare(b);
});
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var person = contributors[key];
delete person.email;
res.push(stringify(person));
}
return res;
}
function parsePersons(arr) {
var res = {};
for (var i = 0; i < arr.length; i++) {
var val = arr[i];
if (typeof val !== 'string') {
res[val.name] = val;
continue;
}
var obj = parse(val);
res[obj.name] = obj;
}
return res;
}
function owner(app) {
return utils.through.obj(function(file, enc, next) {
if (file.basename !== 'package.json') {
next();
return;
}
var pkg = JSON.parse(file.contents.toString());
utils.updateOwner(app, pkg);
file.contents = new Buffer(JSON.stringify(pkg, null, 2));
next(null, file);
});
}