-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin.js
173 lines (157 loc) · 4.51 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
#!/usr/bin/env node
import fs from 'node:fs'
import path from 'node:path'
import spawn from 'cross-spawn'
import { config } from './config.js'
import { getMyPackageJson } from './lib.js'
const argv = process.argv
const myPkg = getMyPackageJson('./package.json')
const viteConfigSite = `node_modules/${myPkg.name}/vite.config.site.js`
const viteConfigDist = `node_modules/${myPkg.name}/vite.config.dist.js`
const viteConfigLib = `node_modules/${myPkg.name}/vite.config.lib.js`
const command = argv?.[2]
const allowed = (
command === 'dev' ||
command === 'build' ||
command === 'preview' ||
command === 'vite'
)
const htmlIndex = path.resolve(
process.cwd(),
path.join(config.src.root, 'index.html')
)
const jsIndex = path.resolve(
process.cwd(),
path.join(config.src.root, 'index.js')
)
const libIndex = path.resolve(
process.cwd(),
path.join(config.src.root, 'index.lib.js')
)
if ((argv.length > 3 && command !== 'vite') || !allowed) {
console.log()
console.log('Invalid command')
console.log()
console.log('Available commands:')
console.log()
console.log(`$ ${myPkg.name} dev (Start dev server and rebuilds when source files have changed)`)
console.log(`$ ${myPkg.name} build (Build for production)`)
console.log(`$ ${myPkg.name} preview (Locally preview production build)`)
console.log(`$ ${myPkg.name} vite (Run Vite commands)`)
process.exit(1)
}
if (command === 'dev') {
if (fs.existsSync(htmlIndex) && config.out.site !== false) {
spawn('vite', ['--config', viteConfigSite], {
stdio: 'inherit',
cwd: process.cwd()
})
} else {
if (fs.existsSync(jsIndex) && config.out.dist !== false) {
if (fs.existsSync(libIndex) && config.out.lib !== false) {
spawn('nodemon', [
'--watch', config.src.root,
'--ext', '*',
'--exec', `vite build --config ${viteConfigDist} && vite build --config ${viteConfigLib}`
], {
stdio: 'inherit',
cwd: process.cwd()
})
} else {
spawn('nodemon', [
'--watch', config.src.root,
'--ext', '*',
'--exec', `vite build --config ${viteConfigDist}`
], {
stdio: 'inherit',
cwd: process.cwd()
})
}
} else {
if (fs.existsSync(libIndex) && config.out.lib !== false) {
spawn('nodemon', [
'--watch', config.src.root,
'--ext', '*',
'--exec', `vite build --config ${viteConfigLib}`
], {
stdio: 'inherit',
cwd: process.cwd()
})
}
}
}
if (
!fs.existsSync(htmlIndex) &&
!fs.existsSync(jsIndex) &&
!fs.existsSync(libIndex) &&
config.out.site !== false &&
config.out.dist !== false &&
config.out.lib !== false
) {
console.log('Error: need at least one entry point index.html/index.js/index.lib.js')
}
if (
config.out.site === false &&
config.out.dist === false &&
config.out.lib === false
) {
console.log('All config.out are disabled')
}
}
if (command === 'build') {
if (fs.existsSync(htmlIndex) && config.out.site !== false) {
spawn('vite', ['build', '--config', viteConfigSite], {
stdio: 'inherit',
cwd: process.cwd()
})
}
if (fs.existsSync(jsIndex) && config.out.dist !== false) {
spawn('vite', ['build', '--config', viteConfigDist], {
stdio: 'inherit',
cwd: process.cwd()
})
}
if (fs.existsSync(libIndex) && config.out.lib !== false) {
spawn('vite', ['build', '--config', viteConfigLib], {
stdio: 'inherit',
cwd: process.cwd()
})
}
if (
!fs.existsSync(htmlIndex) &&
!fs.existsSync(jsIndex) &&
!fs.existsSync(libIndex) &&
config.out.site !== false &&
config.out.dist !== false &&
config.out.lib !== false
) {
console.log('Error: need at least one entry point index.html/index.js/index.lib.js')
}
if (
config.out.site === false &&
config.out.dist === false &&
config.out.lib === false
) {
console.log('All config.out are disabled')
}
}
if (command === 'preview') {
if (fs.existsSync(htmlIndex) && config.out.site !== false) {
spawn('vite', ['preview', '--config', viteConfigSite], {
stdio: 'inherit',
cwd: process.cwd()
})
}
if (!fs.existsSync(htmlIndex) && config.out.site !== false) {
console.log('Missing index.html entry point')
}
if (config.out.site === false) {
console.log('The config.out.site.dest is disabled')
}
}
if (command === 'vite') {
spawn('vite', [...argv.splice(3)], {
stdio: 'inherit',
cwd: process.cwd()
})
}