-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (112 loc) · 3.22 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
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
const { Transform } = require('stream');
const logger = require('gulplog');
const PluginError = require('plugin-error');
const path = require('path');
const dargs = require('dargs');
const { default: PQueue } = require('p-queue');
const commandRunner = require('./lib/command-runner');
const PLUGIN_NAME = 'gulp-reinstall';
const installCommands = {
tsd: ['reinstall', '--save'],
bower: ['install', '--config.interactive=false'],
npm: ['install'],
pip: ['install', '-r', 'requirements.txt'],
composer: ['install'],
typings: ['install'],
};
const defaultFileToCommand = {
'tsd.json': 'tsd',
'bower.json': 'bower',
'package.json': 'npm',
'requirements.txt': 'pip',
'composer.json': 'composer',
'typings.json': 'typings',
};
const noop = () => {};
function transfob(transform, flush) {
const t2 = new Transform({
objectMode: true,
});
// eslint-disable-next-line no-underscore-dangle
t2._transform = transform;
if (flush) {
// eslint-disable-next-line no-underscore-dangle
t2._flush = flush;
}
return t2;
}
function gulpReinstall(opts = {}, done = noop) {
if (typeof opts === 'function') {
// eslint-disable-next-line no-param-reassign
done = opts;
// eslint-disable-next-line no-param-reassign
opts = {};
}
const fileToCommand = { ...defaultFileToCommand, ...opts.commands };
const queue = new PQueue({ concurrency: 1 });
function generateCommand(file) {
const installCmd = fileToCommand[path.basename(file.path)];
if (installCmd) {
const cmd = {
cmd: installCmd,
args: (installCommands[installCmd] || []).slice(),
};
if (['bower', 'npm'].includes(cmd.cmd) && opts.production) {
cmd.args.push('--production');
}
if (cmd.cmd === 'npm' && opts.ignoreScripts) {
cmd.args.push('--ignore-scripts');
}
if (opts.args) {
cmd.args = cmd.args.concat(opts.args).map((arg) => arg.toString());
}
if (Array.isArray(opts[cmd.cmd])) {
cmd.args = cmd.args.concat(opts[cmd.cmd].map((arg) => arg.toString()));
} else if (typeof opts[cmd.cmd] === 'object') {
cmd.args = cmd.args.concat(dargs(opts[cmd.cmd]));
} else if (opts[cmd.cmd]) {
cmd.args = cmd.args.concat(opts[cmd.cmd].toString());
}
if (cmd.cmd === 'bower' && opts.allowRoot) {
cmd.args.push('--allow-root');
}
if (cmd.cmd === 'npm' && opts.noOptional) {
cmd.args.push('--no-optional');
}
cmd.cwd = path.dirname(file.path);
return cmd;
}
return false;
}
function runCommand(cmd, cb, file) {
(async () => {
await queue.add(async () => {
try {
await commandRunner.run(cmd);
cb(null, file);
} catch (error) {
cb(new PluginError(PLUGIN_NAME, error), file);
}
});
})();
}
return transfob(
(file, enc, cb) => {
if (!file.path) {
return cb(null, file);
}
const cmd = generateCommand(file);
if (cmd) {
return runCommand(cmd, cb);
}
logger.warn('File %s is not supported', file);
return cb(null, file);
},
async (cb) => {
await queue.onIdle();
done();
cb();
}
);
}
module.exports = gulpReinstall;