@0x-jerry/x 2.4.3 → 2.5.1

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 (4) hide show
  1. package/dist/x.js +3 -6
  2. package/dist/xr.js +122 -86
  3. package/package.json +21 -20
  4. package/readme.md +19 -19
package/dist/x.js CHANGED
@@ -21,8 +21,7 @@ async function downloadGitRepo(opt) {
21
21
  return;
22
22
  }
23
23
  destDir = await checkDest(destDir) || "";
24
- if (!destDir)
25
- return;
24
+ if (!destDir) return;
26
25
  const gitConf = normalizeGitUrl(url);
27
26
  const zipFilePath = await downloadGitBranch(gitConf, branch);
28
27
  await fs.ensureDir(destDir);
@@ -56,8 +55,7 @@ async function checkDest(dest) {
56
55
  });
57
56
  dest = res.dest;
58
57
  }
59
- if (!dest)
60
- return;
58
+ if (!dest) return;
61
59
  dest = path.isAbsolute(dest) ? dest : path.join(cwd, dest);
62
60
  const files = fs.pathExistsSync(dest) ? await fs.readdir(dest) : null;
63
61
  if (files?.length) {
@@ -67,8 +65,7 @@ async function checkDest(dest) {
67
65
  message: `Detect files in ${pc.cyan(dest)}, override it?`,
68
66
  initial: false
69
67
  });
70
- if (!res.override)
71
- return;
68
+ if (!res.override) return;
72
69
  }
73
70
  return dest;
74
71
  }
