@t8/serve 0.1.1 → 0.1.3

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/dist/index.js CHANGED
@@ -38,7 +38,7 @@ async function isValidFilePath(filePath, dirPath) {
38
38
  if (!filePath.startsWith(dirPath)) return false;
39
39
  try {
40
40
  await (0, import_promises.access)(filePath);
41
- return true;
41
+ return !(await (0, import_promises.lstat)(filePath)).isDirectory();
42
42
  } catch {
43
43
  return false;
44
44
  }
@@ -46,15 +46,13 @@ async function isValidFilePath(filePath, dirPath) {
46
46
 
47
47
  // src/getFilePath.ts
48
48
  var cwd = process.cwd();
49
- async function getFilePath(urlPath = "", { path = "", dirs = [""] }) {
50
- for (let dir of dirs) {
49
+ async function getFilePath(urlPath = "", { path = "", dirs = [] }) {
50
+ for (let dir of dirs.length === 0 ? [""] : dirs) {
51
51
  let dirPath = (0, import_node_path.join)(cwd, path, dir);
52
52
  let filePath = (0, import_node_path.join)(dirPath, urlPath);
53
53
  if (await isValidFilePath(filePath, dirPath)) return filePath;
54
- if (!/\.\w+$/.test(filePath)) {
55
- filePath = (0, import_node_path.join)(dirPath, urlPath, "index.html");
56
- if (await isValidFilePath(filePath, dirPath)) return filePath;
57
- }
54
+ filePath = (0, import_node_path.join)(dirPath, urlPath, "index.html");
55
+ if (await isValidFilePath(filePath, dirPath)) return filePath;
58
56
  }
59
57
  }
60
58
 
package/dist/run.cjs CHANGED
@@ -1,6 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
 
4
+ // src/run.ts
5
+ var import_node_child_process = require("node:child_process");
6
+ var import_promises2 = require("node:fs/promises");
7
+ var import_node_path3 = require("node:path");
8
+ var import_node_util = require("node:util");
9
+
4
10
  // src/serve.ts
5
11
  var import_node_fs = require("node:fs");
6
12
  var import_node_http = require("node:http");
@@ -15,7 +21,7 @@ async function isValidFilePath(filePath, dirPath) {
15
21
  if (!filePath.startsWith(dirPath)) return false;
16
22
  try {
17
23
  await (0, import_promises.access)(filePath);
18
- return true;
24
+ return !(await (0, import_promises.lstat)(filePath)).isDirectory();
19
25
  } catch {
20
26
  return false;
21
27
  }
@@ -23,15 +29,13 @@ async function isValidFilePath(filePath, dirPath) {
23
29
 
24
30
  // src/getFilePath.ts
25
31
  var cwd = process.cwd();
26
- async function getFilePath(urlPath = "", { path = "", dirs = [""] }) {
27
- for (let dir of dirs) {
32
+ async function getFilePath(urlPath = "", { path = "", dirs = [] }) {
33
+ for (let dir of dirs.length === 0 ? [""] : dirs) {
28
34
  let dirPath = (0, import_node_path.join)(cwd, path, dir);
29
35
  let filePath = (0, import_node_path.join)(dirPath, urlPath);
30
36
  if (await isValidFilePath(filePath, dirPath)) return filePath;
31
- if (!/\.\w+$/.test(filePath)) {
32
- filePath = (0, import_node_path.join)(dirPath, urlPath, "index.html");
33
- if (await isValidFilePath(filePath, dirPath)) return filePath;
34
- }
37
+ filePath = (0, import_node_path.join)(dirPath, urlPath, "index.html");
38
+ if (await isValidFilePath(filePath, dirPath)) return filePath;
35
39
  }
36
40
  }
37
41
 
@@ -79,9 +83,22 @@ function serve(config = {}) {
79
83
  }
80
84
 
81
85
  // src/run.ts
82
- var args = process.argv.slice(2);
83
- serve({
84
- url: args[0],
85
- path: args[1],
86
- dirs: args.slice(2)
87
- });
86
+ var exec = (0, import_node_util.promisify)(import_node_child_process.exec);
87
+ async function run() {
88
+ let [url, path = "", ...args] = process.argv.slice(2);
89
+ let buildFlagIndex = args.indexOf("-b");
90
+ if (buildFlagIndex !== -1) {
91
+ let inputFile = (0, import_node_path3.join)(path, args[buildFlagIndex + 1] ?? "index.ts");
92
+ let outputFile = (0, import_node_path3.join)(path, "dist", args[buildFlagIndex + 2] ?? "index.js");
93
+ await (0, import_promises2.rm)((0, import_node_path3.join)(path, "dist"), { recursive: true, force: true });
94
+ await exec(
95
+ `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
96
+ );
97
+ }
98
+ serve({
99
+ url,
100
+ path,
101
+ dirs: buildFlagIndex === -1 ? args : args.slice(0, buildFlagIndex)
102
+ });
103
+ }
104
+ run();
package/dist/run.mjs CHANGED
@@ -1,5 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ // src/run.ts
4
+ import { exec as originalExec } from "node:child_process";
5
+ import { rm } from "node:fs/promises";
6
+ import { join as join2 } from "node:path";
7
+ import { promisify } from "node:util";
8
+
3
9
  // src/serve.ts
4
10
  import { createReadStream } from "node:fs";
5
11
  import { createServer } from "node:http";
@@ -9,12 +15,12 @@ import { extname } from "node:path";
9
15
  import { join } from "node:path";
10
16
 
11
17
  // src/isValidFilePath.ts
12
- import { access } from "node:fs/promises";
18
+ import { access, lstat } from "node:fs/promises";
13
19
  async function isValidFilePath(filePath, dirPath) {
14
20
  if (!filePath.startsWith(dirPath)) return false;
15
21
  try {
16
22
  await access(filePath);
17
- return true;
23
+ return !(await lstat(filePath)).isDirectory();
18
24
  } catch {
19
25
  return false;
20
26
  }
@@ -22,15 +28,13 @@ async function isValidFilePath(filePath, dirPath) {
22
28
 
23
29
  // src/getFilePath.ts
24
30
  var cwd = process.cwd();
25
- async function getFilePath(urlPath = "", { path = "", dirs = [""] }) {
26
- for (let dir of dirs) {
31
+ async function getFilePath(urlPath = "", { path = "", dirs = [] }) {
32
+ for (let dir of dirs.length === 0 ? [""] : dirs) {
27
33
  let dirPath = join(cwd, path, dir);
28
34
  let filePath = join(dirPath, urlPath);
29
35
  if (await isValidFilePath(filePath, dirPath)) return filePath;
30
- if (!/\.\w+$/.test(filePath)) {
31
- filePath = join(dirPath, urlPath, "index.html");
32
- if (await isValidFilePath(filePath, dirPath)) return filePath;
33
- }
36
+ filePath = join(dirPath, urlPath, "index.html");
37
+ if (await isValidFilePath(filePath, dirPath)) return filePath;
34
38
  }
35
39
  }
36
40
 
@@ -78,9 +82,22 @@ function serve(config = {}) {
78
82
  }
79
83
 
80
84
  // src/run.ts
81
- var args = process.argv.slice(2);
82
- serve({
83
- url: args[0],
84
- path: args[1],
85
- dirs: args.slice(2)
86
- });
85
+ var exec = promisify(originalExec);
86
+ async function run() {
87
+ let [url, path = "", ...args] = process.argv.slice(2);
88
+ let buildFlagIndex = args.indexOf("-b");
89
+ if (buildFlagIndex !== -1) {
90
+ let inputFile = join2(path, args[buildFlagIndex + 1] ?? "index.ts");
91
+ let outputFile = join2(path, "dist", args[buildFlagIndex + 2] ?? "index.js");
92
+ await rm(join2(path, "dist"), { recursive: true, force: true });
93
+ await exec(
94
+ `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`
95
+ );
96
+ }
97
+ serve({
98
+ url,
99
+ path,
100
+ dirs: buildFlagIndex === -1 ? args : args.slice(0, buildFlagIndex)
101
+ });
102
+ }
103
+ run();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/serve",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "node",
@@ -6,18 +6,16 @@ const cwd = process.cwd();
6
6
 
7
7
  export async function getFilePath(
8
8
  urlPath: string = "",
9
- { path = "", dirs = [""] }: Config,
9
+ { path = "", dirs = [] }: Config,
10
10
  ) {
11
- for (let dir of dirs) {
11
+ for (let dir of dirs.length === 0 ? [""] : dirs) {
12
12
  let dirPath = join(cwd, path, dir);
13
13
  let filePath = join(dirPath, urlPath);
14
14
 
15
15
  if (await isValidFilePath(filePath, dirPath)) return filePath;
16
16
 
17
- if (!/\.\w+$/.test(filePath)) {
18
- filePath = join(dirPath, urlPath, "index.html");
17
+ filePath = join(dirPath, urlPath, "index.html");
19
18
 
20
- if (await isValidFilePath(filePath, dirPath)) return filePath;
21
- }
19
+ if (await isValidFilePath(filePath, dirPath)) return filePath;
22
20
  }
23
21
  }
@@ -1,11 +1,11 @@
1
- import { access } from "node:fs/promises";
1
+ import { access, lstat } from "node:fs/promises";
2
2
 
3
3
  export async function isValidFilePath(filePath: string, dirPath: string) {
4
4
  if (!filePath.startsWith(dirPath)) return false;
5
5
 
6
6
  try {
7
7
  await access(filePath);
8
- return true;
8
+ return !(await lstat(filePath)).isDirectory();
9
9
  } catch {
10
10
  return false;
11
11
  }
package/src/run.ts CHANGED
@@ -1,15 +1,38 @@
1
1
  #!/usr/bin/env node
2
+ import { exec as originalExec } from "node:child_process";
3
+ import { rm } from "node:fs/promises";
4
+ import { join } from "node:path";
5
+ import { promisify } from "node:util";
2
6
  import { serve } from "./serve";
3
7
 
4
- let args = process.argv.slice(2);
8
+ const exec = promisify(originalExec);
5
9
 
6
10
  /**
7
11
  * @example
8
12
  * serve 3000 app public dist
9
13
  * serve 127.0.0.1:3000 app public dist
14
+ * serve 3000 app public dist -b
15
+ * serve 3000 app public dist -b src/index.ts
10
16
  */
11
- serve({
12
- url: args[0],
13
- path: args[1],
14
- dirs: args.slice(2),
15
- });
17
+ async function run() {
18
+ let [url, path = "", ...args] = process.argv.slice(2);
19
+ let buildFlagIndex = args.indexOf("-b");
20
+
21
+ if (buildFlagIndex !== -1) {
22
+ let inputFile = join(path, args[buildFlagIndex + 1] ?? "index.ts");
23
+ let outputFile = join(path, "dist", args[buildFlagIndex + 2] ?? "index.js");
24
+
25
+ await rm(join(path, "dist"), { recursive: true, force: true });
26
+ await exec(
27
+ `npx esbuild ${inputFile} --outfile=${outputFile} --bundle --platform=neutral --log-level=warning`,
28
+ );
29
+ }
30
+
31
+ serve({
32
+ url,
33
+ path,
34
+ dirs: buildFlagIndex === -1 ? args : args.slice(0, buildFlagIndex),
35
+ });
36
+ }
37
+
38
+ run();