-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
115 lines (98 loc) · 4.33 KB
/
index.mjs
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
#! /usr/bin/env node
import { Command } from 'commander'
import chalk from 'chalk'
import { mkdir, writeFile, readFile } from 'fs/promises'
const controller = new AbortController();
const { signal } = controller;
const program = new Command()
program
.name('scaffolda')
.description('scaffold a web development folder structure')
.version('1.0.0')
program
.command('create')
.description('scaffold an application with starter folders and files')
.argument('<string>', 'App name')
.option('-w, --web', 'create a basic web application with html, css, js')
.option('-b, --bootstrap', 'include the latest bootstrap cdn')
.option('-t, --test', 'include folder for testing')
.action((str, options) => {
if (options.web) {
if (options.bootstrap) {
let template = null;
readFile(new URL('template.html', import.meta.url), 'utf-8')
.then(res => {
template = res;
const data = {
style: `
<!-- Bootstrap 5.2.2 stylesheet cdn -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
`,
script: `
<!-- Bootstrap 5.2.2 script cdn -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
`
}
for (const [k, v] of Object.entries(data)) {
template = template.replace(`{${k}}`, v)
}
writeFile(new URL('template.html', import.meta.url), template, { signal })
.then(res => {
return res
})
.catch(err => {
throw err
})
})
}
let dir = ['css', 'js', 'img', 'utils', 'fonts']
let color = ['bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan']
let dirFile = {
css: 'styles.css',
js: 'app.js',
}
if (options.test) {
dirFile = {
...dirFile,
test: 'app.spec.js'
}
dir.push('test')
color.push('bgCyanBright')
}
mkdir(`./${str}`)
.then(res => {
writeFile(`./${str}/index.html`, '', { signal })
.then(res => {
readFile(new URL('template.html', import.meta.url), { encoding: 'utf-8' })
.then(response => {
writeFile(`./${str}/index.html`, response, { signal })
})
})
.catch(err => {
throw err
})
})
.catch(err => {
throw err
})
dir.forEach((v, i) => {
mkdir(`./${str}/${v}`, {recursive: true})
.then(res => {
dirFile[v] ?
writeFile(`./${str}/${v}/${dirFile[v]}`, '/* welcome to scaffolda */', { signal }) :
null
})
.then(e => {
dirFile[v] ?
console.log(chalk[`${color[i]}`].white.bold(`✅${str}/${v}/${dirFile[v]} scaffolded`)) :
console.log(chalk[`${color[i]}`].white.bold(`✅${str}/${v} scaffolded`))
})
.catch(err => {
throw err
})
})
} else {
console.log(chalk.bgWhite.redBright.bold('❌ You omitted the --web option'))
}
})
program.parse();