-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
Β·287 lines (247 loc) Β· 11.1 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
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
#!/usr/bin/env node
import path from 'path';
import sade from 'sade';
import {
addModelToDBFile, createAPIRoutes, createController,
createModel, createRoutePages, init, createFormComponent, writeDeleteButtonComponent, createHooksServerFile, readConfig, addNewRouteOrComponent, getDebugValue, setDebugValue,
createSitemap
} from './lib/functions.js';
import { spawn } from 'child_process';
import { destroyButtonTemplate } from './lib/templates/component_templates.js';
import { CONSOLE_COLOR, styledBy } from './lib/helpers.js';
import { readFileSync } from 'fs';
let pjson;
try {
const pkgPath = new URL('./package.json', import.meta.url);
const pkgData = readFileSync(pkgPath);
pjson = JSON.parse(pkgData);
} catch (err) {
console.error('Error reading package.json:', err);
}
const prog = sade('chic');
prog
.version(pjson.version);
prog
.command('new <name>')
.describe('Create a new Sveltekit app. To add styles, add "styled with tailwind", for example.')
.example('new myapp')
.example('new myapp styled with tailwind')
.action((name, options, opts) => {
console.log('\x1b[36m%s\x1b[0m', `β’ Creating a new Sveltekit project named ${name}`);
const [ isStyled, styleFrameworkName, styleInstallCommand, styleDocsURL ] = styledBy(options);
const createSvelteProcess = spawn('npm', ['create', 'svelte@latest', name], {
stdio: 'inherit', // this will show the live output of the command
shell: true
});
createSvelteProcess.on('error', (error) => {
console.error(`Error creating the project: ${error}`);
});
createSvelteProcess.on('exit', (code) => {
if (code !== 0) {
console.error(`The process exited with code ${code}`);
} else {
console.log('Project created successfully!');
// Change directory in the Node.js process
process.chdir(path.join(process.cwd(), name));
// Run init function
init();
// Install sequelize
console.log(`> Installing sequelize in project ${name}`);
const installSequelizeProcess = spawn('npm', ['install', 'sequelize', '--save'], {
stdio: 'inherit',
shell: true
});
installSequelizeProcess.on('error', (error) => {
console.error(`Error installing sequelize: ${error}`);
});
installSequelizeProcess.on('exit', (code) => {
if (code !== 0) {
console.error(`The process exited with code ${code}`);
} else {
console.log('Sequelize installed successfully');
// Install Sqlite
console.log(`> Installing sqlite3 in project ${name}`);
const installSsqlite3Process = spawn('npm', ['install', 'sqlite3', '--save'], {
stdio: 'inherit',
shell: true
});
installSsqlite3Process.on('error', (error) => {
console.error(`Error installing sqlite3: ${error}`);
});
installSsqlite3Process.on('exit', (code) => {
if (code !== 0) {
console.error(`The process exited with code ${code}`);
} else {
console.log('Sqlite3 installed successfully');
console.log('\x1b[36m%s\x1b[0m', `----------------------------------------`);
console.log("Project created successfully!");
if (isStyled) {
addStylesToProject(styleFrameworkName, styleInstallCommand, styleDocsURL);
}
console.log(`
π Next steps:
--------------
1: cd ${name}
2: npm install
3: chic make <resource> <fields>
4: chic s
`)
console.log('\x1b[36m%s\x1b[0m', `To start the server run: chic s`);
console.log(CONSOLE_COLOR.GREEN, `Donate to support us: https://ko-fi.com/sveltesafari`);
}
});
}
});
}
});
});
prog
.command('add <what>')
.describe('Adds a new route or component to your project.\n Put a / in front to create a new route, otherwise it will create a new component.\n See examples below.')
.example('add /about')
.example('add Login')
.action((what, options, opts) => {
addNewRouteOrComponent(what);
});
prog
.command('debug <command>')
.describe('Checks and sets CHIC_DEBUG mode. If ON, the routes endpoint will be active. Be sure to set CHIC_DEBUG=OFF in your .env file before deploying.')
.example('chic debug status')
.example('chic debug ON')
.example('chic debug OFF')
.action((command) => {
const v = command.toUpperCase();
switch (v) {
case "STATUS":
console.log(!getDebugValue() ? CONSOLE_COLOR.RED : CONSOLE_COLOR.BLUE, `β’ CHIC_DEBUG is currently ${!getDebugValue() ? "not specified in your .env file" : getDebugValue()}`);
break;
case "ON":
console.log('\x1b[36m%s\x1b[0m', `β’ Turning on debug mode...`);
setDebugValue("ON");
break;
case "OFF":
console.log('\x1b[36m%s\x1b[0m', `β’ Turning off debug mode...`);
setDebugValue("OFF");
break;
default:
console.log('\x1b[36m%s\x1b[0m', `CHIC_DEBUG is currently ${!getDebugValue() ? "not specified in your .env file" : getDebugValue()}`);
break;
}
});
prog
.command('make <what> <options>')
.describe('Scaffolds a new MVC resource. Options are the field names of the model. You can also use a config file by running "chic make from file"')
// .option('-d, --database', 'What kind of database should be used?')
.example('make Guitar name:string brand:string price:number')
.example('make from file')
.action((what, options, opts) => {
console.log(`> Scaffolding a new ${what}`);
// console.log('> Model fields', options);
const allOptions = [options, ...opts._].join(' ');
if(what === "from" && options === "file") {
console.log('\x1b[36m%s\x1b[0m', `β’ Making from config file...`);
const config = readConfig();
for (let index = 0; index < config.models.length; index++) {
console.log(index);
const model = config.models[index];
console.log('\x1b[36m%s\x1b[0m', `β’ Making ${model.name}...`);
createModel(model.name, null, config.models[index].fields);
addModelToDBFile(model.name);
createFormComponent(model.name, ['new', '[id]/edit'], allOptions);
if (index === 0) writeDeleteButtonComponent(destroyButtonTemplate);
createRoutePages(model.name, ['', 'new', '[id]', '[id]/edit'], allOptions);
createAPIRoutes(model.name, ['', '[id]']);
createController(model.name);
createHooksServerFile();
}
} else {
createModel(what, allOptions, null);
addModelToDBFile(what);
createFormComponent(what, ['new', '[id]/edit'], allOptions);
writeDeleteButtonComponent(destroyButtonTemplate); // TODO: only create once.
createRoutePages(what, ['', 'new', '[id]', '[id]/edit'], allOptions);
createAPIRoutes(what, ['', '[id]']);
createController(what);
createHooksServerFile();
}
});
prog
.command('routes')
.describe('Updates routes.')
// .option('-d, --database', 'What kind of database should be used?')
.action((what, options, opts) => {
createHooksServerFile();
});
prog
.command('s')
.describe('Starts the development server')
.action(() => {
console.log(`> Running development server`);
console.log("");
console.log('\x1b[36m%s\x1b[0m', `To stop server, hit Ctrl+C`);
const runServerProcess = spawn('npm', ['run', 'dev'], {
stdio: 'inherit', // this will show the live output of the command
shell: true
});
runServerProcess.on('error', (error) => {
console.error(`Error creating the project: ${error}`);
});
runServerProcess.on('exit', (code) => {
if (code !== 0) {
console.error(`The server stopped.`);
} else {
console.log('Server ran successfully.');
}
});
});
prog
.command('sitemap <url>')
.describe('Generates a sitemap.xml file for your project.')
.example('sitemap https://example.com')
.action((url) => {
createSitemap(url);
});
// if command not found, show help
prog
.command('*', '', { default: true })
.action(() => {
console.log(CONSOLE_COLOR.BLUE, "ββββ¬ β¬β¬βββ β¬βββ");
console.log(CONSOLE_COLOR.BLUE, "β βββ€ββ ββββ");
console.log(CONSOLE_COLOR.BLUE, "ββββ΄ β΄β΄βββoβββββ");
console.log(CONSOLE_COLOR.BLUE, `Version: ${pjson.version}`);
console.log(CONSOLE_COLOR.BLUE, `Author: Mark Schellhas`);
console.log(CONSOLE_COLOR.BLUE, `For help, run chic --help`);
console.log(CONSOLE_COLOR.GREEN, `--------------------`);
console.log(CONSOLE_COLOR.GREEN, `Donate to support this project: https://ko-fi.com/sveltesafari`);
console.log(CONSOLE_COLOR.GREEN, `--------------------`);
});
prog.parse(process.argv);
/**
*
* @param {String} styleFrameworkName
* @param {Array<string>} styleInstallCommand
* @param {String} styleDocsURL
*/
function addStylesToProject(styleFrameworkName, styleInstallCommand, styleDocsURL) {
console.log('\x1b[36m%s\x1b[0m', `β’ Styling the project with ${styleFrameworkName}`);
console.log('\x1b[36m%s\x1b[0m', `β’ Installing ${styleFrameworkName}...`);
let styleCmnd = styleInstallCommand.split(' ');
let styledOptions = styleCmnd.slice(1);
console.log(styleCmnd);
console.log(styledOptions);
const installStyleProcess = spawn(styleCmnd[0], styledOptions, {
stdio: 'inherit',
shell: true
});
installStyleProcess.on('error', (error) => {
console.error(`Error installing ${styleFrameworkName}: ${error}`);
});
installStyleProcess.on('exit', (code) => {
if (code !== 0) {
console.error(`The process exited with code ${code}`);
} else {
console.log(`${styleFrameworkName} installed successfully`);
console.log('\x1b[36m%s\x1b[0m', `β’ For more info: ${styleDocsURL}...`);
}
});
}