Repository navigation
Expand file tree
/
Copy pathmod_script.js
More file actions
287 lines (262 loc) · 8.95 KB
/
Copy pathmod_script.js
File metadata and controls
287 lines (262 loc) · 8.95 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* 1042 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var path = __webpack_require__(1);
var fs = __webpack_require__(12);
var os = __webpack_require__(66);
// ---- script storage ----
var LONG_PROJECT = '.yarn-scripts.json';
var LONG_GLOBAL = 'yarn-scripts.json';
var TMP_PROJECT = '.yarn-scripts-tmp.json';
var TMP_GLOBAL = 'yarn-scripts-tmp.json';
function globalDir() {
return os.homedir();
}
// Sanitize: strip invisible / non-printable characters that may be injected
// into argv (zero-width spaces, BOM, nbsp, control chars, etc.). A no-op on
// clean ASCII, so it never changes legitimate input.
function cleanStr(s) {
if (typeof s !== 'string') return s;
return s.replace(/[^\u0020-\u007E]/g, '');
}
function globalLongFile() { return path.join(globalDir(), LONG_GLOBAL); }
function globalTmpFile() { return path.join(globalDir(), TMP_GLOBAL); }
function projectLongFile(cwd) { return path.join(cwd, LONG_PROJECT); }
function projectTmpFile(cwd) { return path.join(cwd, TMP_PROJECT); }
function readJson(file) {
try {
if (fs.existsSync(file)) {
return JSON.parse(fs.readFileSync(file, 'utf8'));
}
} catch (e) {}
return {};
}
function writeJson(file, obj) {
fs.writeFileSync(file, JSON.stringify(obj, null, 2), 'utf8');
}
function allScripts(cwd, isGlobal) {
var result = {};
var sources = [
[projectTmpFile(cwd), 'tmp-project'],
[globalTmpFile(), 'tmp-global'],
[projectLongFile(cwd), 'project'],
[globalLongFile(), 'global']
];
sources.forEach(function(s) {
var data = readJson(s[0]);
Object.keys(data).forEach(function(k) {
// first-wins so project tmp > global tmp > project longterm > global longterm
if (!result[k]) {
result[k] = { cmd: data[k], src: s[1] };
}
});
});
return result;
}
function listScripts(config, reporter, flags) {
var cwd = config.cwd;
var isGlobal = flags.global;
var showAll = flags.all || flags.la || flags.lsall;
var showTmp = flags.tmp || flags.lt || flags.lstmp;
// -all and -tmp are mutually exclusive; when both are given, tmp takes priority.
if (showAll && showTmp) {
reporter.warn('-all and -tmp are mutually exclusive; -tmp takes priority.');
showAll = false;
}
if (showAll) {
var printed = [];
var sources = [
[projectTmpFile(cwd), 'project tmp'],
[globalTmpFile(), 'global tmp'],
[projectLongFile(cwd), 'project longterm'],
[globalLongFile(), 'global longterm']
];
sources.forEach(function(s) {
var data = readJson(s[0]);
var keys = Object.keys(data);
if (keys.length) {
printed.push('');
printed.push('[' + s[1] + ']');
keys.forEach(function(k) { printed.push(' ' + k + ': ' + data[k]); });
}
});
if (!printed.length) {
reporter.info('No scripts defined.');
} else {
reporter.log(printed.join('\n'), { force: true });
}
return Promise.resolve(0);
}
if (showTmp) {
var printed2 = [];
[projectTmpFile(cwd), globalTmpFile()].forEach(function(f) {
var data = readJson(f);
var keys = Object.keys(data);
if (keys.length) {
keys.forEach(function(k) { printed2.push(' ' + k + ': ' + data[k]); });
}
});
if (!printed2.length) {
reporter.info('No temp scripts defined.');
} else {
reporter.log(printed2.join('\n'), { force: true });
}
return Promise.resolve(0);
}
// default: list longterm scripts in scope order
var longSources = isGlobal
? [[globalLongFile(), 'global longterm'], [globalTmpFile(), 'global tmp']]
: [[projectLongFile(cwd), 'project longterm'], [globalLongFile(), 'global longterm']];
var printed3 = [];
longSources.forEach(function(s) {
var data = readJson(s[0]);
var keys = Object.keys(data);
if (keys.length) {
keys.forEach(function(k) { printed3.push(' ' + k + ': ' + data[k]); });
}
});
if (!printed3.length) {
reporter.info('No scripts defined. Use "yarn script add <name> <cmd>".');
} else {
reporter.log(printed3.join('\n'), { force: true });
}
return Promise.resolve(0);
}
function run(config, reporter, flags, args) {
var action = cleanStr(args[0]);
var rest = args.slice(1);
var cwd = config.cwd;
var isGlobal = flags.global;
if (!action) {
return listScripts(config, reporter, flags);
}
if (action === 'add' || action === 'ad') {
var name = cleanStr(rest[0]);
var cmd = rest.slice(1).join(' ');
if (!name || !cmd) {
throw new Error('Usage: yarn script add <name> <cmd>');
}
var file = isGlobal ? globalLongFile() : projectLongFile(cwd);
var data = readJson(file);
var force = flags.force || flags.Force;
if (!force && Object.prototype.hasOwnProperty.call(data, name)) {
reporter.error('Script "' + name + '" already exists. Use -Force to overwrite.');
process.exitCode = 1;
return Promise.resolve(1);
}
data[name] = cmd;
writeJson(file, data);
reporter.success('Added script "' + name + '" -> ' + cmd);
return Promise.resolve(0);
}
if (action === 'addtemp' || action === 'adtmp') {
var name2 = cleanStr(rest[0]);
var cmd2 = rest.slice(1).join(' ');
if (!name2 || !cmd2) {
throw new Error('Usage: yarn script addtemp <name> <cmd>');
}
var file2 = isGlobal ? globalTmpFile() : projectTmpFile(cwd);
var data2 = readJson(file2);
var force2 = flags.force || flags.Force;
if (!force2 && Object.prototype.hasOwnProperty.call(data2, name2)) {
reporter.error('Temp script "' + name2 + '" already exists. Use -Force to overwrite.');
process.exitCode = 1;
return Promise.resolve(1);
}
data2[name2] = cmd2;
writeJson(file2, data2);
reporter.success('Added temp script "' + name2 + '" -> ' + cmd2);
return Promise.resolve(0);
}
if (action === 'list' || action === 'ls') {
return listScripts(config, reporter, flags);
}
if (action === 'remove' || action === 'delete' || action === 'rm' || action === 'del') {
var name3 = cleanStr(rest[0]);
if (!name3) {
throw new Error('Usage: yarn script remove <name>');
}
var removed = false;
var filesToCheck = isGlobal
? [globalLongFile(), globalTmpFile()]
: [projectLongFile(cwd), projectTmpFile(cwd)];
filesToCheck.forEach(function(f) {
var d = readJson(f);
if (Object.prototype.hasOwnProperty.call(d, name3)) {
delete d[name3];
writeJson(f, d);
removed = true;
}
});
if (removed) {
reporter.success('Removed script "' + name3 + '"');
return Promise.resolve(0);
} else {
reporter.error('Script "' + name3 + '" not found.');
process.exitCode = 1;
return Promise.resolve(1);
}
}
if (action === 'removetemp' || action === 'deltemp' || action === 'rmtmp' || action === 'deltmp') {
var name4 = cleanStr(rest[0]);
if (!name4) {
throw new Error('Usage: yarn script removetemp <name>');
}
var removed2 = false;
var filesToCheck2 = isGlobal ? [globalTmpFile()] : [projectTmpFile(cwd)];
filesToCheck2.forEach(function(f) {
var d = readJson(f);
if (Object.prototype.hasOwnProperty.call(d, name4)) {
delete d[name4];
writeJson(f, d);
removed2 = true;
}
});
if (removed2) {
reporter.success('Removed temp script "' + name4 + '"');
return Promise.resolve(0);
} else {
reporter.error('Temp script "' + name4 + '" not found.');
process.exitCode = 1;
return Promise.resolve(1);
}
}
// otherwise, try to run a script by name
var all = allScripts(cwd, isGlobal);
if (Object.prototype.hasOwnProperty.call(all, action)) {
var entry = all[action];
reporter.command(entry.cmd);
var _execCommand = __webpack_require__(100);
return _execCommand.execCommand({
stage: 'script:' + action,
config: config,
cmd: entry.cmd,
cwd: cwd,
isInteractive: true
});
}
reporter.error('Unknown script action: ' + action);
process.exitCode = 1;
return Promise.resolve(1);
}
function setFlags(commander) {
commander.description('Manage custom yarn scripts. (modyarn extension)');
commander.usage('script [add|addtemp|list|remove|removetemp] [args] [flags]');
// Short single-letter + original-spelling long forms; case-insensitive; only registered for this command.
commander.option('-F, --Force', 'force overwrite');
commander.option('-a, --all', 'list all scripts');
commander.option('-lsall, --lsall', 'list all scripts (alias of -a)');
commander.option('-la, --la', 'list all scripts (alias of -a)');
commander.option('-t, --tmp', 'list temp scripts');
commander.option('-lstmp, --lstmp', 'list temp scripts (alias of -t)');
commander.option('-lt, --lt', 'list temp scripts (alias of -t)');
commander.option('-g, --global', 'use global scope');
}
function hasWrapper(commander, args) {
return false;
}
exports.run = run;
exports.setFlags = setFlags;
exports.hasWrapper = hasWrapper;
/***/ }),