-
Notifications
You must be signed in to change notification settings - Fork 1
/
singlighter.js
174 lines (158 loc) · 5.27 KB
/
singlighter.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
const fs = require("fs");
const http = require("http");
const command = process.argv[2];
const archive = {
page: `import { Page } from '../Singlight.js';
export default class {{PAGENAME}} extends Page {
template() {
return /*html*/\`
<!-- You're HTML codes must be here -->
\`;
}
setup() {
// ...
}
}`,
accessor: `export default function {{NAME}}() {
return true; // or false
}`,
component: `export default function {{NAME}}() {
return /*html*/\`
<!-- You're HTML codes must be here -->
\`;
}`,
hook: `export default function {{NAME}}() {
// You're JavaScript codes must be here
}`,
help: `Usage:
node singlighter <command> [option]
Commands:
version => Get singlighter version
help => Get help
make:page => Create new page (option is required)
make:accessor => Create new accessor (option is required)
make:component => Create new component (option is required)
make:hook => Create new hook (option is required)
serve => Serving project on localhost`
}
switch (command) {
case undefined: {
infoHelper("Hi! I'm here ...");
break;
}
case "version": {
infoHelper("v2.3.8");
break;
}
case "help": {
infoHelper(archive.help);
break;
}
case "make:page": {
if (process.argv.length < 4)
errorHelper("Bad usage:\n\tYou must set a name for your page")
else
makePageCmd(process.argv[3]);
break;
}
case "make:accessor": {
if (process.argv.length < 4)
errorHelper("Bad usage:\n\tYou must set a name for your accessor")
else
makeAccessorCmd(process.argv[3]);
break;
}
case "make:component": {
if (process.argv.length < 4)
errorHelper("Bad usage:\n\tYou must set a name for your component")
else
makeComponentCmd(process.argv[3]);
break;
}
case "make:hook": {
if (process.argv.length < 4)
errorHelper("Bad usage:\n\tYou must set a name for your hook")
else
makeHookCmd(process.argv[3]);
break;
}
case "serve": {
if (process.argv.length < 4)
serveCmd();
else
!isNaN(process.argv[3]) ? serveCmd(Number(process.argv[3])) : errorHelper("Port must be numeric")
break;
}
default: {
errorHelper(`Bad usage:\n\t'${command}' is not a command, use 'help' command for see Methods of usage`);
break;
}
}
function errorHelper(text) {
console.error('\x1b[31m%s\x1b[0m', text);
}
function successHelper(text) {
console.log("\x1b[32m%s\x1b[0m", text);
}
function infoHelper(text) {
console.info("\x1b[34m%s\x1b[0m", text);
}
function makePageCmd(pageName) {
if (fs.existsSync(`./Scripts/Pages/${pageName}.js`))
errorHelper(`'${pageName}' already exists`)
else {
fs.writeFileSync(`./Scripts/Pages/${pageName}.js`, archive.page.replace("{{PAGENAME}}", pageName));
successHelper("Page was created successfully")
}
}
function makeAccessorCmd(accessorName) {
if (fs.existsSync(`./Scripts/Accessors/${accessorName}.js`))
errorHelper(`'${accessorName}' already exists`)
else {
fs.writeFileSync(`./Scripts/Accessors/${accessorName}.js`, archive.accessor.replace("{{NAME}}", accessorName));
successHelper("Accessor was created successfully")
}
}
function makeComponentCmd(componentName) {
if (fs.existsSync(`./Scripts/Components/${componentName}.js`))
errorHelper(`'${componentName}' already exists`)
else {
fs.writeFileSync(`./Scripts/Components/${componentName}.js`, archive.component.replace("{{NAME}}", componentName));
successHelper("Component was created successfully")
}
}
function makeHookCmd(hookName) {
if (fs.existsSync(`./Scripts/Hooks/${hookName}.js`))
errorHelper(`'${hookName}' already exists`)
else {
fs.writeFileSync(`./Scripts/Hooks/${hookName}.js`, archive.hook.replace("{{NAME}}", hookName));
successHelper("Hook was created successfully")
}
}
function serveCmd(port=8000) {
const serverStarted = () => successHelper(`Server was started on [http://localhost:${port}] - Press Ctrl+C for stop server`);
const serverHandler = (req, res) => {
req.url = "." + req.url;
if (!fs.existsSync(req.url)) req.url = "/";
url = fs.lstatSync(req.url).isFile() ? "./" + req.url : `./${req.url}/index.html`;
let output = fs.readFileSync(url);
if (url.substring(url.length-2, url.length) == 'js') res.setHeader('Content-Type', 'text/javascript');
res.write(output);
res.end();
};
let server = http.createServer(serverHandler).listen(port);
const handleError = () => {
errorHelper(`Port ${port} is busy, Try port ${port+1}`)
port++;
server = http.createServer(serverHandler).listen(port);
server.on("error", () => handleError());
server.on("listening", serverStarted);
};
server.on("error", handleError);
server.on("listening", serverStarted);
fs.watch(".", () => {
successHelper("Files was modified, Restarting server ...");
server.close();
server = http.createServer(serverHandler).listen(port);
})
}