@3-/wait_for_port 0.1.1 → 0.1.2

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.
Files changed (3) hide show
  1. package/bin.js +11 -29
  2. package/lib.js +16 -0
  3. package/package.json +1 -1
package/bin.js CHANGED
@@ -1,10 +1,9 @@
1
- #! /usr/bin/env bun
2
-
3
- import { Socket } from "node:net";
1
+ #!/usr/bin/env bun
4
2
  import yargs from "yargs/yargs";
5
3
  import { hideBin } from "yargs/helpers";
6
4
  import { $ } from "zx";
7
5
  import sleep from "@3-/sleep";
6
+ import checkPort from "./lib.js";
8
7
 
9
8
  const argv = yargs(hideBin(process.argv))
10
9
  .usage("用法: $0 <端口或网址> [命令]")
@@ -12,31 +11,14 @@ const argv = yargs(hideBin(process.argv))
12
11
  .parse(),
13
12
  [url, cmd] = argv._,
14
13
  li = String(url).split(":"),
15
- [host, port] = li.length > 1 ? [li[0], +li[1]] : ["127.0.0.1", +li[0]],
16
- checkPort = (h, p) =>
17
- new Promise((resolve) => {
18
- const sock = new Socket(),
19
- finish = (status) => {
20
- sock.destroy();
21
- resolve(status);
22
- };
23
-
24
- sock.setTimeout(1000);
25
- sock.on("connect", () => finish(true));
26
- sock.on("timeout", () => finish(false));
27
- sock.on("error", () => finish(false));
28
- sock.connect(p, h);
29
- }),
30
- waitPort = async () => {
31
- if (!(await checkPort(host, port))) {
32
- if (cmd) {
33
- await $`sh -c ${cmd}`;
34
- }
35
- while (!(await checkPort(host, port))) {
36
- await sleep(1000);
37
- }
38
- }
39
- };
14
+ [host, port] = li.length > 1 ? [li[0], +li[1]] : ["127.0.0.1", +li[0]];
40
15
 
41
- await waitPort();
16
+ if (!(await checkPort(host, port))) {
17
+ if (cmd) {
18
+ await $(["sh -c " + cmd]);
19
+ }
20
+ while (!(await checkPort(host, port))) {
21
+ await sleep(1000);
22
+ }
23
+ }
42
24
  process.exit();
package/lib.js ADDED
@@ -0,0 +1,16 @@
1
+ import { Socket } from "node:net";
2
+
3
+ export default (host, port) =>
4
+ new Promise((resolve) => {
5
+ const sock = new Socket(),
6
+ finish = (status) => {
7
+ sock.destroy();
8
+ resolve(status);
9
+ };
10
+
11
+ sock.setTimeout(1000);
12
+ sock.on("connect", () => finish(true));
13
+ sock.on("timeout", () => finish(false));
14
+ sock.on("error", () => finish(false));
15
+ sock.connect(port, host);
16
+ });
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@3-/wait_for_port","version":"0.1.1","keywords":[],"description":"","repository":{"type":"git","url":"git+https://atomgit.com/i18n/lib.git"},"homepage":"https://atomgit.com/i18n/lib/tree/dev/wait_for_port","author":"i18n.site@gmail.com","license":"MulanPSL-2.0","exports":{"./*":"./*"},"bin":{"wait_for_port":"./bin.js"},"files":["./*"],"devDependencies":{"vitest":"^4.1.2"},"scripts":{"test":"vitest run tests/*.test.js"},"type":"module","dependencies":{"@3-/sleep":"^0.0.4","yargs":"^18.0.0","zx":"^8.8.5"}}
1
+ {"name":"@3-/wait_for_port","version":"0.1.2","keywords":[],"description":"","repository":{"type":"git","url":"git+https://github.com/i18n-site/lib.git"},"homepage":"https://github.com/i18n-site/lib/tree/dev/wait_for_port","author":"i18n.site@gmail.com","license":"MulanPSL-2.0","exports":{".":"./lib.js","./*":"./*"},"bin":{"wait_for_port":"./bin.js"},"files":["./*"],"devDependencies":{"vitest":"^4.1.7"},"scripts":{"test":"vitest run tests/*.test.js"},"type":"module","dependencies":{"@3-/sleep":"^0.0.4","yargs":"^18.0.0","zx":"^8.8.5"}}