-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
236 lines (226 loc) · 11.4 KB
/
index.js
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
const express = require('express');
const app = express();
const fs = require('fs');
const { exec } = require('child_process');
const firebase = require('firebase-admin');
firebase.initializeApp({
credential: firebase.credential.cert({
"type": "service_account",
"project_id": "hacktank-5277b",
"private_key_id": "b5944800015e5428aae44534be4fe50790806e0c",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCupN3OS5CllDyw\nwiFYpqGwtFM4xIXPGV+HY66QKJ3fGLEH7LnfY2t+lzcCJMcI4m56gfp63yN4oDsn\nm5arz+I0dN3RqwVPtYTaemLgjcuLCpoihPB6yaJVpYqD4HTe5hbf2JdA1fUj4lQX\nogFQ5hpqO3NY8ex5on3ZpOheBYjoHGY/C1LrCpdkpTBqiOMrfhLciNXKlpgLOSwn\nlGMDJ7a3b0W0Vk/RxoiUIHcplOcmc1h5ty9SExdJjweyP9fVi5/bXXPHLp33j6rU\nqD2ZqJ921K9TimeCaVJHB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2nMbET6yxfHOG3pYPkbJNq\nl6KYgWodqeZdsAvuKTqfG6HmeBhjc3ww0ADo1rkJOQKBgEAU1H3KwTYA9Vta5TRT\nuF68XEXs8pdypE6KWRy70DWQow8GS+ycBr4Em/xKV5VZxg5UCxzkdU5zyq6IJCno\nY3OBJsLtuFzr/JiZLc/huYw38ihbLZ0mYUY0NAZSMTEn+dWsgOllQr1uk3fU+Q1B\n92K+dlmGMZFjsTmWtzcB+SRk\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-8rjxw@hacktank-5277b.iam.gserviceaccount.com",
"client_id": "106505595061705385981",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-8rjxw%40hacktank-5277b.iam.gserviceaccount.com"
})
});
const db = firebase.firestore();
const components = ["Navbar", "Button", "CheckboxComponent", "Box", "Image"];
const pages = ["index"];
const type = (filename) => {
filename = filename.split(".")[0];
for (let x in components) {
if (components[x] === filename)
return "components";
}
for (let x in pages) {
if (pages[x] === filename)
return "pages";
}
return "element";
}
let arr = [];
let object = [];
const getdata = async (projectName) => {
const layoutRef = db.collection('layout').doc(projectName);
const objectRef = db.collection('component').doc(projectName);
const layoutSnapshot = await layoutRef.get();
arr = layoutSnapshot.data();
const objectSnapshot = await objectRef.get();
object = objectSnapshot.data();
}
class file {
constructor(name, directory) {
this.name = name;
this.directory = directory;
this.location = type(name);
}
}
class Queue {
constructor() {
this.items = {}
this.frontIndex = 0
this.backIndex = 0
}
enqueue(item) {
this.items[this.backIndex] = item
this.backIndex++
}
dequeue() {
const item = this.items[this.frontIndex]
delete this.items[this.frontIndex]
this.frontIndex++
return item
}
peek() {
return this.items[this.frontIndex]
}
isEmpty() {
return this.items.length == 0;
}
}
const donePages = [];
const universal = {
"Navbar": "<Navbar />\n",
"Checkbox": "<CheckboxComponent />\n",
"Button": "<Button />\n",
"Text": "<TextareaAutosize />\n",
"Box": "<Box />\n",
"Image": "<Image />\n",
"div": "<> </>\n"
};
const initial = (arr, projectName, page, index) => {
const projectDirectory = __dirname + "/projects/" + projectName;
const parentDirectory = __dirname + "/templates";
const obj = new file(page, projectDirectory);
const copyto = obj.directory + "/" + obj.location + "/" + obj.name + (obj.location === "components" ? ".jsx" : ".js");
let divfiletop = parentDirectory + "/components/Divtop.txt";
let divfilebottom = parentDirectory + "/components/Divbottom.txt";
let divStylefiletop = parentDirectory + "/components/DivStyletop.txt";
let divStylefilebottom = parentDirectory + "/components/DivStylebottom.txt";
for (let x in arr) {
const dt = fs.readFileSync(divStylefiletop, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, dt, { flag: "a+" });
if (arr[x].type === "column") {
const dsb = fs.readFileSync(divfiletop, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, dsb, { flag: "a+" });
} if (arr[x].type === "row") {
const dst = fs.readFileSync(divStylefiletop, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, dst, { flag: "a+" });
}
if (Array.isArray(arr[x]["children"]) === "true") {
initial(arr[x]["children"], projectName, page, index + 1);
} else {
for (let y in arr[x]["children"]) {
const dst = fs.readFileSync(divfiletop, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, dst, { flag: "a+" });
for (let z in arr[x]["children"][y]["children"])
fs.writeFileSync(copyto, universal[object[arr[x]["children"][y]["children"][z].id].type], { flag: "a+" });
const pst = fs.readFileSync(divfilebottom, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, pst, { flag: "a+" });
}
const dt = fs.readFileSync(divStylefilebottom, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, dt, { flag: "a+" });
}
if (arr[x].type === "column") {
const dst = fs.readFileSync(divfilebottom, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, dst, { flag: "a+" });
} if (arr[x].type === "row") {
const dsb = fs.readFileSync(divStylefilebottom, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, dsb, { flag: "a+" });
}
}
const db = fs.readFileSync(parentDirectory + "/pages/" + obj.name + "bottom.txt", 'utf-8', { flag: 'r' });
fs.appendFileSync(copyto, db, { flag: "a+" });
}
const process = async (projectName) => {
const projectDirectory = __dirname + "/projects/" + projectName;
const parentDirectory = __dirname + "/templates";
const home = new file("index", projectDirectory);
const queue = new Queue();
queue.enqueue(home);
queue.enqueue(new file("Blogs", projectDirectory));
queue.enqueue(new file("About", projectDirectory));
while (queue.isEmpty() == false) {
const obj = queue.dequeue();
if (obj === undefined)
break;
if (data[obj.name] === undefined)
continue;
donePages.push(obj);
data[obj.name].item.forEach(x => {
const part = new file(x, projectDirectory);
if (part.directory !== "element") {
queue.enqueue(part);
}
let copyto = obj.directory + "/" + obj.location + "/" + obj.name + (obj.location === "components" ? ".jsx" : ".js");
fs.writeFileSync(copyto, universal[part.name], { flag: 'a+' });
});
const copyfile = obj.directory + "/" + obj.location + "/" + obj.name + (obj.location === "components" ? ".jsx" : ".js");
const projectfile = parentDirectory + "/" + obj.location + "/" + obj.name + "bottom.txt";
const dt = fs.readFileSync(projectfile, 'utf-8', { flag: 'r' });
fs.appendFileSync(copyfile, dt, { flag: "a+" });
}
// components.forEach(x => {
// let found = true;
// const obj = new file(x, projectDirectory);
// donePages.forEach(y => {
// if (obj === y)
// found = false;
// })
// if (found) {
// console.log(obj);
// const copyfile = obj.directory + "/" + obj.location + "/" + obj.name + (obj.location === "components" ? ".jsx" : ".js");
// const projectfile = parentDirectory + "/" + obj.location + "/" + obj.name + "bottom.txt";
// const dt = fs.readFileSync(projectfile, 'utf-8', { flag: 'r' });
// fs.appendFileSync(copyfile, dt, { flag: "a+" });
// }
// })
// pages.forEach(x => {
// let found = true;
// const obj = new file(x, projectDirectory);
// donePages.forEach(y => {
// if (obj === y)
// found = false;
// })
// if (found) {
// console.log(obj);
// const copyfile = obj.directory + "/" + obj.location + "/" + obj.name + (obj.location === "components" ? ".jsx" : ".js");
// const projectfile = parentDirectory + "/" + obj.location + "/" + obj.name + "bottom.txt";
// const dt = fs.readFileSync(projectfile, 'utf-8', { flag: 'r' });
// fs.appendFileSync(copyfile, dt, { flag: "a+" });
// }
// })
}
const createFolder = (projectName) => {
const projectDirectory = __dirname + "/projects/" + projectName;
const parentDirectory = __dirname + "/templates";
fs.mkdirSync(projectDirectory, { recursive: true });
fs.mkdirSync(projectDirectory + "/components", { recursive: true });
fs.mkdirSync(projectDirectory + "/pages/users", { recursive: true });
fs.mkdirSync(projectDirectory + "/pages/blog", { recursive: true });
fs.mkdirSync(projectDirectory + "/config", { recursive: true });
fs.cpSync(parentDirectory + "/styles", projectDirectory + "/styles", { recursive: true });
fs.copyFileSync(parentDirectory + "/_app.js", projectDirectory + "/pages/_app.js");
fs.copyFileSync(parentDirectory + "/fire-config.js", projectDirectory + "/config/fire-config.js");
fs.copyFileSync(parentDirectory + "/.env.local", projectDirectory + "/.env.local");
exec(`cd ${projectDirectory} && npm init -y`, (error, stdout, stderr) => { });
exec(`cd ${projectDirectory} && npm i @emotion/react @emotion/styled @material-ui/core @material-ui/icons @mui/base @mui/icons-material @mui/material classnames firebase immutability-helper lodash react react-dnd react-dnd-html5-backend react-dom react-frame-component react-scripts shortid`, (error, stdout, stderr) => {
if (error) console.log(error);
if (stdout) { console.log(stdout); exec(`cd ${projectDirectory} && npm i @emotion/react @emotion/styled @material-ui/core @material-ui/icons @mui/base @mui/icons-material @mui/material classnames firebase immutability-helper lodash react react-dnd react-dnd-html5-backend react-dom react-frame-component react-scripts shortid`, (error, stdout, stderr) => { }); }
if (stderr) console.log(stderr);
});
components.forEach(x => {
const obj = new file(x, projectDirectory);
const copyfile = obj.directory + "/" + obj.location + "/" + obj.name + (obj.location === "components" ? ".jsx" : ".js");
const projecfile = parentDirectory + "/" + obj.location + "/" + obj.name + "top.txt";
fs.copyFileSync(projecfile, copyfile);
})
pages.forEach(x => {
const obj = new file(x, projectDirectory);
const copyfile = obj.directory + "/" + obj.location + "/" + obj.name + (obj.location === "components" ? ".jsx" : ".js");
const projecfile = parentDirectory + "/" + obj.location + "/" + obj.name + "top.txt";
fs.copyFileSync(projecfile, copyfile);
})
}
app.listen(5000, async () => {
console.log(`server is running at port 3000`);
await getdata('hacktank');
console.log(arr);
console.log(object);
createFolder('hacktank');
initial(arr.layout, 'hacktank', 'index', 0);
});