-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
377 lines (327 loc) · 8.49 KB
/
cli.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
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
376
377
#!/usr/bin/env node
const { Server } = require("./native/javascript/websom");
const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const iq = require("inquirer");
const cp = require("ncp");
let cwd = process.cwd();
const gatherSettings = (cb) => {
let data = {};
iq.prompt([
{
type: "list",
name: "language",
message: "Back-end language",
choices: [
"Javascript",
"PHP (coming soon)",
"Carbon (Not recommended for new users)"
]
},
{
type: "list",
name: "database",
message: "Database (use loki if in doubt)",
choices: [
"loki.js",
"Firebase",
"MySQL (coming soon)",
"Mongo DB (coming soon)",
"DynamoDB (coming soon)"
]
},
{
type: "list",
name: "mail",
message: "Mail Service",
choices: [
"SendGrid",
"SMTP Login",
"None"
]
},
/*{
type: "checkbox",
name: "modules",
message: "Pre-install commonly used modules",
choices: [
"UserManager (recommended)"
]
},*/
{
type: "list",
name: "theme",
message: "Front-end theme",
choices: [
"Material",
"Zero",
"Air",
"I'll build my own from scratch",
"None"
]
},
{
type: "list",
name: "template",
message: "Start with a template",
choices: [
"App",
"Blog",
"Single Page Experience",
"E-commerce",
"Membership",
"None"
]
},
{
type: "list",
name: "deploy",
message: "Environment",
choices: [
"Serverless GCP - Firebase - No SSR",
"Serverless GCP - Firebase - With SSR",
"Serverless AWS",
"Apache",
"Nginx",
"Express",
"Static",
"None"
]
}
]).then(async answers => {
cb(answers);
});
};
let findWebsomWebsite = (dir) => {
let isDir = f => fs.existsSync(path.resolve(dir, "./" + f)) && fs.lstatSync(fs.realpathSync(path.resolve(dir, "./" + f))).isDirectory();
if (
isDir("website")
/*&& isDir("website/modules")
&& isDir("website/packs")
&& isDir("website/themes")*/
) {
return dir;
}
let up = path.resolve(dir, "../");
if (up == dir) {
return;
}
return findWebsomWebsite(up);
};
let [,, ...args] = process.argv;
if (args[0] == "init") {
let items = fs.readdirSync(cwd);
items = items.filter(i => i != "." && i != "..");
if (items.length > 0) {
console.log(chalk.red("[ERROR] This folder must empty."));
return;
}
gatherSettings((settings) => {
fs.mkdirSync(path.resolve(cwd, "./config"));
let globalConfig = {
"adapters": {},
"theme.theme.app": {
"rasterIcon": "icon/icon.png",
"vectorIcon": "icon/icon.svg"
}
};
if (settings.database == "loki.js") {
globalConfig.adapters.database = "loki";
globalConfig["adapter.database.loki"] = {
persistence: "database.db"
};
}else if (settings.database == "Firebase") {
globalConfig.adapters.database = "firestore";
globalConfig["adapter.database.firestore"] = {
credentials: "./firestore.json"
};
fs.writeFileSync(path.resolve(cwd, "./config/firestore.json"), "{}");
}
if (settings.mail == "SendGrid") {
globalConfig.adapter.email = "sendGrid";
globalConfig["adapter.email.sendGrid"] = {
apiKey: "<sendgrid api key>"
};
}else if (settings.mail == "SMTP Login") {
globalConfig.adapter.email = "smtp";
globalConfig["adapter.email.smtp"] = {
host: "example.com",
port: "445",
ssl: true,
username: "[email protected]",
password: "******"
};
}
fs.writeFileSync(path.resolve(cwd, "./config/global.json"), JSON.stringify(globalConfig, null, "\t"));
fs.writeFileSync(path.resolve(cwd, "./config/deploy.json"), JSON.stringify({
deploys: []
}, null, "\t"));
fs.writeFileSync(path.resolve(cwd, "./package.json"), JSON.stringify({
"name": "websom-website",
"version": "1.0.0",
"description": "",
"main": "devServer.js",
"scripts": {
"dev": "node devServer.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"websom": "*"
}
}, null, "\t"));
fs.writeFileSync(path.resolve(cwd, "./composer.json"), JSON.stringify({
"require": {}
}, null, "\t"));
let devServer = `const { Server } = require("websom");
let server = new Server({
dev: true,
base: __dirname,
config: "./config",
dist: "./dist",
assets: "./website",
buckets: "./buckets",
headless: false,
openInBrowser: true,
verbose: false
});
server.startDevelopmentServer().then(() => {
// Server started
});`;
fs.writeFileSync(path.resolve(cwd, "./devServer.js"), devServer, null, "\t");
fs.mkdirSync(path.resolve(cwd, "./website"));
fs.mkdirSync(path.resolve(cwd, "./website/packs"));
fs.mkdirSync(path.resolve(cwd, "./website/modules"));
fs.mkdirSync(path.resolve(cwd, "./website/themes"));
fs.mkdirSync(path.resolve(cwd, "./website/icon"));
fs.mkdirSync(path.resolve(cwd, "./buckets"));
let copDir = path.resolve(__dirname, "./templates/default") + "/";
if (settings.language == "Javascript") {
copDir += "javascript";
}else if (settings.language == "Carbon (Not recommended for new users)") {
copDir += "carbon";
}else if (settings.language == "PHP (coming soon)") {
copDir += "php";
}
cp(path.resolve(__dirname, "./icon"), path.resolve(cwd, "./website/icon"), () => {
cp(copDir, path.resolve(cwd, "./website/modules/app"), () => {
console.log(chalk.green("[PROJECT] Initialized."));
console.log(chalk.blue("------- Execute -------"));
console.log("$" + chalk.green(" npm") + chalk.green(" install"));
console.log("$" + chalk.green(" npm") + chalk.green(" run") + chalk.green(" dev"));
console.log(chalk.blue("--- To get started ---"));
});
});
});
}else{
let website = findWebsomWebsite(cwd);
if (!website) {
console.log(chalk.red("[ERROR] No websom site found."));
console.log(chalk.blue("[TIP] Create one using ") + chalk.cyan("$ websom init"));
return;
}
if (args[0] == "locate") {
console.log(chalk.green("[FOUND] " + website));
}else if (args[0] == "create") {
let type = args[1];
let name = args[2];
if (!(type == "module" || type == "theme") || !name) {
console.log("Missing name or type. websom create <theme|module> <name>");
return;
}
let packagePath = path.resolve(website, "./website/" + type + "s/" + name);
if (fs.existsSync(packagePath)) {
console.log("A " + type + " with that name already exists.");
return;
}
fs.mkdirSync(packagePath);
fs.writeFileSync(packagePath + "/" + type + ".json", JSON.stringify({
"name": name,
"id": name.toLowerCase(),
"key": name.toLowerCase(),
"resources": [
{
"path": "./src/client/components"
},
{
"path": "./src/client/state"
},
{
"path": "./src/client/script"
},
{
"path": "./src/client/style"
}
],
"npm": {},
"composer": {},
"adapters": {},
"config": {}
}, null, "\t"));
fs.writeFileSync(packagePath + "/package.json", JSON.stringify({
"name": name.toLowerCase(),
"version": "1.0.0",
"description": "",
"main": type + ".js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "",
"websom": {
"type": type
}
}, null, "\t"));
fs.mkdirSync(packagePath + "/src");
fs.mkdirSync(packagePath + "/src/client");
fs.mkdirSync(packagePath + "/src/client/components");
fs.mkdirSync(packagePath + "/src/client/state");
fs.mkdirSync(packagePath + "/src/client/script");
fs.mkdirSync(packagePath + "/src/client/style");
if (type == "module") {
fs.mkdirSync(packagePath + "/src/server");
fs.mkdirSync(packagePath + "/src/server/adapters");
fs.mkdirSync(packagePath + "/src/server/entities");
fs.mkdirSync(packagePath + "/src/server/logic");
}
let classes = {
module: "Module",
theme: "Theme"
};
if (type == "module")
fs.writeFileSync(packagePath + "/" + name + ".carb", `class ${name} extends Websom.${classes[type]} {
override Websom.Status start() {
// Server start.
}
override void permissions() {
// Register your module permissions here.
// See docs here.
}
override void collections() {
// Create your collections and APIs here.
// See docs here.
}
}`);
console.log(chalk.green("[CREATED] " + name));
}else{
(async () => {
let server = new Server({
dev: true,
base: website,
config: "./config",
dist: "./dist",
assets: "./website",
buckets: "./buckets",
headless: false,
openInBrowser: false,
verbose: false
});
await server.startServices();
await server.micro.command.exec(args.join(" "), async () => {
await server.stop();
process.exit();
});
})();
}
}