nuxt-gin-tools 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-gin-tools",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "This project is used as a dependency for [nuxt-gin-starter](https://github.com/RapboyGao/nuxt-gin-starter.git)",
5
5
  "bin": {
6
6
  "nuxt-gin": "index.js"
package/src/dev-go.js CHANGED
@@ -14,12 +14,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.startGoDev = startGoDev;
16
16
  const child_process_1 = require("child_process");
17
- const child_process_2 = require("child_process");
18
17
  const chokidar_1 = __importDefault(require("chokidar"));
19
18
  const chalk_1 = __importDefault(require("chalk"));
20
19
  const fs_extra_1 = require("fs-extra");
21
- const os_1 = __importDefault(require("os"));
22
20
  const path_1 = require("path");
21
+ const utils_1 = require("./utils");
23
22
  const cwd = process.cwd();
24
23
  const RESTART_DEBOUNCE_MS = 150;
25
24
  const SHUTDOWN_TIMEOUT_MS = 2000;
@@ -41,90 +40,11 @@ function getGinPort() {
41
40
  }
42
41
  return null;
43
42
  }
44
- function killPortUnix(port) {
45
- try {
46
- const output = (0, child_process_2.execSync)(`lsof -ti tcp:${port}`, {
47
- stdio: ["ignore", "pipe", "ignore"],
48
- })
49
- .toString()
50
- .trim();
51
- if (!output) {
52
- return;
53
- }
54
- const pids = output
55
- .split("\n")
56
- .map((pid) => Number(pid.trim()))
57
- .filter((pid) => Number.isInteger(pid) && pid > 0);
58
- for (const pid of pids) {
59
- try {
60
- process.kill(pid, "SIGKILL");
61
- console.log(chalk_1.default.yellow(`[${LOG_TAG}] killed process ${pid} on ginPort ${port} (unix)`));
62
- }
63
- catch (_a) {
64
- // best effort
65
- }
66
- }
67
- }
68
- catch (_b) {
69
- // best effort
70
- }
71
- }
72
- function killPortWindows(port) {
73
- try {
74
- const output = (0, child_process_2.execSync)(`netstat -ano -p tcp`, {
75
- stdio: ["ignore", "pipe", "ignore"],
76
- })
77
- .toString()
78
- .trim();
79
- if (!output) {
80
- return;
81
- }
82
- const pids = new Set();
83
- for (const line of output.split("\n")) {
84
- const trimmed = line.trim();
85
- if (!trimmed || !trimmed.startsWith("TCP")) {
86
- continue;
87
- }
88
- const parts = trimmed.split(/\s+/);
89
- if (parts.length < 5) {
90
- continue;
91
- }
92
- const localAddress = parts[1];
93
- const pid = Number(parts[parts.length - 1]);
94
- const match = localAddress.match(/:(\d+)$/);
95
- if (!match) {
96
- continue;
97
- }
98
- const localPort = Number(match[1]);
99
- if (localPort === port && Number.isInteger(pid) && pid > 0) {
100
- pids.add(pid);
101
- }
102
- }
103
- for (const pid of pids) {
104
- try {
105
- (0, child_process_2.execSync)(`taskkill /PID ${pid} /F`, {
106
- stdio: ["ignore", "ignore", "ignore"],
107
- });
108
- console.log(chalk_1.default.yellow(`[${LOG_TAG}] killed process ${pid} on ginPort ${port} (win32)`));
109
- }
110
- catch (_a) {
111
- // best effort
112
- }
113
- }
114
- }
115
- catch (_b) {
116
- // best effort
117
- }
118
- }
119
43
  function killGinPortIfNeeded() {
120
44
  if (!ginPort) {
121
45
  return;
122
46
  }
123
- if (os_1.default.platform() === "win32") {
124
- killPortWindows(ginPort);
125
- return;
126
- }
127
- killPortUnix(ginPort);
47
+ (0, utils_1.killPort)(ginPort, { logPrefix: LOG_TAG, portLabel: "ginPort" });
128
48
  }
129
49
  function normalizePath(filePath) {
130
50
  return filePath.replace(/\\/g, "/").replace(/^\.\//, "");
package/src/develop.js CHANGED
@@ -15,109 +15,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.develop = develop;
16
16
  const concurrently_1 = __importDefault(require("concurrently"));
17
17
  const fs_extra_1 = require("fs-extra");
18
- const os_1 = __importDefault(require("os"));
19
18
  const path_1 = require("path");
20
- const child_process_1 = require("child_process");
21
- const chalk_1 = __importDefault(require("chalk"));
22
19
  const cleanup_1 = __importDefault(require("./cleanup"));
23
20
  const postinstall_1 = __importDefault(require("./postinstall"));
24
21
  const dev_go_1 = require("./dev-go");
22
+ const utils_1 = require("./utils");
25
23
  const cwd = process.cwd();
26
24
  const serverConfig = (0, fs_extra_1.readJSONSync)((0, path_1.join)(cwd, "server.config.json"));
27
- function killPort(port) {
28
- if (!Number.isInteger(port)) {
29
- return;
30
- }
31
- try {
32
- const output = (0, child_process_1.execSync)(`lsof -ti tcp:${port}`, {
33
- stdio: ["ignore", "pipe", "ignore"],
34
- })
35
- .toString()
36
- .trim();
37
- if (!output) {
38
- return;
39
- }
40
- const pids = output
41
- .split("\n")
42
- .map((pid) => Number(pid.trim()))
43
- .filter((pid) => Number.isInteger(pid) && pid > 0);
44
- for (const pid of pids) {
45
- try {
46
- process.kill(pid, "SIGKILL");
47
- console.log(chalk_1.default.yellow(`Killed process ${pid} on port ${port} (unix)`));
48
- }
49
- catch (_a) {
50
- // Best-effort: if the process is already gone, ignore.
51
- }
52
- }
53
- }
54
- catch (_b) {
55
- // Best-effort: lsof might be missing or no process is listening on the port.
56
- }
57
- }
58
- function killPortWindows(port) {
59
- if (!Number.isInteger(port)) {
60
- return;
61
- }
62
- try {
63
- const output = (0, child_process_1.execSync)(`netstat -ano -p tcp`, {
64
- stdio: ["ignore", "pipe", "ignore"],
65
- })
66
- .toString()
67
- .trim();
68
- if (!output) {
69
- return;
70
- }
71
- const pids = new Set();
72
- for (const line of output.split("\n")) {
73
- const trimmed = line.trim();
74
- if (!trimmed || !trimmed.startsWith("TCP")) {
75
- continue;
76
- }
77
- const parts = trimmed.split(/\s+/);
78
- if (parts.length < 5) {
79
- continue;
80
- }
81
- const localAddress = parts[1];
82
- const pid = Number(parts[parts.length - 1]);
83
- const match = localAddress.match(/:(\d+)$/);
84
- if (!match) {
85
- continue;
86
- }
87
- const localPort = Number(match[1]);
88
- if (localPort === port && Number.isInteger(pid) && pid > 0) {
89
- pids.add(pid);
90
- }
91
- }
92
- for (const pid of pids) {
93
- try {
94
- (0, child_process_1.execSync)(`taskkill /PID ${pid} /F`, {
95
- stdio: ["ignore", "ignore", "ignore"],
96
- });
97
- console.log(chalk_1.default.yellow(`Killed process ${pid} on port ${port} (win32)`));
98
- }
99
- catch (_a) {
100
- // Best-effort: if the process is already gone, ignore.
101
- }
102
- }
103
- }
104
- catch (_b) {
105
- // Best-effort: netstat might be missing or no process is listening on the port.
106
- }
107
- }
108
- function killPortsFromConfig() {
109
- const ports = [serverConfig.ginPort, serverConfig.nuxtPort]
110
- .filter((port) => Number.isInteger(port) && port > 0)
111
- .filter((port, index, list) => list.indexOf(port) === index);
112
- for (const port of ports) {
113
- if (os_1.default.platform() === "win32") {
114
- killPortWindows(port);
115
- }
116
- else {
117
- killPort(port);
118
- }
119
- }
120
- }
121
25
  /**
122
26
  * 启动本地开发环境。
123
27
  *
@@ -143,7 +47,7 @@ function develop() {
143
47
  }
144
48
  // 在开发前确保占用端口被释放
145
49
  if (killPortBeforeDevelop) {
146
- killPortsFromConfig();
50
+ (0, utils_1.killPorts)([serverConfig.ginPort, serverConfig.nuxtPort]);
147
51
  }
148
52
  (0, fs_extra_1.ensureDirSync)((0, path_1.join)(cwd, ".build/.server"));
149
53
  // Nuxt 保持在 concurrently 中运行,统一复用 nuxt 标签输出。
package/src/utils.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ type KillPortOptions = {
2
+ logPrefix?: string;
3
+ portLabel?: string;
4
+ };
5
+ export declare function killPort(port: number, options?: KillPortOptions): void;
6
+ export declare function killPorts(ports: Array<number | undefined>, options?: KillPortOptions): void;
7
+ export {};
package/src/utils.js ADDED
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.killPort = killPort;
7
+ exports.killPorts = killPorts;
8
+ const child_process_1 = require("child_process");
9
+ const chalk_1 = __importDefault(require("chalk"));
10
+ const os_1 = __importDefault(require("os"));
11
+ function isValidPort(port) {
12
+ return Number.isInteger(port) && port > 0;
13
+ }
14
+ function buildMessage(pid, port, platform, options) {
15
+ var _a;
16
+ const prefix = (options === null || options === void 0 ? void 0 : options.logPrefix) ? `[${options.logPrefix}] ` : "";
17
+ const label = (_a = options === null || options === void 0 ? void 0 : options.portLabel) !== null && _a !== void 0 ? _a : "port";
18
+ return `${prefix}killed process ${pid} on ${label} ${port} (${platform})`;
19
+ }
20
+ function killPortUnix(port, options) {
21
+ try {
22
+ const output = (0, child_process_1.execSync)(`lsof -ti tcp:${port}`, {
23
+ stdio: ["ignore", "pipe", "ignore"],
24
+ })
25
+ .toString()
26
+ .trim();
27
+ if (!output) {
28
+ return;
29
+ }
30
+ const pids = output
31
+ .split("\n")
32
+ .map((pid) => Number(pid.trim()))
33
+ .filter((pid) => Number.isInteger(pid) && pid > 0);
34
+ for (const pid of pids) {
35
+ try {
36
+ process.kill(pid, "SIGKILL");
37
+ console.log(chalk_1.default.yellow(buildMessage(pid, port, "unix", options)));
38
+ }
39
+ catch (_a) {
40
+ // Best-effort: if the process is already gone, ignore.
41
+ }
42
+ }
43
+ }
44
+ catch (_b) {
45
+ // Best-effort: lsof might be missing or no process is listening on the port.
46
+ }
47
+ }
48
+ function killPortWindows(port, options) {
49
+ try {
50
+ const output = (0, child_process_1.execSync)(`netstat -ano -p tcp`, {
51
+ stdio: ["ignore", "pipe", "ignore"],
52
+ })
53
+ .toString()
54
+ .trim();
55
+ if (!output) {
56
+ return;
57
+ }
58
+ const pids = new Set();
59
+ for (const line of output.split("\n")) {
60
+ const trimmed = line.trim();
61
+ if (!trimmed || !trimmed.startsWith("TCP")) {
62
+ continue;
63
+ }
64
+ const parts = trimmed.split(/\s+/);
65
+ if (parts.length < 5) {
66
+ continue;
67
+ }
68
+ const localAddress = parts[1];
69
+ const pid = Number(parts[parts.length - 1]);
70
+ const match = localAddress.match(/:(\d+)$/);
71
+ if (!match) {
72
+ continue;
73
+ }
74
+ const localPort = Number(match[1]);
75
+ if (localPort === port && Number.isInteger(pid) && pid > 0) {
76
+ pids.add(pid);
77
+ }
78
+ }
79
+ for (const pid of pids) {
80
+ try {
81
+ (0, child_process_1.execSync)(`taskkill /PID ${pid} /F`, {
82
+ stdio: ["ignore", "ignore", "ignore"],
83
+ });
84
+ console.log(chalk_1.default.yellow(buildMessage(pid, port, "win32", options)));
85
+ }
86
+ catch (_a) {
87
+ // Best-effort: if the process is already gone, ignore.
88
+ }
89
+ }
90
+ }
91
+ catch (_b) {
92
+ // Best-effort: netstat might be missing or no process is listening on the port.
93
+ }
94
+ }
95
+ function killPort(port, options) {
96
+ if (!isValidPort(port)) {
97
+ return;
98
+ }
99
+ if (os_1.default.platform() === "win32") {
100
+ killPortWindows(port, options);
101
+ return;
102
+ }
103
+ killPortUnix(port, options);
104
+ }
105
+ function killPorts(ports, options) {
106
+ const validPorts = ports
107
+ .filter((port) => typeof port === "number" && isValidPort(port))
108
+ .filter((port, index, list) => list.indexOf(port) === index);
109
+ for (const port of validPorts) {
110
+ killPort(port, options);
111
+ }
112
+ }