-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
updatefile.js
196 lines (165 loc) · 4.35 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
'use strict';
var del = require('delete');
var ask = require('helper-ask');
var Engine = require('engine');
var utils = require('./utils');
module.exports = function(app) {
if (!utils.isValid(app, 'updater-license')) return;
/**
* Helper
*/
app.asyncHelper('ask', ask(app));
/**
* Register a generator for creating a new `LICENSE` file
* when needed
*/
app.register('new', require('generate-license'));
/**
* Delete the existing `LICENSE` or `LICENSE-MIT` file in the current working directory.
* This task is also aliased as `license:license-del` to free up the `del` task name in case
* you use this generator as a [plugin](#api).
*
* ```sh
* $ update license:del
* ```
* @name license:del
* @api public
*/
app.task('del', ['license-del']);
app.task('license-del', function(cb) {
utils.del(['LICENSE', 'LICENSE-MIT'], done(app, cb));
});
/**
* Create a new `LICENSE` file in the current working directory from the [mit template](templates/license-mit.tmpl). _(This task is also aliased as `license:license-new` to free up the `new` task
* name in case you use this generator as a [plugin](#api))_.
*
* ```sh
* $ update license:new
* ```
* @name license:new
* @api public
*/
app.task('new', ['license-new']);
app.task('license-new', function(cb) {
app.generate('new', cb);
});
/**
* Update the MIT `LICENSE` file in the current working directory. Uses the [mit template](templates/license-mit.tmpl) by default.
*
* ```sh
* $ update license
* ```
* @name license
* @api public
*/
app.task('license', {silent: true}, function(cb) {
app.create('licenses');
app.licenses('templates/*.tmpl', {cwd: __dirname});
return app.src('LICENSE*', {cwd: app.options.srcBase || app.cwd})
.pipe(matter())
.pipe(renameFile())
.pipe(updateLicense(app))
.pipe(app.dest(function(file) {
if (file.basename === 'LICENSE-MIT') {
del.sync(file.path, {force: true});
file.basename = 'LICENSE';
}
return app.cwd;
}));
});
/**
* Alias `license` task to make the updater more shareable
*/
app.task('default', {silent: true}, ['license']);
};
/**
* Update LICENSE using a template
*/
function updateLicense(app) {
return utils.through.obj(function(file, enc, next) {
var filepath = file.path;
var template;
try {
var views = app.licenses.views;
for (var key in views) {
if (views.hasOwnProperty(key)) {
var view = views[key];
rename(view);
if (view.basename === file.basename) {
template = view;
break;
}
}
}
if (typeof template === 'undefined') {
next(null, file);
return;
}
var str = file.contents.toString();
var tok = utils.parse(str);
var latest = utils.copyright(tok.latest.statement, tok.latest);
var lines = [tok.prefix, '', latest];
var len = tok.authors.length;
if (len === 1) {
lines.push('');
}
for (var i = 0; i < len; i++) {
var author = tok.authors[i];
if (author !== tok.latest) {
lines.push(author.statement, '');
}
}
lines.push(tok.license);
file.contents = new Buffer(lines.join('\n'));
} catch (err) {
next(err);
return;
}
utils.del(filepath, function(err) {
if (err) return next(err);
next(null, file);
});
});
}
/**
* Parse front matter
*/
function matter(options) {
return utils.through.obj(function(file, enc, next) {
utils.parser.parse(file, options, next);
});
}
/**
* Rename the vinyl file using properties in front matter
*/
function rename(file) {
file.data.rename = file.data.rename || {};
for (var key in file.data.rename) {
file[key] = file.data.rename[key];
delete file.data.rename;
}
}
/**
* Rename the vinyl file using properties in front matter
*/
function renameFile(file) {
return utils.through.obj(function(file, enc, next) {
rename(file);
next(null, file);
});
}
/**
* Utils
*/
function hasYear(str) {
return str.indexOf(utils.year()) !== -1;
}
function done(app, cb) {
return function(err, files) {
if (err) return cb(err);
if (files.length && app.options.verbose) {
console.log('deleted', files.join(', '));
}
cb();
};
}