/
updatefile.js
167 lines (146 loc) · 4.19 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
'use strict';
var path = require('path');
var isValid = require('is-valid-app');
var choose = require('gulp-choose-files');
var through = require('through2');
var utils = require('./utils');
var list = require('./list');
var Update = require('..');
var argv = require('yargs-parser')(process.argv.slice(2), utils.opts);
module.exports = function(app, base) {
if (!isValid(app, 'update-builtins')) return;
var gm = path.resolve.bind(path, require('global-modules'));
var cwd = path.resolve.bind(path, app.cwd);
/**
* Select the updaters to run every time `update` is run. Use `--add` to
* add additional updaters, and `--remove` to remove them. You can run this command
* whenever you want to update your preferences, like after installing new updaters.
*
* ```sh
* $ update init
* ```
* @name init
* @api public
*/
app.task('init', { silent: true }, function() {
var list = Update.resolveTasks(app, argv);
var updaters = [];
console.log();
console.log(' Current updaters:', app.log.cyan(list.join(', ')));
console.log();
return app.src([gm('updater-*'), cwd('node_modules/updater-*')])
.pipe(through.obj(function(file, enc, next) {
file.basename = app.toAlias(file.basename);
next(null, file);
}))
.pipe(choose({message: 'Choose the updaters to run with the `update` command:'}))
.pipe(through.obj(function(file, enc, next) {
updaters.push(file.basename);
next();
}, function(next) {
save(app, updaters);
next();
}));
});
/**
* Display a list of currently installed updaters.
*
* ```sh
* $ update defaults:list
* # aliased as
* $ update list
* ```
* @name list
* @api public
*/
app.task('list', { silent: true }, function() {
return app.src([gm('updater-*'), cwd('node_modules/updater-*')])
.pipe(through.obj(function(file, enc, next) {
file.alias = app.toAlias(file.basename);
next(null, file);
}))
.pipe(list(app));
});
/**
* Display a help [menu](#help-menu) of available commands and flags.
*
* ```sh
* $ update defaults:help
* # aliased as
* $ update help
* ```
* @name help
* @api public
*/
app.task('help', { silent: true }, function(cb) {
base.cli.process({ help: true }, cb);
});
/**
* Show the list of updaters that are registered to run on the current project.
*
* ```sh
* $ update defaults:show
* # aliased as
* $ update show
* ```
* @name show
* @api public
*/
app.task('show', { silent: true }, function(cb) {
argv._ = [];
var list = Update.resolveTasks(app, argv);
console.log();
console.log(' queued updaters:', `\n · ` + list.join('\n · '));
console.log();
cb();
});
/**
* Default task for the built-in `defaults` generator.
*
* ```sh
* $ update help
* ```
* @name help
* @api public
*/
app.task('default', { silent: true }, ['help']);
};
/**
* Save answers to `init` prompts
*/
function save(app, list) {
if (!list.length) {
console.log(' no updaters were saved.');
return;
}
if (app.options.c || app.options.config) {
app.pkg.set('update.updaters', list);
} else {
app.globals.set('updaters', list);
}
var suffix = list.length > 1 ? 'updaters are' : 'updater is';
var gray = app.log.gray;
var cyan = app.log.cyan;
var bold = app.log.bold;
var command = 'update';
var msg = gray('\n ---')
+ '\n'
+ `\n ${app.log.green(app.log.check)} Done, your default ${suffix}:`
+ '\n'
+ cyan(`\n · ` + list.join('\n · '))
+ '\n'
+ '\n'
+ gray(' ---')
+ '\n'
+ '\n'
+ bold(' Cheetsheet:')
+ '\n'
+ '\n'
+ (` $ ${command} --init`) + gray(' initialize update (start over)\n')
+ (` $ ${command} --remove <names>`) + gray(' remove updaters from your queue\n')
+ (` $ ${command} --add <names>`) + gray(' add updaters to your queue\n')
+ (` $ ${command} show`) + gray(' show your queued updaters\n')
+ (` $ ${command} list`) + gray(' list all installed updaters\n')
console.log(msg);
console.log();
}