package/dist/xr.js CHANGED
@@ -7,101 +7,137 @@ import {
7
7
  import { sliver } from "@0x-jerry/silver";
8
8
 
9
9
  // src/commands/run.ts
10
- import path, { join, resolve } from "path";
10
+ import path4 from "path";
11
11
  import pc from "picocolors";
12
- import jsonc from "jsonc-parser";
13
- import { readFile, readdir } from "fs/promises";
14
- import { existsSync } from "fs";
15
- var { red, cyan } = pc;
16
- async function runScript(command, params = []) {
17
- const [scriptExecuteContent, allScripts] = await getScriptContent(command);
18
- if (scriptExecuteContent) {
19
- await exec(scriptExecuteContent, params, makeEnv());
20
- return;
12
+
13
+ // src/commands/run/node.ts
14
+ import { pathExists } from "fs-extra";
15
+ import path from "path";
16
+ import { readdir, readFile } from "fs/promises";
17
+ var NodeTaskDetecter = class {
18
+ async binaryPaths(cwd) {
19
+ const envPaths = [];
20
+ let dir = cwd;
21
+ do {
22
+ const binPath = path.join(dir, "node_modules", ".bin");
23
+ if (await pathExists(binPath)) {
24
+ envPaths.push(binPath);
25
+ }
26
+ dir = path.resolve(dir, "..");
27
+ } while (dir !== path.resolve(dir, ".."));
28
+ return envPaths;
21
29
  }
22
- console.log(
23
- red("["),
24
- cyan(`${command}`),
25
- red("] not exists in the list: "),
26
- allScripts.map((name) => cyan(name)).join(", ")
27
- );
28
- }
29
- function makeEnv() {
30
- const cwd = process.cwd();
31
- const env = process.env;
32
- const envPaths = process.env.PATH?.split(path.delimiter) || [];
33
- let dir = cwd;
34
- do {
35
- const binPath = join(dir, "node_modules", ".bin");
36
- if (existsSync(binPath)) {
37
- envPaths.push(binPath);
38
- }
39
- dir = resolve(dir, "..");
40
- } while (dir !== resolve(dir, ".."));
41
- env.PATH = envPaths.join(path.delimiter);
42
- return env;
43
- }
44
- async function getPackageScripts() {
45
- const scripts = /* @__PURE__ */ new Map();
46
- const cwd = process.cwd();
47
- const pkgPath = join(cwd, "package.json");
48
- try {
49
- const text = await readFile(pkgPath, { encoding: "utf-8" });
30
+ check(cwd) {
31
+ return pathExists(path.join(cwd, "package.json"));
32
+ }
33
+ async task(cwd, taskName) {
34
+ return (await this.tasks(cwd))[taskName];
35
+ }
36
+ async tasks(cwd) {
37
+ const pkgPath = path.join(cwd, "package.json");
38
+ const text = await readFile(pkgPath, "utf8");
50
39
  const json = JSON.parse(text);
51
- Object.entries(json.scripts).forEach(([name, content]) => {
52
- scripts.set(name, content);
53
- });
54
- } catch (_error) {
40
+ const tasks = json.scripts || {};
41
+ {
42
+ let dir = cwd;
43
+ do {
44
+ const binPath = path.join(dir, "node_modules", ".bin");
45
+ if (await pathExists(binPath)) {
46
+ const files = await readdir(binPath);
47
+ for (const filename of files) {
48
+ tasks[filename] ??= filename;
49
+ }
50
+ }
51
+ dir = path.resolve(dir, "..");
52
+ } while (dir !== path.resolve(dir, ".."));
53
+ }
54
+ return tasks;
55
55
  }
56
- return scripts;
57
- }
58
- async function getBinScripts() {
59
- const binCommands = /* @__PURE__ */ new Map();
60
- let dir = process.cwd();
61
- do {
62
- const binPath = join(dir, "node_modules", ".bin");
63
- try {
64
- for (const file of await readdir(binPath)) {
65
- binCommands.set(file, file);
66
- }
67
- } catch (_error) {
56
+ };
57
+
58
+ // src/commands/run/deno.ts
59
+ import { parse } from "jsonc-parser";
60
+ import { pathExists as pathExists2 } from "fs-extra";
61
+ import path2 from "path";
62
+ import { readFile as readFile2 } from "fs/promises";
63
+ var DenoTaskDetecter = class {
64
+ check(cwd) {
65
+ return pathExists2(path2.join(cwd, "deno.json")) || pathExists2(path2.join(cwd, "deno.jsonc"));
66
+ }
67
+ async task(cwd, taskName) {
68
+ return (await this.tasks(cwd))[taskName];
69
+ }
70
+ async tasks(cwd) {
71
+ let tasks = {};
72
+ let denoConfigPath = path2.join(cwd, "deno.json");
73
+ if (!await pathExists2(denoConfigPath)) {
74
+ denoConfigPath = path2.join(cwd, "deno.jsonc");
68
75
  }
69
- dir = resolve(dir, "..");
70
- } while (dir !== resolve(dir, ".."));
71
- return binCommands;
72
- }
73
- async function getDenoTasks() {
74
- const tasks = /* @__PURE__ */ new Map();
75
- const cwd = process.cwd();
76
- try {
77
- let txt = "";
78
- try {
79
- const denoConf = join(cwd, "deno.json");
80
- txt = await readFile(denoConf, { encoding: "utf-8" });
81
- } catch (_error) {
82
- const denoConf = join(cwd, "deno.jsonc");
83
- txt = await readFile(denoConf, { encoding: "utf-8" });
76
+ if (await pathExists2(denoConfigPath)) {
77
+ const text = await readFile2(denoConfigPath, "utf8");
78
+ const json = parse(text);
79
+ tasks = json.tasks || {};
84
80
  }
85
- if (!txt) {
86
- return tasks;
81
+ return tasks;
82
+ }
83
+ };
84
+
85
+ // src/commands/run/rust.ts
86
+ import { pathExists as pathExists3 } from "fs-extra";
87
+ import path3 from "path";
88
+ var RustTaskDetecter = class {
89
+ check(cwd) {
90
+ return pathExists3(path3.join(cwd, "Cargo.toml"));
91
+ }
92
+ async task(_cwd, _taskName) {
93
+ return "cargo run";
94
+ }
95
+ };
96
+
97
+ // src/commands/run.ts
98
+ async function runScript(command, params = []) {
99
+ const taskDetectors = [
100
+ new NodeTaskDetecter(),
101
+ new DenoTaskDetecter(),
102
+ new RustTaskDetecter()
103
+ ];
104
+ const cwd = process.cwd();
105
+ for (const taskDetector of taskDetectors) {
106
+ if (await taskDetector.check(cwd)) {
107
+ const task = await taskDetector.task(cwd, command);
108
+ if (task) {
109
+ const env = makeEnv(await taskDetector.binaryPaths?.(cwd) || []);
110
+ await exec(task, params, env);
111
+ return;
112
+ }
87
113
  }
88
- const json = jsonc.parse(txt);
89
- Object.entries(json.tasks).forEach(([name, content]) => {
90
- tasks.set(name, content);
91
- });
92
- } catch (_error) {
93
114
  }
94
- return tasks;
115
+ const allScripts = await getAvailableCommands();
116
+ console.log(
117
+ pc.red("["),
118
+ pc.cyan(`${command}`),
119
+ pc.red("] not exists in the list: "),
120
+ allScripts.map((name) => pc.cyan(name)).join(", ")
121
+ );
95
122
  }
96
- async function getScriptContent(cmd = "") {
97
- const tasks = await getDenoTasks();
98
- const scripts = await getPackageScripts();
99
- const binCommands = await getBinScripts();
100
- const executeContent = tasks.get(cmd) || scripts.get(cmd) || binCommands.get(cmd) || false;
101
- const uniq = [
102
- .../* @__PURE__ */ new Set([...tasks.keys(), ...scripts.keys(), ...binCommands.keys()])
123
+ function makeEnv(extraPaths) {
124
+ const env = process.env;
125
+ const envPaths = process.env.PATH?.split(path4.delimiter) || [];
126
+ env.PATH = [...envPaths, ...extraPaths].join(path4.delimiter);
127
+ return env;
128
+ }
129
+ async function getAvailableCommands() {
130
+ const taskDetectors = [
131
+ new NodeTaskDetecter(),
132
+ new DenoTaskDetecter(),
133
+ new RustTaskDetecter()
103
134
  ];
104
- return [executeContent, uniq];
135
+ const cwd = process.cwd();
136
+ const p = taskDetectors.map(
137
+ async (t) => await t.check(cwd) ? Object.keys(await t.tasks?.(cwd) || {}) : []
138
+ );
139
+ const tasks = await Promise.all(p);
140
+ return tasks.flat();
105
141
  }
106
142
 
107
143
  // src/xr.ts
@@ -111,7 +147,7 @@ var ins = sliver`
111
147
  xr [@command:command] #stopEarly, run command quickly. ${defaultAction}
112
148
  `;
113
149
  ins.type("command", async () => {
114
- const [_, allScripts] = await getScriptContent();
150
+ const allScripts = await getAvailableCommands();
115
151
  return allScripts;
116
152
  });
117
153
  async function defaultAction(_, arg) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0x-jerry/x",
3
- "version": "2.4.3",
3
+ "version": "2.5.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/0x-jerry/x.git"
@@ -20,6 +20,16 @@
20
20
  "xn": "dist/xn.js",
21
21
  "x": "dist/x.js"
22
22
  },
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "dev:xr": "tsx src/xr.ts",
26
+ "dev:xn": "tsx src/xn.ts",
27
+ "dev:x": "tsx src/x.ts",
28
+ "test": "vitest run",
29
+ "test:watch": "vitest",
30
+ "prepublishOnly": "npm run build",
31
+ "release": "x-release"
32
+ },
23
33
  "tsup": {
24
34
  "entry": [
25
35
  "src/xr.ts",
@@ -33,13 +43,13 @@
33
43
  "clean": true
34
44
  },
35
45
  "dependencies": {
36
- "@0x-jerry/silver": "^0.1.7",
37
- "@0x-jerry/utils": "^2.2.7",
46
+ "@0x-jerry/silver": "^0.1.8",
47
+ "@0x-jerry/utils": "^2.4.2",
38
48
  "decompress": "^4.2.1",
39
49
  "fs-extra": "^11.2.0",
40
50
  "global-agent": "^3.0.0",
41
- "got": "^14.2.1",
42
- "jsonc-parser": "^3.2.1",
51
+ "got": "^14.4.1",
52
+ "jsonc-parser": "^3.3.1",
43
53
  "ora": "^8.0.1",
44
54
  "picocolors": "^1.0.1",
45
55
  "prompts": "^2.4.2"
@@ -49,21 +59,12 @@
49
59
  "@types/decompress": "^4.2.7",
50
60
  "@types/fs-extra": "^11.0.4",
51
61
  "@types/global-agent": "^2.1.3",
52
- "@types/node": "^20.12.12",
62
+ "@types/node": "^20.14.11",
53
63
  "@types/prompts": "^2.4.9",
54
- "@vitest/coverage-v8": "^1.6.0",
55
- "tsup": "^8.0.2",
56
- "tsx": "^4.10.4",
57
- "typescript": "^5.4.5",
58
- "vitest": "^1.6.0"
59
- },
60
- "scripts": {
61
- "build": "tsup",
62
- "dev:xr": "tsx src/xr.ts",
63
- "dev:xn": "tsx src/xn.ts",
64
- "dev:x": "tsx src/x.ts",
65
- "test": "vitest",
66
- "ci:test": "vitest run",
67
- "release": "x-release"
64
+ "@vitest/coverage-v8": "^2.0.3",
65
+ "tsup": "^8.1.2",
66
+ "tsx": "^4.16.2",
67
+ "typescript": "^5.5.3",
68
+ "vitest": "^2.0.3"
68
69
  }
69
70
  }
package/readme.md CHANGED
@@ -1,19 +1,19 @@
1
- # X
2
-
3
- Some useful command for myself.
4
-
5
- ## Install
6
-
7
- ```sh
8
- npm i -g @0x-jerry/x
9
- ```
10
-
11
- ## Command Completions
12
-
13
- Add this code to `~/.zshrc`
14
-
15
- ```zsh
16
- source <(x completions)
17
- source <(xr completions)
18
- source <(xn completions)
19
- ```
1
+ # X
2
+
3
+ Some useful command for myself.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm i -g @0x-jerry/x
9
+ ```
10
+
11
+ ## Command Completions
12
+
13
+ Add this code to `~/.zshrc`
14
+
15
+ ```zsh
16
+ source <(x completions)
17
+ source <(xr completions)
18
+ source <(xn completions)
19
+ ```