@0x-jerry/x 2.0.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.
- package/dist/chunk-D3ZFYQVJ.js +25 -0
- package/dist/xn.d.ts +2 -0
- package/dist/xn.js +95 -0
- package/dist/xr.d.ts +2 -0
- package/dist/xr.js +121 -0
- package/package.json +52 -0
- package/readme.md +21 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
import { execa } from "execa";
|
|
3
|
+
import { stat } from "fs/promises";
|
|
4
|
+
import pc from "picocolors";
|
|
5
|
+
async function run(cmd, env) {
|
|
6
|
+
console.log(pc.dim("$"), pc.dim(cmd));
|
|
7
|
+
await execa("sh", ["-c", cmd], { stdio: "inherit", env });
|
|
8
|
+
}
|
|
9
|
+
async function exec(script, params, env) {
|
|
10
|
+
const cmd = [script, ...params].join(" ");
|
|
11
|
+
await run(cmd, env);
|
|
12
|
+
}
|
|
13
|
+
async function exists(path) {
|
|
14
|
+
try {
|
|
15
|
+
await stat(path);
|
|
16
|
+
return true;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
exec,
|
|
24
|
+
exists
|
|
25
|
+
};
|
package/dist/xn.d.ts
ADDED
package/dist/xn.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {
|
|
2
|
+
exec,
|
|
3
|
+
exists
|
|
4
|
+
} from "./chunk-D3ZFYQVJ.js";
|
|
5
|
+
|
|
6
|
+
// src/xn.ts
|
|
7
|
+
import { sliver } from "@0x-jerry/silver";
|
|
8
|
+
|
|
9
|
+
// src/commands/npm.ts
|
|
10
|
+
import { join } from "path";
|
|
11
|
+
import { yellow } from "picocolors";
|
|
12
|
+
import { readFile } from "fs/promises";
|
|
13
|
+
var npmCommandMapper = {
|
|
14
|
+
install: {
|
|
15
|
+
npm: "i",
|
|
16
|
+
yarn: "install",
|
|
17
|
+
pnpm: "i"
|
|
18
|
+
},
|
|
19
|
+
add: {
|
|
20
|
+
npm: "i",
|
|
21
|
+
yarn: "add",
|
|
22
|
+
pnpm: "i"
|
|
23
|
+
},
|
|
24
|
+
upgrade: {
|
|
25
|
+
npm: "up",
|
|
26
|
+
yarn: "upgrade",
|
|
27
|
+
pnpm: "up"
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
async function runNpm(action, ...params) {
|
|
31
|
+
if (!await getPkgJson()) {
|
|
32
|
+
console.log(
|
|
33
|
+
yellow(
|
|
34
|
+
`Can't find package.json! Please ensure that current path is a node project.`
|
|
35
|
+
)
|
|
36
|
+
);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const npmCommand = await detectNpmCommand();
|
|
40
|
+
const actionName = npmCommandMapper[action][npmCommand];
|
|
41
|
+
await exec(npmCommand, [actionName, ...params]);
|
|
42
|
+
}
|
|
43
|
+
async function detectNpmCommand(cwd = process.cwd()) {
|
|
44
|
+
const pnpmLockFile = join(cwd, "pnpm-lock.yaml");
|
|
45
|
+
if (await exists(pnpmLockFile)) {
|
|
46
|
+
return "pnpm";
|
|
47
|
+
}
|
|
48
|
+
const yarnLockFile = join(cwd, "yarn.lock");
|
|
49
|
+
if (await exists(yarnLockFile)) {
|
|
50
|
+
return "yarn";
|
|
51
|
+
}
|
|
52
|
+
const jsonLockFile = join(cwd, "package-lock.json");
|
|
53
|
+
if (await exists(jsonLockFile)) {
|
|
54
|
+
return "npm";
|
|
55
|
+
}
|
|
56
|
+
return "pnpm";
|
|
57
|
+
}
|
|
58
|
+
async function getPkgJson(cwd = process.cwd()) {
|
|
59
|
+
const jsonFile = join(cwd, "package.json");
|
|
60
|
+
try {
|
|
61
|
+
const txt = await readFile(jsonFile, { encoding: "utf-8" });
|
|
62
|
+
return JSON.parse(txt);
|
|
63
|
+
} catch (_error) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/xn.ts
|
|
69
|
+
sliver`
|
|
70
|
+
@help @autocompletion
|
|
71
|
+
|
|
72
|
+
xn, install npm package quickly. ${defaultAction}
|
|
73
|
+
|
|
74
|
+
i/install [module] #stopEarly, install npm package quickly. ${installAction}
|
|
75
|
+
|
|
76
|
+
up/upgrade #stopEarly, upgrade npm packages. ${upgradeAction}
|
|
77
|
+
`;
|
|
78
|
+
async function defaultAction() {
|
|
79
|
+
await runNpm("install");
|
|
80
|
+
}
|
|
81
|
+
async function installAction(_, opt) {
|
|
82
|
+
const installOnly = !opt._.length;
|
|
83
|
+
if (installOnly) {
|
|
84
|
+
await runNpm("install");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
await runNpm("add", ...opt._);
|
|
88
|
+
}
|
|
89
|
+
async function upgradeAction(_, opt) {
|
|
90
|
+
const params = opt._;
|
|
91
|
+
if (opt.L) {
|
|
92
|
+
params.push("-L");
|
|
93
|
+
}
|
|
94
|
+
await runNpm("upgrade", ...params);
|
|
95
|
+
}
|
package/dist/xr.d.ts
ADDED
package/dist/xr.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
exec
|
|
3
|
+
} from "./chunk-D3ZFYQVJ.js";
|
|
4
|
+
|
|
5
|
+
// src/xr.ts
|
|
6
|
+
import { sliver } from "@0x-jerry/silver";
|
|
7
|
+
|
|
8
|
+
// src/commands/run.ts
|
|
9
|
+
import { join, resolve } from "path";
|
|
10
|
+
import { red, cyan } from "picocolors";
|
|
11
|
+
import jsonc from "jsonc-parser";
|
|
12
|
+
import { readFile, readdir } from "fs/promises";
|
|
13
|
+
import { statSync } from "fs";
|
|
14
|
+
async function runScript(command, params = []) {
|
|
15
|
+
const [scriptExecuteContent, allScripts] = await getScriptContent(command);
|
|
16
|
+
if (scriptExecuteContent) {
|
|
17
|
+
await exec(scriptExecuteContent, params, makeEnv());
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
console.log(
|
|
21
|
+
red("["),
|
|
22
|
+
cyan(`${command}`),
|
|
23
|
+
red("] not exists in the list: "),
|
|
24
|
+
allScripts.map((name) => cyan(name)).join(", ")
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
function makeEnv() {
|
|
28
|
+
const cwd = process.cwd();
|
|
29
|
+
const env = process.env;
|
|
30
|
+
const envPaths = [];
|
|
31
|
+
let dir = cwd;
|
|
32
|
+
do {
|
|
33
|
+
const PATH2 = join(dir, "node_modules", ".bin");
|
|
34
|
+
try {
|
|
35
|
+
statSync(PATH2);
|
|
36
|
+
envPaths.push(PATH2);
|
|
37
|
+
} catch (_error) {
|
|
38
|
+
}
|
|
39
|
+
dir = resolve(dir, "..");
|
|
40
|
+
} while (dir !== resolve(dir, ".."));
|
|
41
|
+
const PATH = envPaths.join(":");
|
|
42
|
+
env.PATH = [process.env.PATH || "", PATH].filter(Boolean).join(":");
|
|
43
|
+
return env;
|
|
44
|
+
}
|
|
45
|
+
async function getPackageScripts() {
|
|
46
|
+
const scripts = /* @__PURE__ */ new Map();
|
|
47
|
+
const cwd = process.cwd();
|
|
48
|
+
const pkgPath = join(cwd, "package.json");
|
|
49
|
+
try {
|
|
50
|
+
const text = await readFile(pkgPath, { encoding: "utf-8" });
|
|
51
|
+
const json = JSON.parse(text);
|
|
52
|
+
Object.entries(json.scripts).forEach(([name, content]) => {
|
|
53
|
+
scripts.set(name, content);
|
|
54
|
+
});
|
|
55
|
+
} catch (_error) {
|
|
56
|
+
}
|
|
57
|
+
return scripts;
|
|
58
|
+
}
|
|
59
|
+
async function getBinScripts() {
|
|
60
|
+
const binCommands = /* @__PURE__ */ new Map();
|
|
61
|
+
let dir = process.cwd();
|
|
62
|
+
do {
|
|
63
|
+
const binPath = join(dir, "node_modules", ".bin");
|
|
64
|
+
try {
|
|
65
|
+
for (const file of await readdir(binPath)) {
|
|
66
|
+
binCommands.set(file, file);
|
|
67
|
+
}
|
|
68
|
+
} catch (_error) {
|
|
69
|
+
}
|
|
70
|
+
dir = resolve(dir, "..");
|
|
71
|
+
} while (dir !== resolve(dir, ".."));
|
|
72
|
+
return binCommands;
|
|
73
|
+
}
|
|
74
|
+
async function getDenoTasks() {
|
|
75
|
+
const tasks = /* @__PURE__ */ new Map();
|
|
76
|
+
const cwd = process.cwd();
|
|
77
|
+
try {
|
|
78
|
+
let txt = "";
|
|
79
|
+
try {
|
|
80
|
+
const denoConf = join(cwd, "deno.json");
|
|
81
|
+
txt = await readFile(denoConf, { encoding: "utf-8" });
|
|
82
|
+
} catch (_error) {
|
|
83
|
+
const denoConf = join(cwd, "deno.jsonc");
|
|
84
|
+
txt = await readFile(denoConf, { encoding: "utf-8" });
|
|
85
|
+
}
|
|
86
|
+
if (!txt) {
|
|
87
|
+
return tasks;
|
|
88
|
+
}
|
|
89
|
+
const json = jsonc.parse(txt);
|
|
90
|
+
Object.entries(json.tasks).forEach(([name, content]) => {
|
|
91
|
+
tasks.set(name, content);
|
|
92
|
+
});
|
|
93
|
+
} catch (_error) {
|
|
94
|
+
}
|
|
95
|
+
return tasks;
|
|
96
|
+
}
|
|
97
|
+
async function getScriptContent(cmd = "") {
|
|
98
|
+
const tasks = await getDenoTasks();
|
|
99
|
+
const scripts = await getPackageScripts();
|
|
100
|
+
const binCommands = await getBinScripts();
|
|
101
|
+
const executeContent = tasks.get(cmd) || scripts.get(cmd) || binCommands.get(cmd) || false;
|
|
102
|
+
const uniq = [
|
|
103
|
+
.../* @__PURE__ */ new Set([...tasks.keys(), ...scripts.keys(), ...binCommands.keys()])
|
|
104
|
+
];
|
|
105
|
+
return [executeContent, uniq];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/xr.ts
|
|
109
|
+
var ins = sliver`
|
|
110
|
+
@help @autocompletion
|
|
111
|
+
|
|
112
|
+
xr [@command:command] #stopEarly, run command quickly. ${defaultAction}
|
|
113
|
+
`;
|
|
114
|
+
ins.type("command", async () => {
|
|
115
|
+
const [_, allScripts] = await getScriptContent();
|
|
116
|
+
return allScripts;
|
|
117
|
+
});
|
|
118
|
+
async function defaultAction(_, arg) {
|
|
119
|
+
const [command, ...params] = arg._;
|
|
120
|
+
await runScript(command, params);
|
|
121
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@0x-jerry/x",
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"repository": "https://github.com/0x-jerry/x.git",
|
|
6
|
+
"author": "Jerry Wang <x.jerry.wang@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public",
|
|
11
|
+
"registry": "https://registry.npmjs.org"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"bin": {
|
|
17
|
+
"xr": "dist/xr.js",
|
|
18
|
+
"xn": "dist/xn.js"
|
|
19
|
+
},
|
|
20
|
+
"tsup": {
|
|
21
|
+
"entry": [
|
|
22
|
+
"src/xr.ts",
|
|
23
|
+
"src/xn.ts"
|
|
24
|
+
],
|
|
25
|
+
"format": [
|
|
26
|
+
"esm"
|
|
27
|
+
],
|
|
28
|
+
"dts": true,
|
|
29
|
+
"clean": true
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@0x-jerry/silver": "^0.1.1",
|
|
33
|
+
"@0x-jerry/utils": "^1.18.0",
|
|
34
|
+
"execa": "^7.1.1",
|
|
35
|
+
"jsonc-parser": "^3.2.0",
|
|
36
|
+
"picocolors": "^1.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@0x-jerry/x-release": "^0.3.8",
|
|
40
|
+
"@types/node": "^20.4.1",
|
|
41
|
+
"tsup": "^7.1.0",
|
|
42
|
+
"tsx": "^3.12.7",
|
|
43
|
+
"typescript": "^5.1.6"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsup",
|
|
47
|
+
"dev:xr": "tsx src/xr.ts",
|
|
48
|
+
"dev:xn": "tsx src/xn.ts",
|
|
49
|
+
"release": "x-release"
|
|
50
|
+
},
|
|
51
|
+
"typings": "dist/index.d.ts"
|
|
52
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# X
|
|
2
|
+
|
|
3
|
+
Some useful command for myself.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
deno install -r -Af --import-map https://raw.githubusercontent.com/0x-jerry/x/main/import_map.json https://raw.githubusercontent.com/0x-jerry/x/main/x.ts
|
|
9
|
+
deno install -r -Af --import-map https://raw.githubusercontent.com/0x-jerry/x/main/import_map.json https://raw.githubusercontent.com/0x-jerry/x/main/xr.ts
|
|
10
|
+
deno install -r -Af --import-map https://raw.githubusercontent.com/0x-jerry/x/main/import_map.json https://raw.githubusercontent.com/0x-jerry/x/main/xn.ts
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Command Completions
|
|
14
|
+
|
|
15
|
+
Add this code to `~/.zshrc`
|
|
16
|
+
|
|
17
|
+
```zsh
|
|
18
|
+
source <(x completions zsh)
|
|
19
|
+
source <(xr completions zsh)
|
|
20
|
+
source <(xn completions zsh)
|
|
21
|
+
```
|