-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
82 lines (74 loc) · 2.16 KB
/
index.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
const semver = require("semver");
const pkg = require("./package.json");
const requiredVersion = pkg.engines.node;
checkNodeVersion(requiredVersion, pkg.name);
function checkNodeVersion(wanted, id) {
if (!semver.satisfies(process.version, wanted)) {
console.log(
"\x1B[31m%s\x1B[0m",
"You are using Node " +
process.version +
", but this version of " +
id +
" requires Node " +
wanted +
".\nPlease upgrade your Node version."
);
process.exit(1);
}
}
const chalk = require("chalk");
const internalIp = require("internal-ip");
const program = require("commander");
const handler = require("serve-handler");
const http = require("http");
const path = require("path");
const open = require("open");
const updateNotifier = require("update-notifier");
// 检查更新
function updateCheck() {
const notifier = updateNotifier({ pkg });
const message = [];
if (notifier.update) {
message.push(
"Update available: " +
chalk.green.bold(notifier.update.latest) +
chalk.gray(" (current: " + notifier.update.current + ");")
);
message.push(
"Run " +
chalk.magenta("npm install -g " + pkg.name) +
" Or " +
chalk.magenta("yarn global add " + pkg.name) +
" to update."
);
// eslint-disable-next-line no-console
console.log(message.join("\r\n"));
}
}
updateCheck();
const server = http.createServer((request, response) => {
return handler(request, response, {
public: path.resolve(
__dirname,
"./vue-ele-form-generator"
)
});
});
program.version(`fgen ${pkg.version}`).usage("<command> [options]");
program.option("-p, --port <port>", "Port used by the server", 54321);
program.parse(process.argv);
const port = program.port;
server.listen(port, async () => {
const localUrl = "http://localhost:" + port;
const networkUrl = await internalIp.v4();
const networkMsg = networkUrl
? chalk.cyan(`http://${networkUrl}:${port}`)
: chalk.gray("unavailable");
console.log();
console.log(` App running at:`);
console.log(` - Local: ${chalk.cyan(localUrl)}`);
console.log(` - Network: ${networkMsg}`);
console.log();
open(localUrl);
});