create-amytis 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +64 -13
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -14,18 +14,18 @@ function prompt(question, defaultVal = "") {
|
|
|
14
14
|
output: process.stdout
|
|
15
15
|
});
|
|
16
16
|
const display = defaultVal ? `${question} (${defaultVal}): ` : `${question}: `;
|
|
17
|
-
return new Promise((
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
18
|
rl.question(display, (answer) => {
|
|
19
19
|
rl.close();
|
|
20
|
-
|
|
20
|
+
resolve(answer.trim() || defaultVal);
|
|
21
21
|
});
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
function fetchJson(url) {
|
|
25
|
-
return new Promise((
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
26
|
https.get(url, { headers: { "User-Agent": "create-amytis-cli" } }, (res) => {
|
|
27
27
|
if (res.statusCode === 301 || res.statusCode === 302) {
|
|
28
|
-
fetchJson(res.headers.location).then(
|
|
28
|
+
fetchJson(res.headers.location).then(resolve).catch(reject);
|
|
29
29
|
return;
|
|
30
30
|
}
|
|
31
31
|
if (res.statusCode !== 200) {
|
|
@@ -36,7 +36,7 @@ function fetchJson(url) {
|
|
|
36
36
|
res.on("data", (chunk) => data += chunk);
|
|
37
37
|
res.on("end", () => {
|
|
38
38
|
try {
|
|
39
|
-
|
|
39
|
+
resolve(JSON.parse(data));
|
|
40
40
|
} catch {
|
|
41
41
|
reject(new Error("Failed to parse GitHub API response"));
|
|
42
42
|
}
|
|
@@ -45,7 +45,7 @@ function fetchJson(url) {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
function downloadFile(url, dest) {
|
|
48
|
-
return new Promise((
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
49
|
const doGet = (targetUrl) => {
|
|
50
50
|
https.get(targetUrl, { headers: { "User-Agent": "create-amytis-cli" } }, (res) => {
|
|
51
51
|
if (res.statusCode === 301 || res.statusCode === 302) {
|
|
@@ -58,7 +58,7 @@ function downloadFile(url, dest) {
|
|
|
58
58
|
}
|
|
59
59
|
const file = fs.createWriteStream(dest);
|
|
60
60
|
res.pipe(file);
|
|
61
|
-
file.on("finish", () => file.close(() =>
|
|
61
|
+
file.on("finish", () => file.close(() => resolve()));
|
|
62
62
|
file.on("error", (err) => {
|
|
63
63
|
fs.unlink(dest, () => {});
|
|
64
64
|
reject(err);
|
|
@@ -126,6 +126,34 @@ function extractArchive(archivePath, outDir, kind, platform = process.platform)
|
|
|
126
126
|
function escapeTemplateLiteral(str) {
|
|
127
127
|
return str.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
128
128
|
}
|
|
129
|
+
function patchField(src, shapes, field) {
|
|
130
|
+
for (const [pattern, replacement] of shapes) {
|
|
131
|
+
if (pattern.test(src))
|
|
132
|
+
return src.replace(pattern, () => replacement);
|
|
133
|
+
}
|
|
134
|
+
throw new Error(`create-amytis is out of sync with the downloaded site.config.ts: ` + `could not find the "${field}" field to patch. Edit site.config.ts manually, ` + `and please report this at https://github.com/hutusi/amytis/issues`);
|
|
135
|
+
}
|
|
136
|
+
function applyStarterConfig(projectDir) {
|
|
137
|
+
const starterPath = path.join(projectDir, "site.config.example.ts");
|
|
138
|
+
if (!fs.existsSync(starterPath))
|
|
139
|
+
return false;
|
|
140
|
+
fs.copyFileSync(starterPath, path.join(projectDir, "site.config.ts"));
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
var LOCALE_DIR_SHAPE = /^[a-z]{2}(-[A-Z]{2})?$/;
|
|
144
|
+
function removeLocaleTrees(projectDir) {
|
|
145
|
+
const contentDir = path.join(projectDir, "content");
|
|
146
|
+
if (!fs.existsSync(contentDir))
|
|
147
|
+
return [];
|
|
148
|
+
const removed = [];
|
|
149
|
+
for (const entry of fs.readdirSync(contentDir, { withFileTypes: true })) {
|
|
150
|
+
if (!entry.isDirectory() || !LOCALE_DIR_SHAPE.test(entry.name))
|
|
151
|
+
continue;
|
|
152
|
+
fs.rmSync(path.join(contentDir, entry.name), { recursive: true, force: true });
|
|
153
|
+
removed.push(`content/${entry.name}/`);
|
|
154
|
+
}
|
|
155
|
+
return removed;
|
|
156
|
+
}
|
|
129
157
|
function patchSiteConfig(projectDir, title, description) {
|
|
130
158
|
const configPath = path.join(projectDir, "site.config.ts");
|
|
131
159
|
if (!fs.existsSync(configPath)) {
|
|
@@ -133,10 +161,24 @@ function patchSiteConfig(projectDir, title, description) {
|
|
|
133
161
|
return;
|
|
134
162
|
}
|
|
135
163
|
let src = fs.readFileSync(configPath, "utf8");
|
|
136
|
-
|
|
137
|
-
|
|
164
|
+
const quotedTitle = JSON.stringify(title);
|
|
165
|
+
const quotedDescription = JSON.stringify(description);
|
|
138
166
|
const safeTitle = escapeTemplateLiteral(title);
|
|
139
|
-
src = src
|
|
167
|
+
src = patchField(src, [
|
|
168
|
+
[/^ {2}title:\s*\{\s*en:\s*"[^"]*",\s*zh:\s*"[^"]*"\s*\}/m, ` title: { en: ${quotedTitle}, zh: ${quotedTitle} }`],
|
|
169
|
+
[/^ {2}title:\s*"[^"]*"/m, ` title: ${quotedTitle}`]
|
|
170
|
+
], "title");
|
|
171
|
+
src = patchField(src, [
|
|
172
|
+
[/^ {2}description:\s*\{\s*en:\s*"[^"]*",\s*zh:\s*"[^"]*"\s*\}/m, ` description: { en: ${quotedDescription}, zh: ${quotedDescription} }`],
|
|
173
|
+
[/^ {2}description:\s*"[^"]*"/m, ` description: ${quotedDescription}`]
|
|
174
|
+
], "description");
|
|
175
|
+
src = patchField(src, [
|
|
176
|
+
[
|
|
177
|
+
/^ {2}footerText:\s*\{\s*en:\s*`[^`]*`,\s*zh:\s*`[^`]*`\s*\}/m,
|
|
178
|
+
` footerText: { en: \`© \${new Date().getFullYear()} ${safeTitle}. All rights reserved.\`, zh: \`© \${new Date().getFullYear()} ${safeTitle}. 保留所有权利。\` }`
|
|
179
|
+
],
|
|
180
|
+
[/^ {2}footerText:\s*`[^`]*`/m, ` footerText: \`© \${new Date().getFullYear()} ${safeTitle}. All rights reserved.\``]
|
|
181
|
+
], "footerText");
|
|
140
182
|
fs.writeFileSync(configPath, src, "utf8");
|
|
141
183
|
}
|
|
142
184
|
function patchPackageJson(projectDir, projectName) {
|
|
@@ -186,6 +228,13 @@ Site title`, projectName);
|
|
|
186
228
|
const siteDescription = await prompt("Site description", "My digital garden");
|
|
187
229
|
console.log(`
|
|
188
230
|
Patching site.config.ts...`);
|
|
231
|
+
if (applyStarterConfig(targetDir)) {
|
|
232
|
+
const removed = removeLocaleTrees(targetDir);
|
|
233
|
+
if (removed.length > 0)
|
|
234
|
+
console.log(` Removed the demo's translations: ${removed.join(", ")}`);
|
|
235
|
+
} else {
|
|
236
|
+
console.warn(" Warning: site.config.example.ts not found, keeping the release's site.config.ts");
|
|
237
|
+
}
|
|
189
238
|
patchSiteConfig(targetDir, siteTitle, siteDescription);
|
|
190
239
|
console.log("Patching package.json...");
|
|
191
240
|
patchPackageJson(targetDir, projectName);
|
|
@@ -210,8 +259,10 @@ Error:`, err.message);
|
|
|
210
259
|
});
|
|
211
260
|
}
|
|
212
261
|
export {
|
|
213
|
-
|
|
214
|
-
|
|
262
|
+
applyStarterConfig,
|
|
263
|
+
buildExtractCommand,
|
|
215
264
|
getArchiveMetadata,
|
|
216
|
-
|
|
265
|
+
patchPackageJson,
|
|
266
|
+
patchSiteConfig,
|
|
267
|
+
removeLocaleTrees
|
|
217
268
|
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-amytis",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Create a new Amytis digital garden",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": { "create-amytis": "dist/index.js" },
|
|
7
8
|
"scripts": {
|
|
8
9
|
"build": "bun build src/index.ts --outfile dist/index.js --target node --sourcemap=none",
|
|
9
10
|
"dev": "bun src/index.ts",
|