@0x-jerry/x 2.0.6 → 2.1.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.
package/dist/x.d.ts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/dist/x.js ADDED
@@ -0,0 +1,123 @@
1
+ // src/x.ts
2
+ import { sliver } from "@0x-jerry/silver";
3
+
4
+ // src/commands/downloadGitRepo.ts
5
+ import path from "node:path";
6
+ import os from "node:os";
7
+ import { createWriteStream } from "node:fs";
8
+ import { pipeline } from "node:stream/promises";
9
+ import prompts from "prompts";
10
+ import pc from "picocolors";
11
+ import got from "got";
12
+ import fs from "fs-extra";
13
+ import ora from "ora";
14
+ import decompress from "decompress";
15
+ async function downloadGitRepo(opt) {
16
+ let { url, branch = "main", destDir } = opt;
17
+ if (!url) {
18
+ console.log(pc.red(`Please specify git url by using -u,--url`));
19
+ return;
20
+ }
21
+ destDir = await checkDest(destDir) || "";
22
+ if (!destDir)
23
+ return;
24
+ const gitConf = normalizeGitUrl(url);
25
+ const zipFilePath = await downloadGitBranch(gitConf, branch);
26
+ await fs.ensureDir(destDir);
27
+ await decompress(zipFilePath, destDir, {
28
+ strip: 1
29
+ });
30
+ await fs.unlink(zipFilePath);
31
+ }
32
+ function normalizeGitUrl(url) {
33
+ if (!url.startsWith("http")) {
34
+ url = `https://github.com/${url}`;
35
+ }
36
+ const seq = url.split("/");
37
+ const owner = seq.at(-2);
38
+ const repo = seq.at(-1);
39
+ const conf = {
40
+ url,
41
+ owner,
42
+ repo
43
+ };
44
+ return conf;
45
+ }
46
+ async function checkDest(dest) {
47
+ const cwd = process.cwd();
48
+ if (!dest) {
49
+ const res = await prompts({
50
+ type: "text",
51
+ name: "dest",
52
+ message: "Please specify dest folder",
53
+ initial: "."
54
+ });
55
+ dest = res.dest;
56
+ }
57
+ if (!dest)
58
+ return;
59
+ dest = path.isAbsolute(dest) ? dest : path.join(cwd, dest);
60
+ const files = fs.pathExistsSync(dest) ? await fs.readdir(dest) : null;
61
+ if (files?.length) {
62
+ const res = await prompts({
63
+ type: "confirm",
64
+ name: "override",
65
+ message: `Detect files in ${pc.cyan(dest)}, override it?`,
66
+ initial: false
67
+ });
68
+ if (!res.override)
69
+ return;
70
+ }
71
+ return dest;
72
+ }
73
+ async function downloadGitBranch(conf, branch) {
74
+ const downloadUrl = `${conf.url}/archive/refs/heads/${branch}.zip`;
75
+ const tempZipFilePath = path.join(
76
+ os.tmpdir(),
77
+ `${conf.owner}-${conf.repo}-${branch}.zip`
78
+ );
79
+ if (fs.pathExistsSync(tempZipFilePath)) {
80
+ console.log(`Template already downloaded: ${tempZipFilePath}`);
81
+ return tempZipFilePath;
82
+ }
83
+ const downloadStream = got.stream(downloadUrl);
84
+ console.log(`Start download ${downloadUrl} to ${tempZipFilePath}`);
85
+ const spinner = ora({
86
+ prefixText: `Downloading ${downloadUrl}`
87
+ }).start();
88
+ downloadStream.on("downloadProgress", (progress) => {
89
+ spinner.text = ` (${progress.percent * 100}%)`;
90
+ });
91
+ downloadStream.once("error", (e) => {
92
+ spinner.fail(e.message);
93
+ });
94
+ const zipFile = createWriteStream(tempZipFilePath);
95
+ await pipeline(downloadStream, zipFile);
96
+ spinner.succeed("Done");
97
+ return tempZipFilePath;
98
+ }
99
+
100
+ // src/x.ts
101
+ import { bootstrap } from "global-agent";
102
+ bootstrap({
103
+ environmentVariableNamespace: ""
104
+ });
105
+ var ins = sliver`
106
+ @help @autocompletion
107
+
108
+ x, Some useful subcommand.
109
+
110
+ t/template [dest path], download git repo as a template. ${defaultAction}
111
+
112
+ -u --url, Git url to download with. eg. -u 0x-jerry/x, -u https://github.com/0x-jerry/x
113
+ -b --branch, Git branch.
114
+ `;
115
+ async function defaultAction(_, args) {
116
+ let [destDir] = _;
117
+ let { url, branch = "main" } = args;
118
+ await downloadGitRepo({
119
+ url,
120
+ branch,
121
+ destDir
122
+ });
123
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0x-jerry/x",
3
- "version": "2.0.6",
3
+ "version": "2.1.1",
4
4
  "main": "dist/index.js",
5
5
  "repository": "https://github.com/0x-jerry/x.git",
6
6
  "author": "Jerry Wang <x.jerry.wang@gmail.com>",
@@ -15,12 +15,14 @@
15
15
  ],
