-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
70 lines (56 loc) · 1.72 KB
/
dev.js
File metadata and controls
70 lines (56 loc) · 1.72 KB
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
#!/usr/bin/env node
const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')
const os = require('os')
const getPort = () => {
const portsPath = path.join(os.homedir(), 'Code', 'ports.csv')
if (!fs.existsSync(portsPath)) {
console.log('⚠️ ~/Code/ports.csv not found, using default port')
return null
}
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const projectName = packageJson.name
const csv = fs.readFileSync(portsPath, 'utf8')
const lines = csv.trim().split('\n').slice(1)
for (const line of lines) {
const [name, port] = line.split(',')
if (name === projectName) {
return parseInt(port, 10)
}
}
console.log(`⚠️ ${projectName} not found in ports.csv, using default port`)
return null
}
const port = getPort()
const serveArgs = port ? ['serve', 'build', '-l', port.toString()] : ['serve', 'build']
if (port) console.log(`🚀 Starting dev server on port ${port}...\n`)
const build = spawn('node', ['build', '--dev', '--watch'], { stdio: 'inherit' })
const serve = spawn('npx', serveArgs, { stdio: 'inherit' })
build.on('error', err => {
console.error('❌ Build process failed:', err.message)
process.exit(1)
})
build.on('exit', code => {
if (code !== 0) {
console.error(`❌ Build process exited with code ${code}`)
serve.kill()
process.exit(code)
}
})
serve.on('error', err => {
console.error('❌ Serve process failed:', err.message)
process.exit(1)
})
serve.on('exit', code => {
if (code !== 0) {
console.error(`❌ Serve process exited with code ${code}`)
build.kill()
process.exit(code)
}
})
process.on('SIGINT', () => {
build.kill()
serve.kill()
process.exit()
})