-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin.js
executable file
Β·274 lines (242 loc) Β· 7.9 KB
/
bin.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
#!/usr/bin/env node
// @ts-ignore
import { readFile } from 'node:fs/promises'
import { resolve, join, relative } from 'node:path'
import { parseArgs } from 'node:util'
import { printHelpText } from 'argsclopts'
import readline from 'node:readline'
import process from 'process'
// @ts-ignore
import tree from 'pretty-tree'
import { inspect } from 'util'
import { packageDirectory } from 'pkg-dir'
import { readPackage } from 'read-pkg'
import { addPackageDependencies } from 'write-package'
import { copyFile } from './lib/helpers/copy-file.js'
import { TopBun } from './index.js'
import { TopBunAggregateError } from './lib/helpers/top-bun-aggregate-error.js'
import { generateTreeData } from './lib/helpers/generate-tree-data.js'
import { askYesNo } from './lib/helpers/cli-prompt.js'
/**
* @import {TopBunOpts} from './lib/builder.js'
* @import { ArgscloptsParseArgsOptionsConfig } from 'argsclopts'
*/
const __dirname = import.meta.dirname
async function getPkg () {
const pkgPath = resolve(__dirname, './package.json')
const pkg = JSON.parse(await readFile(pkgPath, 'utf8'))
return pkg
}
/** @type {ArgscloptsParseArgsOptionsConfig} */
const options = {
src: {
type: 'string',
short: 's',
default: 'src',
help: 'path to source directory',
},
dest: {
type: 'string',
short: 'd',
default: 'public',
help: 'path to build destination directory',
},
ignore: {
type: 'string',
short: 'i',
help: 'comma separated gitignore style ignore string',
},
drafts: {
type: 'boolean',
help: 'Build draft pages with the `.draft.{md,js,html}` page suffix.',
default: false
},
target: {
type: 'string',
short: 't',
help: 'comma separated target strings for esbuild',
},
noEsbuildMeta: {
type: 'boolean',
help: 'skip writing the esbuild metafile to disk',
},
eject: {
type: 'boolean',
short: 'e',
help: 'eject the top bun default layout, style and client into the src flag directory',
},
watch: {
type: 'boolean',
short: 'w',
help: 'build, watch and serve the site build',
},
'watch-only': {
type: 'boolean',
help: 'watch and build the src folder without serving',
},
help: {
type: 'boolean',
short: 'h',
help: 'show help',
},
version: {
type: 'boolean',
short: 'v',
help: 'show version information',
},
}
const { values: argv } = parseArgs({ options })
async function run () {
if (argv['version']) {
const pkg = await getPkg()
console.log(pkg.version)
process.exit(0)
}
if (argv['help']) {
const pkg = await getPkg()
await printHelpText({
options,
name: pkg.name,
version: pkg.version,
exampleFn: ({ name }) => ' ' + `Example: ${name} --src website --dest public\n`,
})
process.exit(0)
}
const cwd = process.cwd()
const srcFlag = String(argv['src'])
const destFlag = String(argv['dest'])
if (!srcFlag) throw new Error('The src flag is required')
if (!destFlag) throw new Error('The dest flag is required')
const src = resolve(join(cwd, srcFlag))
const dest = resolve(join(cwd, destFlag))
// Eject task
if (argv['eject']) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const localPkg = await packageDirectory({ cwd: src })
if (!localPkg) {
console.error('Can\'t locate package.json, exiting without making changes')
process.exit(1)
}
const localPkgJson = join(localPkg, 'package.json')
const localPkgJsonContents = await readPackage({ cwd: localPkg })
const targetIsModule = localPkgJsonContents.type === 'module'
const relativeSrc = relative(process.cwd(), src)
const relativePkg = relative(process.cwd(), localPkgJson)
const targetLayoutPath = `layouts/root.layout.${targetIsModule ? 'js' : 'mjs'}`
const targetGlobalStylePath = 'globals/global.css'
const targetGlobalClientPath = `globals/global.client.${targetIsModule ? 'js' : 'mjs'}`
const tbPkgContents = await readPackage({ cwd: __dirname })
const mineVersion = tbPkgContents?.['dependencies']?.['mine.css']
const uhtmlVersion = tbPkgContents?.['dependencies']?.['uhtml-isomorphic']
const highlightVersion = tbPkgContents?.['dependencies']?.['highlight.js']
if (!mineVersion || !uhtmlVersion || !highlightVersion) {
console.error('Unable to resolve ejected depdeency versions. Exiting...')
process.exit(1)
}
console.log(`
top-bun eject actions:
- Write ${join(relativeSrc, targetLayoutPath)}
- Write ${join(relativeSrc, targetGlobalStylePath)}
- Write ${join(relativeSrc, targetGlobalClientPath)}
- Add mine.css@${mineVersion} to ${relativePkg}
- Add uhtml-isomorphic@${uhtmlVersion} to ${relativePkg}
- Add highlight.js@${highlightVersion} to ${relativePkg}
`)
const answer = await askYesNo(rl, 'Continue?')
if (answer === false) {
console.log('No action taken. Exiting.')
process.exit(0)
}
const defaultLayoutPath = join(__dirname, 'lib/defaults/default.root.layout.js')
const defaultGlobalStylePath = join(__dirname, 'lib/defaults/default.style.css')
const defaultGlobalClientPath = join(__dirname, 'lib/defaults/default.client.js')
await Promise.all([
copyFile(defaultLayoutPath, join(src, targetLayoutPath)),
copyFile(defaultGlobalStylePath, join(src, targetGlobalStylePath)),
copyFile(defaultGlobalClientPath, join(src, targetGlobalClientPath)),
])
await addPackageDependencies(
localPkgJson,
{
dependencies: {
'mine.css': mineVersion,
'uhtml-isomorphic': uhtmlVersion,
'highlight.js': highlightVersion,
},
})
console.log('Done ejecting files!')
process.exit(0)
}
/** @type {TopBunOpts} */
const opts = {}
if (argv['ignore']) opts.ignore = String(argv['ignore']).split(',')
if (argv['target']) opts.target = String(argv['target']).split(',')
if (argv['noEsbuildMeta']) opts.metafile = false
if (argv['drafts']) opts.buildDrafts = true
const topBun = new TopBun(src, dest, opts)
process.once('SIGINT', quit)
process.once('SIGTERM', quit)
async function quit () {
if (topBun.watching) {
const results = await topBun.stopWatching()
console.log(results)
console.log('watching stopped')
}
console.log('\nquitting cleanly')
process.exit(0)
}
if (!argv['watch'] && !argv['watch-only']) {
try {
const results = await topBun.build()
console.log(tree(generateTreeData(cwd, src, dest, results)))
if (results?.warnings?.length > 0) {
console.log(
'\nThere were build warnings:\n'
)
}
for (const warning of results?.warnings) {
if ('message' in warning) {
console.log(` ${warning.message}`)
} else {
console.warn(warning)
}
}
console.log('\nBuild Success!\n\n')
} catch (err) {
if (!(err instanceof Error || err instanceof AggregateError)) throw new Error('Non-error thrown', { cause: err })
if (err instanceof TopBunAggregateError) {
if (err?.results?.siteData?.pages) {
console.log(tree(generateTreeData(cwd, src, dest, err.results)))
}
}
if ('results' in err) delete err.results
console.error(inspect(err, { depth: 999, colors: true }))
console.log('\nBuild Failed!\n\n')
process.exit(1)
}
} else {
const initialResults = await topBun.watch({
serve: !argv['watch-only'],
})
console.log(tree(generateTreeData(cwd, src, dest, initialResults)))
if (initialResults?.warnings?.length > 0) {
console.log(
'\nThere were build warnings:\n'
)
}
for (const warning of initialResults?.warnings) {
if ('message' in warning) {
console.log(` ${warning.message}`)
} else {
console.warn(warning)
}
}
}
}
run().catch(err => {
console.error(new Error('Unhandled top-bun error', { cause: err }))
process.exit(1)
})