forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
292 lines (264 loc) · 8.03 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const fsp = require("fs").promises;
const chalk = require("chalk");
const path = require("path");
const { execSync } = require("child_process");
const jsonfile = require("jsonfile");
const Confirm = require("prompt-confirm");
let rootDir = path.resolve(__dirname, "..");
let examplesDir = path.resolve(rootDir, "examples");
let remixPackages = {
adapters: [
"architect",
"cloudflare-pages",
"cloudflare-workers",
"express",
"netlify",
"vercel",
],
runtimes: ["cloudflare", "deno", "node"],
core: ["dev", "server-runtime", "react", "eslint-config"],
get all() {
return [...this.adapters, ...this.runtimes, ...this.core, "serve"];
},
};
/**
* @param {string} packageName
* @param {string} [directory]
* @returns {string}
*/
function packageJson(packageName, directory = "") {
return path.join(rootDir, directory, packageName, "package.json");
}
/**
* @param {string} packageName
* @returns {Promise<string | undefined>}
*/
async function getPackageVersion(packageName) {
let file = packageJson(packageName, "packages");
let json = await jsonfile.readFile(file);
return json.version;
}
/**
* @returns {void}
*/
function ensureCleanWorkingDirectory() {
let status = execSync(`git status --porcelain`).toString().trim();
let lines = status.split("\n");
if (!lines.every((line) => line === "" || line.startsWith("?"))) {
console.error(
"Working directory is not clean. Please commit or stash your changes."
);
process.exit(1);
}
}
/**
* @param {string} question
* @returns {Promise<string | boolean>}
*/
async function prompt(question) {
let confirm = new Confirm(question);
let answer = await confirm.run();
return answer;
}
/**
* @param {string} packageName
* @param {(json: import('type-fest').PackageJson) => any} transform
*/
async function updatePackageConfig(packageName, transform) {
let file = packageJson(packageName, "packages");
try {
let json = await jsonfile.readFile(file);
if (!json) {
console.log(`No package.json found for ${packageName}; skipping`);
return;
}
transform(json);
await jsonfile.writeFile(file, json, { spaces: 2 });
} catch (err) {
return;
}
}
/**
* @param {string} example
* @param {(json: import('type-fest').PackageJson) => any} transform
*/
async function updateExamplesPackageConfig(example, transform) {
let file = packageJson(example, "examples");
if (!(await fileExists(file))) return;
let json = await jsonfile.readFile(file);
transform(json);
await jsonfile.writeFile(file, json, { spaces: 2 });
}
/**
* @param {string} nextVersion
*/
async function updateExamplesRemixVersion(nextVersion) {
let examples = await fsp.readdir(examplesDir);
if (examples.length > 0) {
for (let example of examples) {
let stat = await fsp.stat(path.join(examplesDir, example));
if (!stat.isDirectory()) continue;
await updateExamplesPackageConfig(example, (config) => {
if (config.dependencies?.["remix"]) {
config.dependencies["remix"] = nextVersion;
}
for (let pkg of remixPackages.all) {
if (config.dependencies?.[`@remix-run/${pkg}`]) {
config.dependencies[`@remix-run/${pkg}`] = nextVersion;
}
if (config.devDependencies?.[`@remix-run/${pkg}`]) {
config.devDependencies[`@remix-run/${pkg}`] = nextVersion;
}
}
console.log(
chalk.green(
` Updated Remix to version ${chalk.bold(
nextVersion
)} in ${chalk.bold(example)} example`
)
);
});
}
}
}
/**
* @param {string} packageName
* @param {string} nextVersion
* @param {string} [successMessage]
*/
async function updateRemixVersion(packageName, nextVersion, successMessage) {
await updatePackageConfig(packageName, (config) => {
config.version = nextVersion;
for (let pkg of remixPackages.all) {
if (config.dependencies?.[`@remix-run/${pkg}`]) {
config.dependencies[`@remix-run/${pkg}`] = nextVersion;
}
if (config.devDependencies?.[`@remix-run/${pkg}`]) {
config.devDependencies[`@remix-run/${pkg}`] = nextVersion;
}
if (config.peerDependencies?.[`@remix-run/${pkg}`]) {
config.peerDependencies[`@remix-run/${pkg}`] = nextVersion;
}
}
});
let logName = packageName.startsWith("remix-")
? `@remix-run/${packageName.slice(6)}`
: packageName;
console.log(
chalk.green(
` ${
successMessage ||
`Updated ${chalk.bold(logName)} to version ${chalk.bold(nextVersion)}`
}`
)
);
}
/**
*
* @param {string} nextVersion
*/
async function updateDeploymentScriptVersion(nextVersion) {
let file = packageJson("deployment-test", "scripts");
let json = await jsonfile.readFile(file);
json.dependencies["@remix-run/dev"] = nextVersion;
await jsonfile.writeFile(file, json, { spaces: 2 });
console.log(
chalk.green(
` Updated Remix to version ${chalk.bold(nextVersion)} in ${chalk.bold(
"scripts/deployment-test"
)}`
)
);
}
/**
* @param {string} importSpecifier
* @returns {[string, string]} [packageName, importPath]
*/
const getPackageNameFromImportSpecifier = (importSpecifier) => {
if (importSpecifier.startsWith("@")) {
let [scope, pkg, ...path] = importSpecifier.split("/");
return [`${scope}/${pkg}`, path.join("/")];
}
let [pkg, ...path] = importSpecifier.split("/");
return [pkg, path.join("/")];
};
/**
* @param {string} importMapPath
* @param {string} nextVersion
*/
const updateDenoImportMap = async (importMapPath, nextVersion) => {
let { imports, ...json } = await jsonfile.readFile(importMapPath);
let remixPackagesFull = remixPackages.all.map(
(remixPackage) => `@remix-run/${remixPackage}`
);
let newImports = Object.fromEntries(
Object.entries(imports).map(([importName, path]) => {
let [packageName, importPath] =
getPackageNameFromImportSpecifier(importName);
return remixPackagesFull.includes(packageName)
? [
importName,
`https://esm.sh/${packageName}@${nextVersion}${
importPath ? `/${importPath}` : ""
}`,
]
: [importName, path];
})
);
return jsonfile.writeFile(
importMapPath,
{ ...json, imports: newImports },
{ spaces: 2 }
);
};
/**
* @param {string} nextVersion
*/
async function incrementRemixVersion(nextVersion) {
// Update version numbers in package.json for all packages
await updateRemixVersion("remix", nextVersion);
await updateRemixVersion("create-remix", nextVersion);
for (let name of remixPackages.all) {
await updateRemixVersion(`remix-${name}`, nextVersion);
}
// Update version numbers in Deno's import maps
await Promise.all(
[
path.join(".vscode", "deno_resolve_npm_imports.json"),
path.join("templates", "deno", ".vscode", "resolve_npm_imports.json"),
].map((importMapPath) =>
updateDenoImportMap(path.join(rootDir, importMapPath), nextVersion)
)
);
// Update versions in the examples
await updateExamplesRemixVersion(nextVersion);
// Update deployment script `@remix-run/dev` version
await updateDeploymentScriptVersion(nextVersion);
// Commit and tag
execSync(`git commit --all --message="Version ${nextVersion}"`);
execSync(`git tag -a -m "Version ${nextVersion}" v${nextVersion}`);
console.log(chalk.green(` Committed and tagged version ${nextVersion}`));
}
/**
* @param {string} filePath
* @returns {Promise<boolean>}
*/
async function fileExists(filePath) {
try {
await fsp.stat(filePath);
return true;
} catch (_) {
return false;
}
}
exports.rootDir = rootDir;
exports.examplesDir = examplesDir;
exports.remixPackages = remixPackages;
exports.fileExists = fileExists;
exports.packageJson = packageJson;
exports.getPackageVersion = getPackageVersion;
exports.ensureCleanWorkingDirectory = ensureCleanWorkingDirectory;
exports.prompt = prompt;
exports.updatePackageConfig = updatePackageConfig;
exports.updateRemixVersion = updateRemixVersion;
exports.incrementRemixVersion = incrementRemixVersion;