naen-cli 0.0.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/README.md +3 -0
- package/bin/.gitignore +2 -0
- package/dist/getBinary.d.ts +3 -0
- package/dist/getBinary.js +48 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +136 -0
- package/dist/install.d.ts +1 -0
- package/dist/install.js +8 -0
- package/dist/run.d.ts +1 -0
- package/dist/run.js +17 -0
- package/dist/test.d.ts +1 -0
- package/dist/uninstall.d.ts +1 -0
- package/dist/uninstall.js +4 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.js +33 -0
- package/getBinary.js +1 -0
- package/install.js +1 -0
- package/package.json +43 -0
- package/run.js +2 -0
- package/uninstall.js +3 -0
package/README.md
ADDED
package/bin/.gitignore
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBinary = exports.AWSUrl = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const os = require("os");
|
|
7
|
+
const { version } = require("../package.json");
|
|
8
|
+
const NAME = "raen";
|
|
9
|
+
function getPlatform() {
|
|
10
|
+
const type = os.type();
|
|
11
|
+
const arch = os.arch();
|
|
12
|
+
let typeDict = {
|
|
13
|
+
"Darwin": "apple-darwin",
|
|
14
|
+
"Linux": "unknown-linux-gnu",
|
|
15
|
+
"Windows_NT": "pc-windows-msvc"
|
|
16
|
+
};
|
|
17
|
+
let archDict = {
|
|
18
|
+
"x64": "x86_64",
|
|
19
|
+
"arm64": "aarch64"
|
|
20
|
+
};
|
|
21
|
+
//@ts-ignore
|
|
22
|
+
let rust_type = typeDict[type];
|
|
23
|
+
//@ts-ignore
|
|
24
|
+
let rust_arch = archDict[arch];
|
|
25
|
+
if (rust_type && rust_arch) {
|
|
26
|
+
return [rust_type, rust_arch];
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`Unsupported platform: ${type} ${arch}`);
|
|
29
|
+
}
|
|
30
|
+
function AWSUrl() {
|
|
31
|
+
const [platform, arch] = getPlatform();
|
|
32
|
+
return `https://github.com/AhaLabs/${NAME}/releases/download/v${version}/${NAME}-v${version}-${arch}-${platform}.tar.gz`;
|
|
33
|
+
}
|
|
34
|
+
exports.AWSUrl = AWSUrl;
|
|
35
|
+
function getBinary(name = NAME) {
|
|
36
|
+
if (!process.env["RAEN_BIN_PATH"]) {
|
|
37
|
+
process.env["RAEN_BINARY_PATH"] = (0, path_1.join)(os.homedir(), `.${NAME}`, NAME);
|
|
38
|
+
}
|
|
39
|
+
// Will use version after publishing to AWS
|
|
40
|
+
// const version = require("./package.json").version;
|
|
41
|
+
const fromEnv = process.env["RAEN_ARTIFACT_URL"];
|
|
42
|
+
const urls = [AWSUrl()];
|
|
43
|
+
if (fromEnv) {
|
|
44
|
+
urls.unshift(fromEnv);
|
|
45
|
+
}
|
|
46
|
+
return _1.Binary.create(name, urls);
|
|
47
|
+
}
|
|
48
|
+
exports.getBinary = getBinary;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { URL } from "url";
|
|
3
|
+
export declare class Binary {
|
|
4
|
+
name: string;
|
|
5
|
+
installDir: string;
|
|
6
|
+
urls: URL[];
|
|
7
|
+
static readonly DEFAULT_INSTALL_DIR: string;
|
|
8
|
+
protected constructor(name: string, url: string | URL | string[] | URL[], installDir?: string);
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param name binary name, e.g. 'git'
|
|
12
|
+
* @param path URL of where to find binary
|
|
13
|
+
* @param destination Directory to put the binary
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
static create(name: string, path: string | URL | string[] | URL[], destination?: string): Promise<Binary>;
|
|
17
|
+
get binPath(): string;
|
|
18
|
+
download(url: URL): Promise<void>;
|
|
19
|
+
install(): Promise<boolean>;
|
|
20
|
+
exists(): Promise<boolean>;
|
|
21
|
+
run(cliArgs?: string[], options?: {
|
|
22
|
+
stdio: ("inherit" | null)[];
|
|
23
|
+
}): Promise<number>;
|
|
24
|
+
runAndExit(cliArgs?: string[], options?: {
|
|
25
|
+
stdio: ("inherit" | null)[];
|
|
26
|
+
}): Promise<void>;
|
|
27
|
+
uninstall(): Promise<void>;
|
|
28
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Binary = void 0;
|
|
4
|
+
const fs = require("fs/promises");
|
|
5
|
+
const url_1 = require("url");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const tar = require("tar");
|
|
8
|
+
const got_1 = require("got");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const child_process_1 = require("child_process");
|
|
11
|
+
const stream = require("stream");
|
|
12
|
+
const util_1 = require("util");
|
|
13
|
+
const pipeline = (0, util_1.promisify)(stream.pipeline);
|
|
14
|
+
class Binary {
|
|
15
|
+
constructor(name, url, installDir = Binary.DEFAULT_INSTALL_DIR) {
|
|
16
|
+
Object.defineProperty(this, "name", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: name
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "installDir", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: installDir
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "urls", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
let errors = [];
|
|
35
|
+
let urls = [];
|
|
36
|
+
if (typeof url === "string" || url instanceof url_1.URL) {
|
|
37
|
+
urls.push(url);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
if (url.length == 0) {
|
|
41
|
+
throw new Error("No URL provided got empty array");
|
|
42
|
+
}
|
|
43
|
+
urls = url;
|
|
44
|
+
}
|
|
45
|
+
if (!name || typeof name !== "string") {
|
|
46
|
+
errors.push("You must specify the name of your binary as a string");
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
this.urls = urls.map((path) => typeof path === "string" ? new url_1.URL(path) : path);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
errors.push(e);
|
|
53
|
+
}
|
|
54
|
+
if (errors.length > 0) {
|
|
55
|
+
errors.push('\nCorrect usage: new Binary("my-binary", "https://example.com/binary/download.tar.gz"');
|
|
56
|
+
errors.unshift("One or more of the parameters you passed to the Binary constructor are invalid:\n");
|
|
57
|
+
throw new Error(errors.join("\n"));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
*
|
|
62
|
+
* @param name binary name, e.g. 'git'
|
|
63
|
+
* @param path URL of where to find binary
|
|
64
|
+
* @param destination Directory to put the binary
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
static async create(name, path, destination) {
|
|
68
|
+
const bin = new Binary(name, path, destination !== null && destination !== void 0 ? destination : (await (0, utils_1.searchPath)(name)));
|
|
69
|
+
if (destination === bin.installDir) {
|
|
70
|
+
await fs.mkdir(bin.installDir, { recursive: true });
|
|
71
|
+
}
|
|
72
|
+
return bin;
|
|
73
|
+
}
|
|
74
|
+
get binPath() {
|
|
75
|
+
return (0, path_1.join)(this.installDir, this.name);
|
|
76
|
+
}
|
|
77
|
+
download(url) {
|
|
78
|
+
return pipeline(got_1.default.stream(url), new stream.PassThrough(), tar.x({ strip: 1, C: this.installDir }));
|
|
79
|
+
}
|
|
80
|
+
async install() {
|
|
81
|
+
for (let url of this.urls) {
|
|
82
|
+
try {
|
|
83
|
+
await this.download(url);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
catch (error) { }
|
|
87
|
+
}
|
|
88
|
+
throw new Error(`Failed to download from: \n${this.urls.join("\n")}`);
|
|
89
|
+
}
|
|
90
|
+
async exists() {
|
|
91
|
+
return await (0, utils_1.fileExists)(this.binPath);
|
|
92
|
+
}
|
|
93
|
+
async run(cliArgs, options = { stdio: [null, utils_1.inherit, utils_1.inherit] }) {
|
|
94
|
+
if (!(await this.exists())) {
|
|
95
|
+
try {
|
|
96
|
+
await this.install();
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
console.error(err);
|
|
100
|
+
return 1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const args = cliArgs !== null && cliArgs !== void 0 ? cliArgs : process.argv.slice(2);
|
|
104
|
+
const result = (0, child_process_1.spawn)(this.binPath, args, options);
|
|
105
|
+
result.on("error", (error) => {
|
|
106
|
+
console.log(error);
|
|
107
|
+
});
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
result.on("close", (code) => {
|
|
110
|
+
if (!code) {
|
|
111
|
+
resolve(code !== null && code !== void 0 ? code : 0);
|
|
112
|
+
}
|
|
113
|
+
reject(code);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
async runAndExit(cliArgs, options = { stdio: [null, utils_1.inherit, utils_1.inherit] }) {
|
|
118
|
+
process.exit(await this.run(cliArgs, options));
|
|
119
|
+
}
|
|
120
|
+
async uninstall() {
|
|
121
|
+
if (this.installDir === Binary.DEFAULT_INSTALL_DIR &&
|
|
122
|
+
(await this.exists())) {
|
|
123
|
+
await (0, utils_1.rm)(this.binPath);
|
|
124
|
+
if (await this.exists()) {
|
|
125
|
+
throw new Error(`Failed to remove binary located ${this.binPath}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.Binary = Binary;
|
|
131
|
+
Object.defineProperty(Binary, "DEFAULT_INSTALL_DIR", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: true,
|
|
134
|
+
writable: true,
|
|
135
|
+
value: (0, path_1.join)(__dirname, "..", "bin")
|
|
136
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/install.js
ADDED
package/dist/run.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/run.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const getBinary_1 = require("./getBinary");
|
|
4
|
+
async function run() {
|
|
5
|
+
try {
|
|
6
|
+
const bin = await (0, getBinary_1.getBinary)();
|
|
7
|
+
if (process.argv.length < 3) {
|
|
8
|
+
process.argv.push("--help");
|
|
9
|
+
}
|
|
10
|
+
bin.runAndExit();
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
console.error(err);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
run();
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rm = exports.inherit = exports.searchPath = exports.fileExists = void 0;
|
|
4
|
+
const promises_1 = require("fs/promises");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
async function fileExists(s) {
|
|
7
|
+
try {
|
|
8
|
+
const f = await (0, promises_1.stat)(s);
|
|
9
|
+
return f.isFile();
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.fileExists = fileExists;
|
|
16
|
+
async function searchPath(filename) {
|
|
17
|
+
const binPath = process.env["RAEN_BINARY_PATH"];
|
|
18
|
+
if (binPath &&
|
|
19
|
+
binPath.length > 0 &&
|
|
20
|
+
(await fileExists((0, path_1.join)(binPath, filename)))) {
|
|
21
|
+
return binPath;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
exports.searchPath = searchPath;
|
|
26
|
+
exports.inherit = "inherit";
|
|
27
|
+
async function rm(path) {
|
|
28
|
+
try {
|
|
29
|
+
await (0, promises_1.rm)(path);
|
|
30
|
+
}
|
|
31
|
+
catch (e) { }
|
|
32
|
+
}
|
|
33
|
+
exports.rm = rm;
|
package/getBinary.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./dist/getBinary");
|
package/install.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./dist/install");
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "naen-cli",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Tools for generating to and from wit format",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"WebAssembly",
|
|
7
|
+
"Wasm",
|
|
8
|
+
"wit",
|
|
9
|
+
"NEAR"
|
|
10
|
+
],
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": "https://github.com/raendev/raen",
|
|
15
|
+
"author": "Willem Wyndham (willem@ahalabs.dev)",
|
|
16
|
+
"bin": {
|
|
17
|
+
"raen": "./run.js"
|
|
18
|
+
},
|
|
19
|
+
"private": false,
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"got": "^11.8.2",
|
|
22
|
+
"tar": "^6.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@sindresorhus/tsconfig": "^2.0.0",
|
|
26
|
+
"@types/node": "^16.4.10",
|
|
27
|
+
"@types/tar": "^4.0.5",
|
|
28
|
+
"ava": "3.15.0",
|
|
29
|
+
"ts-node": "^10.1.0",
|
|
30
|
+
"typescript": "^4.3.5"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"postinstall": "node ./install.js",
|
|
34
|
+
"preinstall": "node ./uninstall.js",
|
|
35
|
+
"test": "ava",
|
|
36
|
+
"build": "tsc"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"*.js",
|
|
40
|
+
"dist/*.ts",
|
|
41
|
+
"dist/*.js"
|
|
42
|
+
]
|
|
43
|
+
}
|
package/run.js
ADDED
package/uninstall.js
ADDED