16
16
  "bin": {
17
17
  "xr": "dist/xr.js",
18
- "xn": "dist/xn.js"
18
+ "xn": "dist/xn.js",
19
+ "x": "dist/x.js"
19
20
  },
20
21
  "tsup": {
21
22
  "entry": [
22
23
  "src/xr.ts",
23
- "src/xn.ts"
24
+ "src/xn.ts",
25
+ "src/x.ts"
24
26
  ],
25
27
  "format": [
26
28
  "esm"
@@ -29,23 +31,34 @@
29
31
  "clean": true
30
32
  },
31
33
  "dependencies": {
32
- "@0x-jerry/silver": "^0.1.2",
33
- "@0x-jerry/utils": "^1.18.0",
34
- "execa": "^7.1.1",
34
+ "@0x-jerry/silver": "^0.1.4",
35
+ "@0x-jerry/utils": "^2.0.0",
36
+ "decompress": "^4.2.1",
37
+ "execa": "^8.0.1",
38
+ "fs-extra": "^11.1.1",
39
+ "global-agent": "^3.0.0",
40
+ "got": "^13.0.0",
35
41
  "jsonc-parser": "^3.2.0",
36
- "picocolors": "^1.0.0"
42
+ "ora": "^7.0.1",
43
+ "picocolors": "^1.0.0",
44
+ "prompts": "^2.4.2"
37
45
  },
38
46
  "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"
47
+ "@0x-jerry/x-release": "^0.3.9",
48
+ "@types/decompress": "^4.2.4",
49
+ "@types/fs-extra": "^11.0.2",
50
+ "@types/global-agent": "^2.1.1",
51
+ "@types/node": "^20.6.2",
52
+ "@types/prompts": "^2.4.4",
53
+ "tsup": "^7.2.0",
54
+ "tsx": "^3.12.10",
55
+ "typescript": "^5.2.2"
44
56
  },
45
57
  "scripts": {
46
58
  "build": "tsup",
47
59
  "dev:xr": "tsx src/xr.ts",
48
60
  "dev:xn": "tsx src/xn.ts",
61
+ "dev:x": "tsx src/x.ts",
49
62
  "release": "x-release"
50
63
  },
51
64
  "typings": "dist/index.d.ts"
package/readme.md CHANGED
@@ -5,9 +5,7 @@ Some useful command for myself.
5
5
  ## Install
6
6
 
7
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
8
+ npm i -g @0x-jerry/x
11
9
  ```
12
10
 
13
11
  ## Command Completions
@@ -15,7 +13,7 @@ deno install -r -Af --import-map https://raw.githubusercontent.com/0x-jerry/x/ma
15
13
  Add this code to `~/.zshrc`
16
14
 
17
15
  ```zsh
18
- source <(x completions zsh)
19
- source <(xr completions zsh)
20
- source <(xn completions zsh)
16
+ source <(x completions)
17
+ source <(xr completions)
18
+ source <(xn completions)
21
19
  ```