forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.ts
More file actions
205 lines (184 loc) · 4.11 KB
/
add.ts
File metadata and controls
205 lines (184 loc) · 4.11 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
/**
* Interactive script to create a change file
*
* Usage:
* node scripts/changes/add.ts
*/
import * as fs from "node:fs";
import * as path from "node:path";
import prompts from "prompts";
import { getAllPackageDirNames, getPackagePath } from "../utils/packages.ts";
// Common English stop words that add no meaning to a filename slug
const STOP_WORDS = new Set([
"a",
"an",
"the",
"and",
"or",
"but",
"so",
"nor",
"yet",
"in",
"on",
"at",
"by",
"for",
"to",
"of",
"from",
"with",
"into",
"onto",
"about",
"as",
"via",
"is",
"are",
"was",
"were",
"be",
"been",
"being",
"have",
"has",
"had",
"do",
"does",
"did",
"it",
"its",
"this",
"that",
"these",
"those",
"we",
"us",
"our",
"i",
"me",
"my",
"you",
"your",
"he",
"him",
"his",
"she",
"her",
"they",
"them",
"their",
"now",
"then",
"also",
"just",
]);
const bumpTypes = ["patch", "minor", "major", "unstable"] as const;
interface Package {
dirName: string;
name: string;
}
console.log("\nCreate a change file\n");
let packages = getPackages();
// Abort cleanly on Ctrl-C
let cancelled = false;
const onCancel = () => {
cancelled = true;
return false; // stops prompts from throwing
};
// 1. Select packages
const { selectedPackages } = await prompts(
{
type: "multiselect",
name: "selectedPackages",
message: "Select packages",
choices: packages.map((pkg) => ({ title: pkg.name, value: pkg })),
min: 1,
hint: "Space to select, arrow keys to navigate, Enter to confirm",
},
{ onCancel },
);
if (cancelled || !selectedPackages) process.exit(0);
// 2. Select bump type
const { bump } = await prompts(
{
type: "select",
name: "bump",
message: "Change type",
choices: bumpTypes.map((t) => ({ title: t, value: t })),
initial: 0,
},
{ onCancel },
);
if (cancelled || bump == null) process.exit(0);
// 3. Description
const { description } = await prompts(
{
type: "text",
name: "description",
message: "Description",
validate: (v: string) =>
v.trim().length > 0 ? true : "Description cannot be empty",
},
{ onCancel },
);
if (cancelled || description == null) process.exit(0);
// 4. Derive slug and write files
let slug = toSlug(description.trim());
let fileName = `${bump}.${slug}.md`;
console.log();
for (let pkg of selectedPackages as Package[]) {
let changesDir = path.join(getPackagePath(pkg.dirName), ".changes");
let filePath = path.join(changesDir, fileName);
if (!fs.existsSync(changesDir)) {
fs.mkdirSync(changesDir, { recursive: true });
}
if (fs.existsSync(filePath)) {
console.warn(
`⚠️ File already exists, skipping: packages/${pkg.dirName}/.changes/${fileName}`,
);
continue;
}
fs.writeFileSync(filePath, description.trim() + "\n", "utf-8");
console.log(`✅ ${pkg.name}: packages/${pkg.dirName}/.changes/${fileName}`);
}
console.log();
// --- Utils ---
function getPackages(): Package[] {
return getAllPackageDirNames()
.map((dirName) => {
let pkgJsonPath = path.join(getPackagePath(dirName), "package.json");
if (!fs.existsSync(pkgJsonPath)) return null;
let { name } = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
return { dirName, name } as Package;
})
.filter((p): p is Package => p !== null)
.sort((a, b) => {
const order = (name: string) => {
if (name === "react-router") return 0;
if (name === "react-router-dom") return 1;
if (name.startsWith("@react-router/")) return 2;
return 3;
};
const oa = order(a.name);
const ob = order(b.name);
if (oa !== ob) return oa - ob;
return a.name.localeCompare(b.name);
});
}
/**
* Converts a free-text description into a kebab-case slug of at most 6
* meaningful words. Stop words and non-alphanumeric characters are stripped.
*/
function toSlug(description: string): string {
return (
description
.toLowerCase()
.replace(/[^a-z0-9\s]/g, "")
.trim()
.split(/\s+/)
.filter((w) => !STOP_WORDS.has(w))
.slice(0, 6)
.join("-") || "change"
);
}