-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnew-rule.ts
95 lines (91 loc) · 2.47 KB
/
new-rule.ts
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
import { readFileSync, writeFileSync } from "node:fs";
import prompts, { type PromptObject } from "prompts";
import { compile } from "tempura";
const questions: PromptObject[] = [
{
type: "text",
name: "name",
message: "Name of the new rule.",
validate: (name: string) =>
/[a-z][a-z-]*[a-z]/.test(name)
? true
: "Rule name should contain lowercase latin characters and hyphens only, and should not start/end with a hyphen.",
},
{
type: "text",
name: "description",
message: "Concise description of the rule.",
validate: (description: string) =>
/[A-Z].+\./.test(description)
? true
: "Description should start with an uppercase and end with a period.",
},
{
type: "select",
name: "type",
message: "Type of the rule.",
choices: [
{
title: "Problem",
value: "problem",
description:
"A rule that identifies possible errors or confusing behaviors.",
},
{
title: "Suggestion",
value: "suggestion",
description:
"A rule that identifies something that could be done in a better way.",
},
{
title: "Layout",
value: "layout",
description: "A rule that identifies stylistic issues.",
},
],
},
{
type: "confirm",
name: "hasOption",
message: "Will the new rule have an option?",
initial: false,
},
];
const answer = await prompts(questions);
const targets = [
{
src: "./scripts/templates/doc.md.hbs",
dest: "./docs/rules",
ext: "md",
},
{
src: "./scripts/templates/rule.ts.hbs",
dest: "./src/rules",
ext: "ts",
},
{
src: "./scripts/templates/test.ts.hbs",
dest: "./src/__tests__/rules",
ext: "test.ts",
},
];
if (
["name", "description", "type", "hasOption"].every((key) =>
// @ts-expect-error: typing is missing in built-in
// cf. https://github.com/microsoft/TypeScript/issues/44253
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
Object.hasOwn(key)
)
) {
for (const { src, dest, ext } of targets) {
writeFileSync(
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions --
* The value `answer.name` is guaranteed to be a string because the corresponding prompts'
* question is of type text.
*/
`${dest}/${answer.name}.${ext}`,
await compile(readFileSync(src, { encoding: "utf-8" }))(answer),
{ encoding: "utf-8" }
);
}
}