This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
utils.js
75 lines (63 loc) · 2.12 KB
/
utils.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
const { compile } = require('ejs');
const { readFileSync } = require('fs-extra');
const { stringify } = require('javascript-stringify');
const { join } = require('path');
const RE_INDENT = /^(?!\s*$)/gm;
const isYarn =
process.env.npm_config_user_agent &&
process.env.npm_config_user_agent.includes('yarn');
const cli = isYarn ? 'yarn' : 'npm';
const yarnReplacers = new Map();
const npmReplacers = new Map();
yarnReplacers.set(/^run /, '');
yarnReplacers.set(/^install /, 'add ');
yarnReplacers.set(/--save-dev/, '--dev');
npmReplacers.set(/--dev/, '--save-dev');
npmReplacers.set(/^add /, 'install ');
npmReplacers.set(/--fix/, '-- --fix');
const packageManager = (command, registry) => {
if (!command) {
return cli;
}
const formatted = [...(isYarn ? yarnReplacers : npmReplacers)].reduce(
(command, [matcher, replacement]) =>
matcher.test(command) ? command.replace(matcher, replacement) : command,
command,
);
return registry
? `${cli} --registry ${registry} ${formatted}`
: `${cli} ${formatted}`;
};
const packageLint = (jsx = false, vue = false, test = false) => {
// The list of extensions here needs to be kept in sync with the extension
// list defined by neutrino/extensions.source. Modifying a value here should
// have an accompanying change there as well. We can't pull in neutrino here
// as that would potentially give us conflicting versions in node_modules.
const exts = ['mjs', vue && 'vue', jsx && 'jsx', 'js']
.filter(Boolean)
.join(',');
const dirs = ['src', test && 'test'].filter(Boolean).join(' ');
return {
scripts: {
lint: `eslint --cache --format codeframe --ext ${exts} ${dirs}`,
},
};
};
const rcTemplate = compile(
readFileSync(
join(__dirname, 'templates/neutrino/.neutrinorc.js.ejs'),
).toString(),
);
const getNeutrinorcOptions = (name, project) => {
if (!project.options) {
return '';
}
const [head, ...tail] = stringify(project.options(name), null, 2).split('\n');
return `${head}\n${tail.join('\n').replace(RE_INDENT, ' ')}`;
};
module.exports = {
getNeutrinorcOptions,
packageLint,
packageManager,
rcTemplate,
};