-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
git.ts
158 lines (137 loc) · 3.31 KB
/
git.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
// Copyright 2021 the Fastro author. All rights reserved. MIT license.
// deno-lint-ignore-file
import { App, Appdb } from "../core/types.ts";
import { parseYml, stringify, v4 } from "../deps.ts";
const cwd = Deno.cwd() + "/.fastro";
const database = ".fastro/database.yml";
const appFile = ".fastro/app.yml";
async function gitAdd() {
const p = Deno.run({
cwd,
cmd: [
"git",
"add",
"database.yml",
],
});
const { code } = await p.status();
if (code !== 0) throw new Error("Add app error");
}
async function gitDiscard() {
const p = Deno.run({
cwd,
cmd: [
"git",
"checkout",
"--",
"app.yml",
],
});
const { code } = await p.status();
if (code !== 0) throw new Error("Discard app error");
}
async function gitCommit() {
const p = Deno.run({
cwd,
cmd: [
"git",
"commit",
"-am",
"update",
],
});
const { code } = await p.status();
if (code !== 0) throw new Error("Commit app error");
}
async function gitPush() {
const p = Deno.run({
cwd,
cmd: [
"git",
"push",
],
});
const { code } = await p.status();
if (code !== 0) throw new Error("Push app error");
}
async function gitGetEmail() {
const p = Deno.run({
cmd: [
"git",
"config",
"user.email",
],
stdout: "piped",
stderr: "piped",
});
const { code } = await p.status();
if (code !== 0) throw new Error("Get email error");
const rawOutput = await p.output();
let str = new TextDecoder().decode(rawOutput);
str = str.trim();
return str;
}
async function getApp() {
const apps = await Deno.readTextFile(database);
const appList = parseYml(apps) as Appdb[];
return appList;
}
function writeFile(data: string) {
Deno.writeTextFile(database, data)
.then(() => gitDiscard())
.then(() => gitAdd())
.then(() => gitCommit())
.then(() => gitPush());
}
function sortApp(appList: Appdb[]) {
const sorted = appList.sort((a: Appdb, b: Appdb) => {
return ("" + a.n).localeCompare(b.n);
});
return sorted as any;
}
function validateApp(app: Appdb) {
if (app.n === undefined || app.n === null) {
throw new Error("Your app name is empty");
}
if (app.r === undefined || app.r === null) {
throw new Error("Your app repository is empty");
}
}
function isExist(newApp: Appdb, appList: Appdb[]) {
const exist = appList.filter((regApp) => {
return newApp.r === regApp.r || newApp.n === regApp.n;
});
return exist.length > 0;
}
async function addApp(app: Appdb) {
try {
validateApp(app);
const appList = await getApp();
if (isExist(app, appList)) {
throw new Error(
"The application already exists. Use a different name or repository.",
);
}
if (appList) appList.push(app);
const sorted = sortApp(appList);
const yaml = stringify(sorted, { noCompatMode: true, condenseFlow: true });
writeFile(yaml);
} catch (error) {
console.error(error.message);
}
}
export async function handleAddApp() {
const appYaml = Deno.readTextFileSync(appFile);
const app = parseYml(appYaml) as App;
app.email = await gitGetEmail();
app.id = v4.generate();
const appdb: Appdb = {
n: app.name,
d: app.description,
r: app.repository,
s: app.screenshot,
e: app.email,
i: app.id,
};
addApp(appdb);
}