forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-next.js
More file actions
executable file
·50 lines (37 loc) · 1.22 KB
/
dev-next.js
File metadata and controls
executable file
·50 lines (37 loc) · 1.22 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
#!/usr/bin/env node
const fs = require('fs')
const { resolve } = require('path')
const childProcess = require('child_process')
const args = process.argv
const target = args[2]
const nextCmd = args[3] || 'dev'
function error(message) {
console.log(message)
process.exit(1)
}
if (!target) {
error(`Example ${target} is not found\n`)
}
const examplesDir = resolve(__dirname, '../examples')
const exampleDir = resolve(examplesDir, target)
const nextBin = resolve(exampleDir, 'node_modules/.bin/next')
if (!fs.existsSync(nextBin)) {
error(`Please run "yarn install && npx yalc link swr" inside ${target} example directory\n`)
}
const devCmd = `node ${nextBin} ${nextCmd}`.split(' ')
const sub = childProcess.spawn(devCmd.shift(), devCmd, { cwd: exampleDir })
const logStdout = data => process.stdout.write(data.toString())
const logStderr = data => process.stderr.write(data.toString())
sub.stdout.on('data', logStdout)
sub.stderr.on('data', logStderr)
sub.stderr.on('close', () => {
sub.stdout.off('data', logStdout)
sub.stderr.off('data', logStderr)
})
const teardown = () => {
if (sub.killed) sub.kill('SIGINT')
}
process.on('exit', teardown)
process.on('SIGINT', teardown)
// main
console.log(`Running ${devCmd.join(' ')}`)