Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit a35b50f

Browse files
committed
Update lib/dev.js
1 parent a751d33 commit a35b50f

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

lib/dev.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const output = require('./console/output');
2+
const outputDev = require('./console/outputDev');
3+
const load = require("io-spin");
4+
const fs = require("fs-extra");
5+
const path = require("path");
6+
const pc = require("picocolors");
7+
8+
async function dev(options) {
9+
// check ./package.json
10+
if (!fs.existsSync(path.join(process.cwd(), "package.json"))) {
11+
output("package.json not found", "red");
12+
process.exit(1);
13+
}
14+
const startTime = Date.now();
15+
// close spin if dev
16+
outputDev("Spinners are disabled in dev mode");
17+
const spinner = fs.pathExistsSync(path.join(process.cwd(), "ondev.key")) ? {
18+
start: () => 0,
19+
stop: () => 0,
20+
update: () => 0
21+
} : load("Starting dev server...");
22+
spinner.start();
23+
spinner.update("Starting project...");
24+
outputDev("Starting project...");
25+
outputDev(`spawn in ${process.cwd()}`);
26+
outputDev("")
27+
// setup port
28+
outputDev(`options.port: ${options.port}`);
29+
outputDev(`port: ${options.port || 3000}`);
30+
outputDev("")
31+
let port = options.port || 3000;
32+
if (options.port) {
33+
// check is number
34+
if (isNaN(options.port) || options.port.length !== 4) {
35+
spinner.stop();
36+
outputDev("Error: port is not a number");
37+
outputDev("Program exit by error");
38+
output("Error: port should be a 4 sig number", "red");
39+
return
40+
} else if (await checkPort(port)) {
41+
spinner.stop();
42+
outputDev("Error: port is busy");
43+
outputDev("Program exit by error");
44+
output("Error: port is busy", "red");
45+
return
46+
}
47+
} else {
48+
const portfinder = require("portfinder");
49+
portfinder.getPort({
50+
port: 3000,
51+
stopPort: 4000
52+
}, (err, active) => {
53+
if (err) {
54+
spinner.stop();
55+
outputDev("Error: port 3000-4000 all busy");
56+
outputDev("Program exit by error");
57+
output("Error: port 3000-4000 all busy", "red");
58+
return
59+
}
60+
port = active;
61+
})
62+
}
63+
// run command `vite` and listen for stdout
64+
const {exec} = require("child_process");
65+
const vite = exec(`vite --port ${port}`, {cwd: process.cwd()}, error => {
66+
if (error) {
67+
spinner.stop();
68+
outputDev(error);
69+
outputDev("Program exit by error");
70+
return;
71+
}
72+
spinner.stop();
73+
outputDev("Program exit by vite");
74+
});
75+
vite.stdout.on("data", data => {
76+
// if include `ready in` then vite is ready
77+
if (data.toString().includes("ready in")) {
78+
spinner.stop();
79+
output(`${pc.bold("[Fastjs-cli]")}`, "green");
80+
output("")
81+
const Version = require("../package.json").version;
82+
output(`*green*${pc.bold("Fastjs-cli")} v${Version}*green* is ready in ${Date.now() - startTime}ms`);
83+
output("")
84+
output(` > ${pc.bold("Local")}: http://localhost:${port}`);
85+
output(` > ${pc.bold("Close")}: Ctrl + C`);
86+
}
87+
})
88+
}
89+
90+
module.exports = dev;

0 commit comments

Comments
 (0)