|
1 | 1 | #!/usr/bin/env node |
2 | | -import { accessSync, readFileSync, unlinkSync, writeFileSync } from "fs"; |
3 | | -import spawn from "cross-spawn"; |
4 | | -import tempDir from "temp-dir"; // /tmp |
5 | | -import yargs from "yargs/yargs"; |
6 | | -import { hideBin } from "yargs/helpers"; |
| 2 | +/* eslint-env node */ |
| 3 | +/* global console, process */ |
| 4 | +import { accessSync, readFileSync, unlinkSync, writeFileSync } from 'fs' |
| 5 | +import spawn from 'cross-spawn' |
| 6 | +import tempDir from 'temp-dir' // /tmp |
| 7 | +import yargs from 'yargs/yargs' |
| 8 | +import { hideBin } from 'yargs/helpers' |
7 | 9 |
|
8 | | -const pidFile = `${tempDir}/debug-server-hmDAr917.pid`; |
9 | | -const port = 3000; |
| 10 | +const pidFile = `${tempDir}/debug-server-hmDAr917.pid` |
| 11 | +const port = 3000 |
10 | 12 |
|
| 13 | +/** |
| 14 | + * Ensure we do not start a duplicate dev server instance |
| 15 | + */ |
11 | 16 | const failOnPidFileExists = () => { |
12 | 17 | try { |
13 | | - accessSync(pidFile); |
14 | | - } catch (err) { |
15 | | - return false; |
| 18 | + accessSync(pidFile) |
| 19 | + } catch { |
| 20 | + return false |
16 | 21 | } |
17 | 22 | console.error( |
18 | 23 | `[error] Server appears to be running, found PID file ${pidFile}`, |
19 | | - ); |
20 | | - return process.exit(116); |
21 | | -}; |
| 24 | + ) |
| 25 | + return process.exit(116) |
| 26 | +} |
22 | 27 |
|
23 | | -const writePidFile = (pid) => { |
| 28 | +/** |
| 29 | + * Persist the child process PID to allow stop command to find it later |
| 30 | + */ |
| 31 | +const writePidFile = pid => { |
24 | 32 | try { |
25 | | - writeFileSync(pidFile, pid.toString(10), { flag: "ax" }); |
| 33 | + writeFileSync(pidFile, pid.toString(10), { flag: 'ax' }) |
26 | 34 | } catch (err) { |
27 | | - console.error(`[error] Failed to write PID ${pid} PID file ${pidFile}: ${err.message}`); |
28 | | - return process.exit(11); |
| 35 | + console.error(`[error] Failed to write PID ${pid} PID file ${pidFile}: ${err.message}`) |
| 36 | + return process.exit(11) |
29 | 37 | } |
30 | 38 | } |
31 | 39 |
|
| 40 | +/** |
| 41 | + * Remove the PID file when the dev server stops |
| 42 | + */ |
32 | 43 | const unlinkPidFile = () => { |
33 | 44 | try { |
34 | | - unlinkSync(pidFile); |
| 45 | + unlinkSync(pidFile) |
35 | 46 | } catch (err) { |
36 | 47 | console.error( |
37 | 48 | `[error] Failed to delete PID file ${pidFile}: ${err.message}`, |
38 | | - ); |
39 | | - return process.exit(11); |
| 49 | + ) |
| 50 | + return process.exit(11) |
40 | 51 | } |
41 | | -}; |
| 52 | +} |
42 | 53 |
|
43 | 54 | yargs(hideBin(process.argv)) |
44 | 55 | .command( |
45 | | - "start", |
46 | | - "Start the server", |
| 56 | + 'start', |
| 57 | + 'Start the server', |
47 | 58 | {}, |
48 | 59 | () => { |
49 | | - failOnPidFileExists(); |
| 60 | + failOnPidFileExists() |
50 | 61 |
|
51 | | - const child = spawn(`${process.cwd()}/node_modules/.bin/vercel`, ["dev", "--listen", port], { |
| 62 | + const child = spawn(`${process.cwd()}/node_modules/.bin/vercel`, ['dev', '--listen', port], { |
52 | 63 | detached: true, |
53 | | - stdio: "pipe", |
54 | | - }); |
| 64 | + stdio: 'pipe', |
| 65 | + }) |
55 | 66 |
|
56 | | - let isPidWritten = false; |
| 67 | + let isPidWritten = false |
57 | 68 |
|
58 | | - child.on("error", (err) => { |
59 | | - console.log(err); |
60 | | - }); |
| 69 | + child.on('error', error => { |
| 70 | + console.error(error) |
| 71 | + }) |
61 | 72 |
|
62 | 73 | /** Forward child stdout to VS Code Task */ |
63 | | - child.stdout.setEncoding("utf8"); |
64 | | - child.stdout.on("data", (data) => { |
| 74 | + child.stdout.setEncoding('utf8') |
| 75 | + child.stdout.on('data', data => { |
65 | 76 | if (!isPidWritten) { |
66 | | - writePidFile(child.pid); |
67 | | - isPidWritten = true; |
| 77 | + writePidFile(child.pid) |
| 78 | + isPidWritten = true |
68 | 79 | } |
69 | | - console.log(data); |
70 | | - }); |
| 80 | + console.log(data) |
| 81 | + }) |
71 | 82 |
|
72 | | - child.stderr.setEncoding("utf8"); |
73 | | - child.stderr.on("data", (error) => { |
74 | | - console.error(error); |
75 | | - }); |
| 83 | + child.stderr.setEncoding('utf8') |
| 84 | + child.stderr.on('data', error => { |
| 85 | + console.error(error) |
| 86 | + }) |
76 | 87 |
|
77 | | - child.on("close", (code) => { |
78 | | - child.unref(); |
79 | | - return process.exit(code); |
80 | | - }); |
| 88 | + child.on('close', code => { |
| 89 | + child.unref() |
| 90 | + return process.exit(code) |
| 91 | + }) |
81 | 92 | }, |
82 | 93 | ) |
83 | 94 | .command( |
84 | | - "stop", |
85 | | - "Stop the server", |
| 95 | + 'stop', |
| 96 | + 'Stop the server', |
86 | 97 | {}, |
87 | 98 | () => { |
88 | 99 | try { |
89 | | - const pid = readFileSync(pidFile, { encoding: "utf8", flag: "r" }); |
90 | | - process.kill(parseInt(pid, 10), "SIGINT"); |
91 | | - unlinkPidFile(); |
92 | | - return process.exit(0); |
| 100 | + const pid = readFileSync(pidFile, { encoding: 'utf8', flag: 'r' }) |
| 101 | + process.kill(parseInt(pid, 10), 'SIGINT') |
| 102 | + unlinkPidFile() |
| 103 | + return process.exit(0) |
93 | 104 | } catch (err) { |
94 | 105 | console.error( |
95 | 106 | `[error] Failed to shut down server, reading PID file ${pidFile}: ${err.message}`, |
96 | | - ); |
97 | | - return process.exit(11); |
| 107 | + ) |
| 108 | + return process.exit(11) |
98 | 109 | } |
99 | 110 | }, |
100 | 111 | ) |
101 | | - .parse(); |
| 112 | + .parse() |
0 commit comments