-
Notifications
You must be signed in to change notification settings - Fork 1
/
poops.js
executable file
·226 lines (198 loc) · 7.36 KB
/
poops.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
#!/usr/bin/env node
const chokidar = require('chokidar')
const connect = require('connect')
const helpers = require('./lib/utils/helpers.js')
const http = require('node:http')
const livereload = require('livereload')
const Markups = require('./lib/markups.js')
const path = require('node:path')
const serveStatic = require('serve-static')
const Scripts = require('./lib/scripts.js')
const PrintStyle = require('./lib/utils/print-style.js')
const Styles = require('./lib/styles.js')
const portscanner = require('portscanner')
const { pathExists } = helpers
const cwd = process.cwd() // Current Working Directory
const pkg = require('./package.json')
const args = process.argv.slice(2)
const pstyle = new PrintStyle()
let build = false
let defaultConfigPath = 'poops.json'
let overridePort = null
let overrideLivereloadPort = null
for (let i = 0; i < args.length; i++) {
const arg = args[i]
switch (arg) {
case '-b':
case '--build':
build = true
break
case '-c':
case '--config':
if (args.length === i + 1 || args[i + 1].startsWith('-')) {
console.log(`${pstyle.redBright + pstyle.bold}[error]${pstyle.reset} Missing config file path`)
process.exit(1)
}
defaultConfigPath = args[i + 1]
i++
break
case '-p':
case '--port':
if (args.length === i + 1 || args[i + 1].startsWith('-') || isNaN(args[i + 1])) {
console.log(`${pstyle.redBright + pstyle.bold}[error]${pstyle.reset} Missing port number`)
process.exit(1)
}
overridePort = args[i + 1]
i++
break
case '-l':
case '--livereload':
if (args.length === i + 1 || args[i + 1].startsWith('-') || isNaN(args[i + 1])) {
console.log(`${pstyle.redBright + pstyle.bold}[error]${pstyle.reset} Missing livereload port number`)
process.exit(1)
}
overrideLivereloadPort = args[i + 1]
i++
break
case '-v':
case '--version':
console.log(pkg.version)
process.exit(0)
break
case '-h':
case '--help':
console.log(`Usage: ${pkg.name} [config-file] [options]
-b, --build\t\tBuild the project and exit
-c, --config\t\tSpecify the config file
-h, --help\t\tShow this help message
-l, --livereload\t\tSpecify the port to use for the livereload server, overrides the config file
-p, --port\t\tSpecify the port to use for the server, overrides the config file
-v, --version\t\tShow version number`)
process.exit(0)
break
default:
if (arg.startsWith('-')) {
console.log(`Unknown option: ${arg}`)
process.exit(1)
} else {
defaultConfigPath = arg
}
}
}
let configPath = path.join(cwd, defaultConfigPath)
if (!pathExists(configPath)) configPath = path.join(cwd, '💩.json') // TODO: Ok dude, I know it's late, but you can do better than this.
// Main function 💩
async function poops() {
let lport
if (config.livereload) {
lport = overrideLivereloadPort || config.livereload.port || 35729
if (!overrideLivereloadPort) lport = await getAvailablePort(lport, lport + 10)
config.livereload_port = lport
}
const styles = new Styles(config)
const scripts = new Scripts(config)
const markups = new Markups(config)
if (build || (!config.watch && !config.livereload && !config.serve)) {
await styles.compile()
await scripts.compile()
await markups.compile()
process.exit(0)
}
if (config.livereload) {
const lrExcludes = ['.git', '.svn', '.hg']
if (config.watch) {
lrExcludes.push(...config.watch)
}
if (config.includePaths) {
lrExcludes.push(...config.includePaths)
}
if (config.livereload.exclude) {
lrExcludes.push(...config.livereload.exclude)
}
const lrserver = livereload.createServer({
exclusions: [...new Set(lrExcludes)],
port: lport
})
console.log(`${pstyle.blue + pstyle.bold}[info]${pstyle.reset} 🔃${pstyle.dim} LiveReload server:${pstyle.reset} ${pstyle.italic + pstyle.underline}http://localhost:${lrserver.config.port}${pstyle.reset}\n`)
lrserver.watch(cwd)
}
await styles.compile()
await scripts.compile()
await markups.compile()
if (config.watch) {
// TODO: think about watching the updates of the config file itself, we can reload the config and recompile everything.
// TODO: ability to automatically create a watch list of directories if watch is set to true. The list will be generated from the `in` property of each task.
chokidar.watch(config.watch, { ignoreInitial: true }).on('change', (file) => {
if (/(\.m?js|\.ts)$/i.test(file)) scripts.compile()
if (/(\.sass|\.scss|\.css)$/i.test(file)) styles.compile()
if (/(\.html|\.xml|\.rss|\.atom|\.njk|\.md)$/i.test(file)) markups.compile()
// TODO: We can actually reload the page only if the data file from data has changed.
if (/(\.json|\.ya?ml)$/i.test(file)) {
markups.reloadDataFiles().then(() => {
markups.compile()
})
}
}).on('unlink', (file) => {
if (/(\.html|\.xml|\.rss|\.atom|\.njk|\.md)$/i.test(file)) markups.compile()
}).on('add', (file) => {
if (/(\.json|\.ya?ml)$/i.test(file)) {
markups.reloadDataFiles().then(() => {
markups.compile()
})
}
})
}
}
// CLI Header
const title = `💩 Poops — v${pkg.version}`
console.log(`\n${pstyle.color('#8b4513')}${title}
${title.replace(/./g, '-')}${pstyle.reset + pstyle.bell}\n`)
// Check if poops.json exists
if (!pathExists(configPath)) {
console.log(`${pstyle.redBright + pstyle.bold}[error]${pstyle.reset} \`${pstyle.underline}${defaultConfigPath}${pstyle.reset}\` or \`${pstyle.underline}💩.json${pstyle.reset}\` not found.
${pstyle.dim}Configuration file \`${defaultConfigPath}\` or \`💩.json\` not found in your working directory: ${pstyle.underline}${cwd}${pstyle.reset}\n
${pstyle.dim}Please specify another file path or create a \`poops.json\` or \`💩.json\` file in your working directory and try again.\n
${pstyle.dim}For information on the structure of the configuration file, please visit: \n${pstyle.underline}https://stamat.github.io/poops${pstyle.reset}\n`)
process.exit(1)
}
// Load poops.json
const config = require(configPath)
if (config.watch) {
config.watch = Array.isArray(config.watch) ? config.watch : [config.watch]
}
if (config.includePaths) {
config.includePaths = Array.isArray(config.includePaths) ? config.includePaths : [config.includePaths]
} else {
config.includePaths = ['node_modules']
}
async function getAvailablePort(port, max) {
while (port < max) {
const status = await portscanner.checkPortStatus(port, 'localhost')
if (status === 'closed') {
return port
} else {
port++
}
}
return port
}
async function startServer() {
const app = connect()
if (config.serve.base && pathExists(cwd, config.serve.base)) {
app.use(serveStatic(path.join(cwd, config.serve.base)))
} else {
app.use(serveStatic(cwd))
}
let port = overridePort || config.serve.port || 4040
if (!overridePort) port = await getAvailablePort(port, port + 10)
http.createServer(app).listen(parseInt(port), () => {
console.log(`${pstyle.blue + pstyle.bold}[info]${pstyle.reset} 🌍${pstyle.dim} Local server:${pstyle.reset} ${pstyle.italic + pstyle.underline}http://localhost:${port}${pstyle.reset}`)
poops()
})
}
// Start the webserver
if (!build && config.serve) {
startServer()
} else {
poops()
}