@zerops/zcli 1.0.33 → 1.0.35
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/package.json +7 -5
- package/utils/binary-source.js +131 -0
- package/utils/binary.js +1 -1
package/package.json
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zerops/zcli",
|
|
3
|
-
"version": "v1.0.
|
|
3
|
+
"version": "v1.0.35",
|
|
4
4
|
"description": "The command-line interface used for interacting with Zerops Platform.",
|
|
5
5
|
"main": "./utils/binary.js",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"private": false,
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@
|
|
10
|
-
"@octokit/core": "^6.1.2",
|
|
9
|
+
"@octokit/core": "6.1.2",
|
|
11
10
|
"actions-toolkit": "^6.0.1",
|
|
12
|
-
"
|
|
11
|
+
"axios": "1.7.9",
|
|
12
|
+
"rimraf": "3.0.2",
|
|
13
|
+
"tar": "^6.2.1",
|
|
14
|
+
"yargs": "17.7.2"
|
|
13
15
|
},
|
|
14
16
|
"devDependencies": {
|
|
15
|
-
"standard": "
|
|
17
|
+
"standard": "17.1.0"
|
|
16
18
|
},
|
|
17
19
|
"bin": {
|
|
18
20
|
"zcli": "utils/run.js"
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
const { existsSync, mkdirSync } = require("fs");
|
|
2
|
+
const { join } = require("path");
|
|
3
|
+
const { spawnSync } = require("child_process");
|
|
4
|
+
|
|
5
|
+
const axios = require("axios");
|
|
6
|
+
const tar = require("tar");
|
|
7
|
+
const rimraf = require("rimraf");
|
|
8
|
+
|
|
9
|
+
const error = msg => {
|
|
10
|
+
console.error(msg);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
class Binary {
|
|
15
|
+
constructor(url, data) {
|
|
16
|
+
if (typeof url !== "string") {
|
|
17
|
+
errors.push("url must be a string");
|
|
18
|
+
} else {
|
|
19
|
+
try {
|
|
20
|
+
new URL(url);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
errors.push(e);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
let errors = [];
|
|
26
|
+
if (data.name && typeof data.name !== "string") {
|
|
27
|
+
errors.push("name must be a string");
|
|
28
|
+
}
|
|
29
|
+
if (data.installDirectory && typeof data.installDirectory !== "string") {
|
|
30
|
+
errors.push("installDirectory must be a string");
|
|
31
|
+
}
|
|
32
|
+
if (!data.installDirectory && !data.name) {
|
|
33
|
+
errors.push("You must specify either name or installDirectory");
|
|
34
|
+
}
|
|
35
|
+
if (errors.length > 0) {
|
|
36
|
+
let errorMsg = "Your Binary constructor is invalid:";
|
|
37
|
+
errors.forEach(error => {
|
|
38
|
+
errorMsg += error;
|
|
39
|
+
});
|
|
40
|
+
error(errorMsg);
|
|
41
|
+
}
|
|
42
|
+
this.url = url;
|
|
43
|
+
this.name = data.name || -1;
|
|
44
|
+
this.installDirectory = data.installDirectory || join(__dirname, "bin");
|
|
45
|
+
this.binaryDirectory = -1;
|
|
46
|
+
this.binaryPath = -1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_getInstallDirectory() {
|
|
50
|
+
if (!existsSync(this.installDirectory)) {
|
|
51
|
+
mkdirSync(this.installDirectory, { recursive: true });
|
|
52
|
+
}
|
|
53
|
+
return this.installDirectory;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
_getBinaryDirectory() {
|
|
57
|
+
const installDirectory = this._getInstallDirectory();
|
|
58
|
+
const binaryDirectory = join(this.installDirectory, "bin");
|
|
59
|
+
if (existsSync(binaryDirectory)) {
|
|
60
|
+
this.binaryDirectory = binaryDirectory;
|
|
61
|
+
} else {
|
|
62
|
+
error(`You have not installed ${this.name ? this.name : "this package"}`);
|
|
63
|
+
}
|
|
64
|
+
return this.binaryDirectory;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_getBinaryPath() {
|
|
68
|
+
if (this.binaryPath === -1) {
|
|
69
|
+
const binaryDirectory = this._getBinaryDirectory();
|
|
70
|
+
this.binaryPath = join(binaryDirectory, this.name);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return this.binaryPath;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
install() {
|
|
77
|
+
const dir = this._getInstallDirectory();
|
|
78
|
+
if (!existsSync(dir)) {
|
|
79
|
+
mkdirSync(dir, { recursive: true });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.binaryDirectory = join(dir, "bin");
|
|
83
|
+
|
|
84
|
+
if (existsSync(this.binaryDirectory)) {
|
|
85
|
+
rimraf.sync(this.binaryDirectory);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
mkdirSync(this.binaryDirectory, { recursive: true });
|
|
89
|
+
|
|
90
|
+
console.log(`Downloading release from ${this.url}`);
|
|
91
|
+
|
|
92
|
+
return axios({ url: this.url, responseType: "stream" })
|
|
93
|
+
.then(res => {
|
|
94
|
+
res.data.pipe(tar.x({ strip: 1, C: this.binaryDirectory }));
|
|
95
|
+
})
|
|
96
|
+
.then(() => {
|
|
97
|
+
console.log(
|
|
98
|
+
`${this.name ? this.name : "Your package"} has been installed!`
|
|
99
|
+
);
|
|
100
|
+
})
|
|
101
|
+
.catch(e => {
|
|
102
|
+
error(`Error fetching release: ${e.message}`);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
uninstall() {
|
|
107
|
+
if (existsSync(this._getInstallDirectory())) {
|
|
108
|
+
rimraf.sync(this.installDirectory);
|
|
109
|
+
console.log(
|
|
110
|
+
`${this.name ? this.name : "Your package"} has been uninstalled`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
run() {
|
|
116
|
+
const binaryPath = this._getBinaryPath();
|
|
117
|
+
const [, , ...args] = process.argv;
|
|
118
|
+
|
|
119
|
+
const options = { cwd: process.cwd(), stdio: "inherit" };
|
|
120
|
+
|
|
121
|
+
const result = spawnSync(binaryPath, args, options);
|
|
122
|
+
|
|
123
|
+
if (result.error) {
|
|
124
|
+
error(result.error);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
process.exit(result.status);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = Binary;
|
package/utils/binary.js
CHANGED