create-amytis 0.1.0
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 +173 -0
- package/package.json +17 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import * as https from "https";
|
|
9
|
+
import * as readline from "readline";
|
|
10
|
+
import { execSync } from "child_process";
|
|
11
|
+
function prompt(question, defaultVal = "") {
|
|
12
|
+
const rl = readline.createInterface({
|
|
13
|
+
input: process.stdin,
|
|
14
|
+
output: process.stdout
|
|
15
|
+
});
|
|
16
|
+
const display = defaultVal ? `${question} (${defaultVal}): ` : `${question}: `;
|
|
17
|
+
return new Promise((resolve2) => {
|
|
18
|
+
rl.question(display, (answer) => {
|
|
19
|
+
rl.close();
|
|
20
|
+
resolve2(answer.trim() || defaultVal);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function fetchJson(url) {
|
|
25
|
+
return new Promise((resolve2, reject) => {
|
|
26
|
+
https.get(url, { headers: { "User-Agent": "create-amytis-cli" } }, (res) => {
|
|
27
|
+
if (res.statusCode === 301 || res.statusCode === 302) {
|
|
28
|
+
fetchJson(res.headers.location).then(resolve2).catch(reject);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (res.statusCode !== 200) {
|
|
32
|
+
reject(new Error(`GitHub API returned ${res.statusCode} for ${url}`));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
let data = "";
|
|
36
|
+
res.on("data", (chunk) => data += chunk);
|
|
37
|
+
res.on("end", () => {
|
|
38
|
+
try {
|
|
39
|
+
resolve2(JSON.parse(data));
|
|
40
|
+
} catch {
|
|
41
|
+
reject(new Error("Failed to parse GitHub API response"));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}).on("error", reject);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function downloadFile(url, dest) {
|
|
48
|
+
return new Promise((resolve2, reject) => {
|
|
49
|
+
const doGet = (targetUrl) => {
|
|
50
|
+
https.get(targetUrl, { headers: { "User-Agent": "create-amytis-cli" } }, (res) => {
|
|
51
|
+
if (res.statusCode === 301 || res.statusCode === 302) {
|
|
52
|
+
doGet(res.headers.location);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (res.statusCode !== 200) {
|
|
56
|
+
reject(new Error(`Download failed with status ${res.statusCode}`));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const file = fs.createWriteStream(dest);
|
|
60
|
+
res.pipe(file);
|
|
61
|
+
file.on("finish", () => file.close(() => resolve2()));
|
|
62
|
+
file.on("error", (err) => {
|
|
63
|
+
fs.unlink(dest, () => {});
|
|
64
|
+
reject(err);
|
|
65
|
+
});
|
|
66
|
+
}).on("error", reject);
|
|
67
|
+
};
|
|
68
|
+
doGet(url);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function extractTarball(tarPath, outDir) {
|
|
72
|
+
const tmpDir = `${outDir}.__tmp__`;
|
|
73
|
+
fs.mkdirSync(tmpDir, { recursive: true });
|
|
74
|
+
execSync(`tar xzf "${tarPath}" -C "${tmpDir}"`);
|
|
75
|
+
const entries = fs.readdirSync(tmpDir);
|
|
76
|
+
if (entries.length !== 1) {
|
|
77
|
+
throw new Error(`Unexpected tarball structure: ${entries.join(", ")}`);
|
|
78
|
+
}
|
|
79
|
+
const innerDir = path.join(tmpDir, entries[0]);
|
|
80
|
+
fs.renameSync(innerDir, outDir);
|
|
81
|
+
fs.rmdirSync(tmpDir);
|
|
82
|
+
fs.unlinkSync(tarPath);
|
|
83
|
+
}
|
|
84
|
+
function escapeTemplateLiteral(str) {
|
|
85
|
+
return str.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
86
|
+
}
|
|
87
|
+
function patchSiteConfig(projectDir, title, description) {
|
|
88
|
+
const configPath = path.join(projectDir, "site.config.ts");
|
|
89
|
+
if (!fs.existsSync(configPath)) {
|
|
90
|
+
console.warn(" Warning: site.config.ts not found, skipping patch");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
let src = fs.readFileSync(configPath, "utf8");
|
|
94
|
+
src = src.replace(/title:\s*\{\s*en:\s*"[^"]*",\s*zh:\s*"[^"]*"\s*\}/, `title: { en: ${JSON.stringify(title)}, zh: ${JSON.stringify(title)} }`);
|
|
95
|
+
src = src.replace(/description:\s*\{\s*en:\s*"[^"]*",\s*zh:\s*"[^"]*"\s*\}/, `description: { en: ${JSON.stringify(description)}, zh: ${JSON.stringify(description)} }`);
|
|
96
|
+
const safeTitle = escapeTemplateLiteral(title);
|
|
97
|
+
src = src.replace(/footerText:\s*\{\s*en:\s*`[^`]*`,\s*zh:\s*`[^`]*`\s*\}/, `footerText: { en: \`© \${new Date().getFullYear()} ${safeTitle}. All rights reserved.\`, zh: \`© \${new Date().getFullYear()} ${safeTitle}. 保留所有权利。\` }`);
|
|
98
|
+
fs.writeFileSync(configPath, src, "utf8");
|
|
99
|
+
}
|
|
100
|
+
function patchPackageJson(projectDir, projectName) {
|
|
101
|
+
const pkgPath = path.join(projectDir, "package.json");
|
|
102
|
+
if (!fs.existsSync(pkgPath)) {
|
|
103
|
+
console.warn(" Warning: package.json not found, skipping patch");
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
107
|
+
pkg["name"] = projectName;
|
|
108
|
+
pkg["private"] = true;
|
|
109
|
+
delete pkg["repository"];
|
|
110
|
+
delete pkg["bugs"];
|
|
111
|
+
delete pkg["homepage"];
|
|
112
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + `
|
|
113
|
+
`, "utf8");
|
|
114
|
+
}
|
|
115
|
+
async function main() {
|
|
116
|
+
console.log(`
|
|
117
|
+
Create Amytis — scaffold a new digital garden
|
|
118
|
+
`);
|
|
119
|
+
let projectName = process.argv[2] ?? "";
|
|
120
|
+
if (!projectName) {
|
|
121
|
+
projectName = await prompt("Project name", "my-blog");
|
|
122
|
+
}
|
|
123
|
+
const targetDir = path.resolve(process.cwd(), projectName);
|
|
124
|
+
if (fs.existsSync(targetDir)) {
|
|
125
|
+
console.error(`Error: directory "${projectName}" already exists.`);
|
|
126
|
+
process.exit(1);
|
|
127
|
+
}
|
|
128
|
+
console.log(`
|
|
129
|
+
Fetching latest Amytis release...`);
|
|
130
|
+
const release = await fetchJson("https://api.github.com/repos/hutusi/amytis/releases/latest");
|
|
131
|
+
const tag = release["tag_name"];
|
|
132
|
+
if (!tag)
|
|
133
|
+
throw new Error("Could not determine latest release tag");
|
|
134
|
+
console.log(` Found: ${tag}`);
|
|
135
|
+
const tarUrl = `https://github.com/hutusi/amytis/archive/refs/tags/${tag}.tar.gz`;
|
|
136
|
+
const tarDest = path.join(process.cwd(), `amytis-${tag}.tar.gz`);
|
|
137
|
+
console.log("Downloading tarball...");
|
|
138
|
+
await downloadFile(tarUrl, tarDest);
|
|
139
|
+
console.log("Extracting...");
|
|
140
|
+
extractTarball(tarDest, targetDir);
|
|
141
|
+
console.log(` Scaffolded: ${targetDir}`);
|
|
142
|
+
const siteTitle = await prompt(`
|
|
143
|
+
Site title`, projectName);
|
|
144
|
+
const siteDescription = await prompt("Site description", "My digital garden");
|
|
145
|
+
console.log(`
|
|
146
|
+
Patching site.config.ts...`);
|
|
147
|
+
patchSiteConfig(targetDir, siteTitle, siteDescription);
|
|
148
|
+
console.log("Patching package.json...");
|
|
149
|
+
patchPackageJson(targetDir, projectName);
|
|
150
|
+
console.log("Installing dependencies (bun install)...");
|
|
151
|
+
execSync("bun install", { cwd: targetDir, stdio: "inherit" });
|
|
152
|
+
console.log(`
|
|
153
|
+
Done! Your new Amytis site is ready.
|
|
154
|
+
|
|
155
|
+
cd ${projectName}
|
|
156
|
+
bun dev # start the dev server at http://localhost:3000
|
|
157
|
+
bun run build # production build
|
|
158
|
+
|
|
159
|
+
Edit site.config.ts to customise navigation, themes, and more.
|
|
160
|
+
Happy gardening!
|
|
161
|
+
`);
|
|
162
|
+
}
|
|
163
|
+
if (__require.main == __require.module) {
|
|
164
|
+
main().catch((err) => {
|
|
165
|
+
console.error(`
|
|
166
|
+
Error:`, err.message);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
export {
|
|
171
|
+
patchSiteConfig,
|
|
172
|
+
patchPackageJson
|
|
173
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-amytis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Create a new Amytis digital garden",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"bin": {
|
|
7
|
+
"create-amytis": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "bun build src/index.ts --outfile dist/index.js --target node --sourcemap=none",
|
|
11
|
+
"dev": "bun src/index.ts",
|
|
12
|
+
"test": "bun test src"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
]
|
|
17
|
+
}
|