-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatefile.js
163 lines (139 loc) · 4.53 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
'use strict';
var path = require('path');
var defaults = require('./templates/defaults');
var utils = require('./utils');
module.exports = function(app) {
if (!utils.isValid(app, 'updater-appveyor')) return;
var ctx = {paths: {}, tasks: []};
var calculated;
/**
* Register a Generate generator, for creating a new `appveyor.yml` file
* when specified by the user.
*/
app.register('generate-appveyor', require('generate-appveyor'));
/**
* Delete the `appveyor.yml` file in the current working directory. This task is also aliased
* as `appveyor:appveyor-del` to free up the `new` task name in case you use this updater as
* a [plugin](#api).
*
* ```sh
* $ update appveyor:del
* ```
* @name appveyor:del
* @api public
*/
app.task('del', ['appveyor-del']);
app.task('appveyor-del', function(cb) {
utils.del(['appveyor.yml'], {cwd: app.cwd}, cb);
});
/**
* Add a new `appveyor.yml` file using [templates/appveyor.yml](template/appveyor.yml) as a template.
* This task is also aliased as `appveyor:appveyor-new` to free up the `new` task name in case you use this
* updater as a [plugin](#api).
*
* ```sh
* $ update appveyor:new
* ```
* @name appveyor:new
* @api public
*/
app.task('new', ['appveyor-new']);
app.task('appveyor-new', ['paths'], function(cb) {
app.generate('generate-appveyor:appveyor', cb);
});
/**
* Update an existing `appveyor.yml` file with the defaults in [templates/defaults.js](template/defaults.js).
* Aliased as `appveyor:appveyor-update` to free up the `update` task name in case you use this
* updater as a [plugin](#api).
*
* ```sh
* $ update appveyor:update
* ```
* @name appveyor:update
* @api public
*/
app.task('update', ['appveyor-update']);
app.task('appveyor-update', ['paths'], function() {
var srcBase = app.options.srcBase || app.cwd;
return app.src('appveyor.yml', {cwd: srcBase, dot: true})
.pipe(appveyorUpdate(app))
.pipe(app.dest(app.cwd));
});
/**
* The default task calls the other tasks to either update an existing `appveyor.yml` file or
* add a new `appveyor.yml` file if one doesn't already exist. Aliased as `appveyor:appveyor` so you
* can use this updater as a [plugin](#api) and safely overwrite the `default` task.
*
* ```sh
* $ update appveyor
* ```
* @name appveyor
* @api public
*/
app.task('default', ['appveyor']);
app.task('appveyor', ['paths'], function(cb) {
app.build(ctx.tasks, cb);
});
/**
* Calculate build paths
*/
app.task('paths', {silent: true}, function(cb) {
calculatePaths();
cb();
});
function calculatePaths() {
if (calculated) return ctx.tasks;
calculated = true;
var cwd = path.resolve.bind(path, app.options.dest || app.cwd);
var hasAppveyor = utils.exists(cwd('appveyor.yml'));
var hasTests = utils.exists(cwd('test')) || utils.exists(cwd('test.js'));
ctx.tasks = hasTests && hasAppveyor ? ['appveyor-update'] : [hasTests ? 'appveyor-new' : 'appveyor-del'];
return ctx.tasks;
}
};
/**
* Update `appveyor.yml`
*/
function appveyorUpdate(app) {
var opts = utils.extend({}, app.options);
return utils.through.obj(function(file, enc, next) {
var engines = app.pkg.get('engines.node') || '0.10.0';
var obj = utils.extend({}, defaults);
// only pass in actual versions
var arr = obj.environment.matrix.map(function(item) {
return item.nodejs_version;
});
// convert versions to objects for appveyor
obj.environment.matrix = utils.updateEngine(engines, arr).map(function(v) {
return {nodejs_version: v};
});
if (opts.merge) {
// keep existing and merge in missing versions
var existing = utils.yaml.safeLoad(file.contents.toString());
if (existing.environment && existing.environment.matrix) {
var from = obj.environment.matrix;
var to = existing.environment.matrix;
from.forEach(function(item) {
if (!utils.contains(to, item.nodejs_version)) {
to.push(item);
}
});
to.sort(function(a, b) {
if (a.nodejs_version > b.nodejs_version) return -1;
if (a.nodejs_version < b.nodejs_version) return 1;
return 0;
});
}
obj = utils.extend({}, obj, existing);
}
file.contents = new Buffer(utils.yaml.dump(obj));
if (opts.delete === false) {
next(null, file);
return;
}
utils.del(file.path, function(err) {
if (err) return next(err);
next(null, file);
});
});
}