-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.js
190 lines (168 loc) · 4.7 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
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
const _ = require('lodash');
const {
json,
packageJson,
lines,
install,
uninstall,
getExtsFromCommand,
} = require('mrm-core');
const getConfigName = (configName, scope, prefix) => {
if (!scope && !configName.startsWith(prefix)) {
return `${prefix}-${configName}`;
} else if (scope && !configName) {
return prefix;
} else {
return configName;
}
};
const normalizePresetPackageName = presetName => {
const prefix = 'eslint-config';
const presetNameRegex = /^(?:(@[^/]+)\/?)?((?:eslint-config-)?[^/]*)(?:\/[^/]+)?$/;
const match = presetName.match(presetNameRegex);
if (!match) {
throw new Error(
`Invalid preset name is passed to the eslint task: ${presetName}`
);
}
const [, scope = '', configNameRaw] = match;
const configName = getConfigName(configNameRaw, scope, prefix);
const packageName = `${scope ? `${scope}/` : ''}${configName}`;
return packageName;
};
module.exports = function task({
eslintPreset,
eslintPeerDependencies = [],
eslintObsoleteDependencies = [],
eslintRules,
}) {
let exts = '';
const legacyConfigFile = '.eslintrc';
const configFile = '.eslintrc.json';
const ignores = ['node_modules/'];
const ignoresToRemove = ['node_modules'];
const gitIgnores = ['.eslintcache'];
const packages = ['eslint'];
const packagesToRemove = ['jslint', 'jshint'];
// Preset
if (eslintPreset !== 'eslint:recommended') {
packages.push(normalizePresetPackageName(eslintPreset));
}
// Peer dependencies
packages.push(...eslintPeerDependencies);
// Migrate legacy config
const legacyEslintrc = json(legacyConfigFile);
const legacyConfig = legacyEslintrc.get();
legacyEslintrc.delete();
// .eslintrc.json
const eslintrc = json(configFile, legacyConfig);
const hasCustomPreset = _.castArray(eslintrc.get('extends', [])).find(x =>
x.startsWith(eslintPreset)
);
if (!hasCustomPreset) {
const presets = eslintrc.get('extends');
if (!presets) {
eslintrc.set('extends', eslintPreset);
} else {
eslintrc.set('extends', [eslintPreset, ..._.castArray(presets)]);
}
}
if (eslintRules) {
eslintrc.merge({
rules: eslintRules,
});
}
const pkg = packageJson();
// TODO: Babel
// Not sure how to detect that we need it, checking for babel-core is not enough because
// babel-eslint is only needed for experimental features and Flow (this one is easy to test)
// Flow also needs this: https://github.com/gajus/eslint-plugin-flowtype
// if (pkg.get('devDependencies.babel-core')) {
// packages.push('babel-eslint');
// eslintrc.set('parser', 'babel-eslint');
// }
// TypeScript
if (pkg.get('devDependencies.typescript')) {
const parser = '@typescript-eslint/parser';
const plugin = '@typescript-eslint/eslint-plugin';
packages.push(parser, plugin);
eslintrc.merge({
parser,
plugins: [plugin],
parserOptions: {
// If using React, turn on JSX support in the TypeScript parser.
...(pkg.get('dependencies.react') && {
ecmaFeatures: {
jsx: true,
},
}),
project: './tsconfig.json',
},
rules: eslintRules || {},
});
exts = ' --ext .ts,.tsx';
if (pkg.get('devDependencies.prettier')) {
packages.push('eslint-config-prettier');
const extensions = eslintrc.get('extends', []);
eslintrc.merge({
extends: [
...(Array.isArray(extensions) ? extensions : [extensions]),
'prettier',
'prettier/@typescript-eslint',
],
});
}
}
eslintrc.save();
// .eslintignore
lines('.eslintignore')
.remove(ignoresToRemove)
.add(ignores)
.save();
// .gitignore
lines('.gitignore')
.add(gitIgnores)
.save();
// Keep custom extensions
const lintScript =
pkg.getScript('lint', 'eslint') || pkg.getScript('test', 'eslint');
if (lintScript) {
const lintExts = getExtsFromCommand(lintScript, 'ext');
if (lintExts && lintExts.toString() !== 'js') {
const extsPattern = lintExts.map(x => `.${x}`).join(',');
exts = ` --ext ${extsPattern}`;
}
}
pkg
// Remove existing JS linters
.removeScript(/^(lint:js|eslint|jshint|jslint)$/)
.removeScript('test', / (lint|lint:js|eslint|jshint|jslint)( |$)/) // npm run jest && npm run lint
.removeScript('test', /\beslint|jshint|jslint\b/) // jest && eslint
// Add lint script
.setScript('lint', 'eslint . --cache --fix' + exts)
// Add pretest script
.prependScript('pretest', 'npm run lint')
.save();
// Dependencies
uninstall([...packagesToRemove, ...eslintObsoleteDependencies]);
install(packages);
};
module.exports.description = 'Adds ESLint';
module.exports.parameters = {
eslintPreset: {
type: 'input',
message: 'Enter ESLint preset name',
default: 'eslint:recommended',
},
eslintPeerDependencies: {
type: 'config',
default: [],
},
eslintObsoleteDependencies: {
type: 'config',
default: [],
},
eslintRules: {
type: 'config',
},
};