-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (62 loc) · 1.47 KB
/
index.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
'use strict';
var through = require('through2');
var extend = require('extend-shallow');
var Prompt = require('prompt-checkbox');
module.exports = function(options) {
var opts = extend({key: 'relative', save: false}, options);
var msg = opts.message || 'Which files do you want to write?';
var paths = [];
var files = {};
return through.obj(function(file, enc, next) {
if (opts.skip) {
next(null, file);
return;
}
var key = fileKey(file, opts);
paths.push(key);
files[key] = file;
next();
}, function(next) {
var stream = this;
if (typeof opts.choices === 'string') {
opts.choices = [opts.choices];
}
if (Array.isArray(opts.choices)) {
opts.choices.forEach(function(filepath) {
stream.push(files[filepath]);
});
next();
return;
}
if (paths.length === 0) {
next();
return;
}
var answers = {};
var prompt = new Prompt({
name: 'files',
message: msg,
type: 'checkbox',
choiceObject: true,
radio: true,
choices: paths
});
prompt.run(answers)
.then(function(answers) {
answers.forEach(function(filepath) {
stream.push(files[filepath]);
});
next();
})
.catch(next);
});
};
function fileKey(file, opts) {
if (typeof opts.key === 'string') {
return file[opts.key];
}
if (typeof opts.key === 'function') {
return opts.key(file);
}
return file.relative;
}