-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.ts
126 lines (104 loc) · 3.43 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
// Copyright 2020 the Fastro author. All rights reserved. MIT license.
// deno-lint-ignore-file no-explicit-any
import {
APPS,
MIDDLEWARE_DIR,
SERVICE_DIR,
STATIC_DIR,
VSCODE_DIR,
} from "../core/constant.ts";
import { main } from "../templates/main.ts";
import { favicon } from "../templates/favicon.ts";
import { render } from "../templates/render.ts";
import { docker } from "../templates/docker.ts";
import { html } from "../templates/html.ts";
import { middleware } from "../templates/middleware.ts";
import { controller } from "../templates/controller.ts";
import { setting } from "../templates/settings.ts";
import { gitignore } from "../templates/gitignore.ts";
import { parseYml } from "../deps.ts";
import { App } from "../core/types.ts";
function initHelp() {
const message = `USAGE
fastro init [OPTIONS]
OPTIONS:
--app Application repository
EXAMPLE:
fastro init --app fastrojs/admin
`;
console.log(message);
}
async function cloneRepo(app: string) {
const p = Deno.run({
cmd: [
"git",
"clone",
`https://github.com/${app}.git`,
".",
],
});
const { code } = await p.status();
if (code !== 0) throw new Error("Clone app error");
}
async function removeGit() {
const p = Deno.run({
cmd: [
"rm",
"-rf",
".git",
],
});
const { code } = await p.status();
if (code !== 0) throw new Error("Init app error");
}
function filterAppList(appList: App[], app: string) {
const exist = appList.filter((val) => val.repository === app);
if (exist.length < 1) throw new Error("App not found");
}
async function getApp() {
const result = await fetch(APPS);
const apps = await result.text();
const appList = parseYml(apps) as App[];
return appList;
}
function initApp(app: string) {
getApp()
.then((appList) => filterAppList(appList, app))
.then(() => cloneRepo(app))
.then(() => removeGit())
.then(() => console.log("Init app done"))
.catch((error) => console.error(error.message));
}
export async function init(args?: any) {
if (args.help) return initHelp();
if (args.app) return initApp(args.app);
const encoder = new TextEncoder();
const mainFile = encoder.encode(main);
await Deno.writeFile("main.ts", mainFile);
const gitFile = encoder.encode(gitignore);
await Deno.writeFile(".gitignore", gitFile);
const dockerFile = encoder.encode(docker);
await Deno.writeFile("Dockerfile", dockerFile);
await Deno.mkdir(MIDDLEWARE_DIR, { recursive: true });
const mid = encoder.encode(middleware);
const midPath = `${MIDDLEWARE_DIR}/support.ts`;
await Deno.writeFile(midPath, mid);
await Deno.mkdir(SERVICE_DIR, { recursive: true });
const ctrl = encoder.encode(controller);
const ctrlPath = `${SERVICE_DIR}/hello.controller.ts`;
await Deno.writeFile(ctrlPath, ctrl);
const htmlTemplate = encoder.encode(render);
const templatePath = `${SERVICE_DIR}/hello.template.html`;
await Deno.writeFile(templatePath, htmlTemplate);
await Deno.mkdir(STATIC_DIR, { recursive: true });
const icon = new Uint8Array(favicon);
const iconPath = `${STATIC_DIR}/favicon.ico`;
await Deno.writeFile(iconPath, icon);
const idx = encoder.encode(html);
const idxPath = `${STATIC_DIR}/index.html`;
await Deno.writeFile(idxPath, idx);
await Deno.mkdir(`${VSCODE_DIR}`, { recursive: true });
const settingJson = encoder.encode(setting);
const settingPath = `${VSCODE_DIR}/settings.json`;
await Deno.writeFile(settingPath, settingJson);
}