-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
create-rule.mjs
173 lines (154 loc) · 3.8 KB
/
create-rule.mjs
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
#!/usr/bin/env node
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import enquirer from 'enquirer';
import {template} from 'lodash-es';
import execa from 'execa';
const dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.join(dirname, '..');
function checkFiles(ruleId) {
const files = [
`docs/rules/${ruleId}.md`,
`rules/${ruleId}.js`,
`test/${ruleId}.mjs`,
`test/snapshots/${ruleId}.mjs.md`,
`test/snapshots/${ruleId}.mjs.snap`,
];
for (const file of files) {
if (fs.existsSync(path.join(ROOT, file))) {
throw new Error(`“${file}” already exists.`);
}
}
}
function renderTemplate({source, target, data}) {
const templateFile = path.join(dirname, `template/${source}`);
const targetFile = path.join(ROOT, target);
const templateContent = fs.readFileSync(templateFile, 'utf8');
const compiled = template(templateContent);
const content = compiled(data);
return fs.writeFileSync(targetFile, content);
}
function updateRecommended(id) {
const RULE_START = '\n\trules: {\n';
const RULE_END = '\n\t}';
const RULE_INDENT = '\t'.repeat(2);
let ruleContent = `${RULE_INDENT}'unicorn/${id}': 'error',`;
const file = path.join(ROOT, 'configs/recommended.js');
const content = fs.readFileSync(file, 'utf8');
const [before, rest] = content.split(RULE_START);
const [rules, after] = rest.split(RULE_END);
const lines = rules.split('\n');
if (!lines.every(line => line.startsWith(RULE_INDENT))) {
throw new Error('Unexpected content in “configs/recommended.js”.');
}
const unicornRuleLines = lines.filter(line => line.startsWith(`${RULE_INDENT}'unicorn/`));
let insertIndex;
if (ruleContent.localeCompare(unicornRuleLines[0]) === -1) {
insertIndex = 0;
} else if (ruleContent.localeCompare(unicornRuleLines[unicornRuleLines.length - 1]) === 1) {
insertIndex = lines.length;
lines[lines.length - 1] += ',';
ruleContent = ruleContent.slice(0, -1);
} else {
const lineBefore = unicornRuleLines[
unicornRuleLines.findIndex(line => line.localeCompare(ruleContent) === 1) - 1
];
insertIndex = lines.indexOf(lineBefore) + 1;
}
lines.splice(insertIndex, 0, ruleContent);
const updated = `${before}${RULE_START}${lines.join('\n')}${RULE_END}${after}`;
fs.writeFileSync(file, updated);
}
(async () => {
const data = await enquirer.prompt([
{
type: 'input',
name: 'id',
message: 'Rule name:',
validate(value) {
if (!value) {
return 'Rule name is required.';
}
if (!/^[a-z-]+$/.test(value)) {
return 'Invalid rule name.';
}
return true;
},
},
{
type: 'input',
name: 'description',
message: 'Rule description:',
validate(value) {
if (!value) {
return 'Rule description is required.';
}
return true;
},
},
{
type: 'select',
name: 'fixableType',
message: 'Is it fixable?',
choices: [
{
message: 'Code',
value: 'code',
},
{
message: 'Whitespace',
value: 'whitespace',
},
{
message: 'No',
value: false,
},
],
},
{
type: 'select',
name: 'type',
message: 'Type:',
choices: [
'problem',
'suggestion',
'layout',
],
},
]);
if (data.fixableType === 'No') {
data.fixableType = false;
}
const {id} = data;
checkFiles(id);
renderTemplate({
source: 'documentation.md.jst',
target: `docs/rules/${id}.md`,
data,
});
renderTemplate({
source: 'rule.js.jst',
target: `rules/${id}.js`,
data,
});
renderTemplate({
source: 'test.mjs.jst',
target: `test/${id}.mjs`,
data,
});
updateRecommended(id);
try {
await execa('code', [
'--new-window',
'.',
`docs/rules/${id}.md`,
`rules/${id}.js`,
`test/${id}.mjs`,
], {cwd: ROOT});
} catch {}
})().catch(error => {
console.error(error);
process.exit(1);
});