-
Notifications
You must be signed in to change notification settings - Fork 167
/
init.ts
375 lines (348 loc) · 10.8 KB
/
init.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/** @format */
import { Untar } from "https://deno.land/[email protected]/archive/untar.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { blue, bold, cyan, dim, green, red } from "https://deno.land/[email protected]/fmt/colors.ts";
import { copy as copyDir } from "https://deno.land/[email protected]/fs/copy.ts";
import { copy } from "https://deno.land/[email protected]/streams/copy.ts";
import { readerFromStreamReader } from "https://deno.land/[email protected]/streams/reader_from_stream_reader.ts";
import { ensureDir } from "https://deno.land/[email protected]/fs/ensure_dir.ts";
import { basename, join } from "https://deno.land/[email protected]/path/mod.ts";
const templates = ["react", "react-mdx", "yew", "leptos", "api"];
const rsApps = ["yew", "leptos"];
const unocssApps = ["react", "yew", "leptos"];
const versions = {
react: "18.2.0",
};
type Options = {
template?: string;
};
export default async function init(nameArg?: string, options?: Options) {
let { template } = options || {};
// get and check the project name
const name = nameArg ?? (await ask("Project Name:"));
if (!name) {
console.error(`${red("!")} Please entry project name.`);
Deno.exit(1);
}
if (template && !templates.includes(template)) {
console.error(
`${red("!")} Invalid template name ${
red(
template,
)
}, must be one of [${blue(templates.join(","))}]`,
);
Deno.exit(1);
}
// check the dir is clean
if (
!(await isFolderEmpty(Deno.cwd(), name)) &&
!(await confirm(`Folder ${blue(name)} already exists, continue?`))
) {
Deno.exit(1);
}
if (!template) {
const answer = await ask(
[
"Select a framework:",
...templates.map(
(name, i) =>
` ${
bold(
(i + 1).toString(),
)
}. ${getTemplateDisplayName(name)}`,
),
dim(`[1-${templates.length}]`),
].join("\n"),
);
const n = parseInt(answer);
if (!isNaN(n) && n > 0 && n <= templates.length) {
template = templates[n - 1];
} else {
console.error(
`${red("!")} Please entry ${cyan(`[1-${templates.length}]`)}.`,
);
Deno.exit(1);
}
}
const appDir = join(Deno.cwd(), name);
const withUnocss = unocssApps.includes(template!) &&
(await confirm("Use Atomic CSS (powered by Unocss)?"));
const withVscode = await confirm(
"Initialize VS Code workspace configuration?",
);
const deploy = !rsApps.includes(template) ? await confirm("Deploy to Deno Deploy?") : false;
const isRsApp = rsApps.includes(template);
let alephPkgUri: string;
if (import.meta.url.startsWith("file://")) {
const src = `examples/${withUnocss ? "with-unocss/" : ""}${template}-app/`;
await copyDir(src, name);
alephPkgUri = "..";
} else {
console.log(
`${dim("↓")} Downloading template(${
blue(
template!,
)
}), this might take a moment...`,
);
const res = await fetch(
"https://cdn.deno.land/aleph/meta/versions.json",
);
if (res.status !== 200) {
console.error(await res.text());
Deno.exit(1);
}
const { latest: VERSION } = await res.json();
const repo = "alephjs/aleph.js";
const resp = await fetch(
`https://codeload.github.com/${repo}/tar.gz/refs/tags/${VERSION}`,
);
if (resp.status !== 200) {
console.error(await resp.text());
Deno.exit(1);
}
// deno-lint-ignore ban-ts-comment
// @ts-ignore
const gz = new DecompressionStream("gzip");
const entryList = new Untar(
readerFromStreamReader(
resp.body!.pipeThrough<Uint8Array>(gz).getReader(),
),
);
const prefix = `${basename(repo)}-${VERSION}/examples/${withUnocss ? "with-unocss/" : ""}${template}-app/`;
for await (const entry of entryList) {
if (
entry.fileName.startsWith(prefix) &&
!entry.fileName.endsWith("/README.md")
) {
const fp = join(appDir, trimPrefix(entry.fileName, prefix));
if (entry.type === "directory") {
await ensureDir(fp);
continue;
}
const file = await Deno.open(fp, { write: true, create: true });
await copy(entry, file);
}
}
alephPkgUri = `https://deno.land/x/aleph@${VERSION}`;
}
const serverCode = await Deno.readTextFile(join(appDir, "server.ts"));
if (!isRsApp && !deploy) {
await Deno.writeTextFile(
join(appDir, "server.ts"),
serverCode
.replace('import modules from "./routes/_export.ts";\n', "")
.replace('import denoDeploy from "aleph/plugins/deploy";\n', "")
.replace(
" denoDeploy({ moduleMain: import.meta.url, modules }),\n",
"",
)
.replace(" denoDeploy({ modules }),\n", ""),
);
await Deno.remove(join(appDir, "routes/_export.ts"));
} else {
await Deno.writeTextFile(
join(appDir, "server.ts"),
serverCode.replace(
"denoDeploy({ moduleMain: import.meta.url, modules })",
"denoDeploy({ modules })",
),
);
}
const res = await fetch("https://esm.sh/status.json");
if (res.status !== 200) {
console.error(await res.text());
Deno.exit(1);
}
const { version: ESM_VERSION } = await res.json();
const denoConfig = {
compilerOptions: {
lib: ["dom", "dom.iterable", "dom.extras", "deno.ns"],
types: [`${alephPkgUri}/types.d.ts`],
},
importMap: "import_map.json",
tasks: {
dev: (await existsFile(join(appDir, "dev.ts"))) ? "deno run -A dev.ts" : `deno run -A ${alephPkgUri}/dev.ts`,
start: "deno run -A server.ts",
build: "deno run -A server.ts --build",
"esm:add": `deno run -A https://esm.sh/v${ESM_VERSION} add`,
"esm:update": `deno run -A https://esm.sh/v${ESM_VERSION} update`,
"esm:remove": `deno run -A https://esm.sh/v${ESM_VERSION} remove`,
},
};
const importMap = {
imports: {
"~/": "./",
"std/": "https://deno.land/[email protected]/",
"aleph/": `${alephPkgUri}/`,
"aleph/server": `${alephPkgUri}/server/mod.ts`,
"aleph/dev": `${alephPkgUri}/server/dev.ts`,
} as Record<string, string>,
scopes: {},
};
if (deploy) {
Object.assign(importMap.imports, {
"aleph/plugins/deploy": `${alephPkgUri}/plugins/deploy.ts`,
});
}
if (withUnocss) {
Object.assign(importMap.imports, {
"aleph/plugins/unocss": `${alephPkgUri}/plugins/unocss.ts`,
"@unocss/core": `https://esm.sh/v${ESM_VERSION}/@unocss/[email protected]`,
"@unocss/preset-uno": `https://esm.sh/v${ESM_VERSION}/@unocss/[email protected]`,
});
}
switch (template) {
case "react-mdx":
Object.assign(importMap.imports, {
"aleph/plugins/mdx": `${alephPkgUri}/plugins/mdx.ts`,
"@mdx-js/react": `https://esm.sh/v${ESM_VERSION}/@mdx-js/[email protected]`,
});
/* falls through */
case "react": {
Object.assign(denoConfig.compilerOptions, {
jsx: "react-jsx",
jsxImportSource: `https://esm.sh/v${ESM_VERSION}/react@${versions.react}`,
});
Object.assign(importMap.imports, {
"aleph/react": `${alephPkgUri}/framework/react/mod.ts`,
"aleph/plugins/react": `${alephPkgUri}/framework/react/plugin.ts`,
react: `https://esm.sh/v${ESM_VERSION}/react@${versions.react}`,
"react-dom": `https://esm.sh/v${ESM_VERSION}/react-dom@${versions.react}`,
"react-dom/": `https://esm.sh/v${ESM_VERSION}/react-dom@${versions.react}/`,
});
break;
}
}
await ensureDir(appDir);
await Promise.all([
Deno.writeTextFile(
join(appDir, "deno.json"),
JSON.stringify(denoConfig, undefined, 2),
),
Deno.writeTextFile(
join(appDir, "import_map.json"),
JSON.stringify(importMap, undefined, 2),
),
]);
if (withVscode) {
const settings = {
"deno.enable": true,
"deno.lint": true,
"deno.config": "./deno.json",
};
await ensureDir(join(appDir, ".vscode"));
await Deno.writeTextFile(
join(appDir, ".vscode", "settings.json"),
JSON.stringify(settings, undefined, 2),
);
}
await Deno.run({
cmd: [Deno.execPath(), "cache", "--no-lock", "server.ts"],
cwd: appDir,
stderr: "inherit",
stdout: "inherit",
}).status();
console.log(
[
"",
green("▲ Aleph.js is ready to go!"),
"",
`${dim("$")} cd ${name}`,
`${dim("$")} deno task dev ${
dim(
"# Start the server in `development` mode",
)
}`,
`${dim("$")} deno task start ${
dim(
"# Start the server in `production` mode",
)
}`,
`${dim("$")} deno task build ${
dim(
"# Build & Optimize the app (bundling, SSG, etc.)",
)
}`,
"",
`Docs: ${cyan("https://alephjs.org/docs")}`,
`Bugs: ${cyan("https://github.com/alephjs/aleph.js/issues")}`,
"",
].join("\n"),
);
Deno.exit(0);
}
async function isFolderEmpty(root: string, name: string): Promise<boolean> {
const dir = join(root, name);
if (await existsFile(dir)) {
throw new Error(`Folder ${name} already exists as a file.`);
}
if (await existsDir(dir)) {
for await (const file of Deno.readDir(dir)) {
if (file.name !== ".DS_Store") {
return false;
}
}
}
return true;
}
async function ask(question = ":") {
await Deno.stdout.write(
new TextEncoder().encode(cyan("? ") + question + " "),
);
const buf = new Uint8Array(1024);
const n = <number> await Deno.stdin.read(buf);
const answer = new TextDecoder().decode(buf.subarray(0, n));
return answer.trim();
}
async function confirm(question = "are you sure?") {
let a: string;
// deno-lint-ignore no-empty
while (!/^(y|n|)$/i.test(a = await ask(question + dim(" [y/N]")))) {}
return a.toLowerCase() === "y";
}
function trimPrefix(s: string, prefix: string): string {
if (prefix !== "" && s.startsWith(prefix)) {
return s.slice(prefix.length);
}
return s;
}
function getTemplateDisplayName(name: string) {
if (name === "api") {
return "REST API";
}
if (name === "react-mdx") {
return "React with MDX";
}
return name.at(0)?.toUpperCase() + name.slice(1);
}
/** Check whether or not the given path exists as a directory. */
export async function existsDir(path: string): Promise<boolean> {
try {
const stat = await Deno.lstat(path);
return stat.isDirectory;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}
/** Check whether or not the given path exists as regular file. */
export async function existsFile(path: string): Promise<boolean> {
try {
const stat = await Deno.lstat(path);
return stat.isFile;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}
if (import.meta.main) {
const { _: args, ...options } = parse(Deno.args);
await init(args[0] as string | undefined, options);
}