-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (117 loc) · 3.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Require Third-party Dependencies
import { execa } from "execa";
// Require Internal Dependencies
import { taggedString } from "./src/utils.js";
import { NimAddr } from "./src/nimAddr.js";
// CONSTANTS
const NIM_CMD = taggedString`${"path"} -u ${"login"} -p '${"password"}' ${"addr"} ${"callback"} `;
const ADDR_SEP = "/";
const PDS_VOID = "''";
const DEFAULT_CMD_TIMEOUT_MS = 2000;
const PDS_REGEX = /([a-zA-Z0-9_-]+)\s+(PDS_PCH|PDS_I|PDS_PDS|PDS_PPDS)\s+[0-9]+\s?(.*)/gm;
const PDS = {
INT: "PDS_I",
STRING: "PDS_PCH",
ARRAY: "PDS_PPDS",
HASH: "PDS_PDS"
};
/**
* @func parsePUStdout
* @param {!String} str PU stdout string
* @returns {Map<String, any>}
*/
function parsePUStdout(str) {
const parseMap = Object.create(null);
let result;
let PPDSName = null;
let CurrentPDS = null;
while ((result = PDS_REGEX.exec(str)) !== null) {
const [, varName, varType, varValue] = result;
if (varType === PDS.STRING || varType === PDS.INT) {
const convertedValue = varType === PDS.INT ? Number(varValue) : varValue;
if (CurrentPDS === null) {
parseMap[varName] = convertedValue;
}
else if (PPDSName === null) {
parseMap[CurrentPDS][varName] = convertedValue;
}
else {
parseMap[PPDSName][CurrentPDS][varName] = convertedValue;
}
}
else if (varType === PDS.ARRAY) {
PPDSName = varName;
parseMap[varName] = [];
}
else if (varType === PDS.HASH) {
CurrentPDS = varName;
if (PPDSName === null) {
parseMap[varName] = Object.create(null);
}
else if (/^\d+$/.test(CurrentPDS)) {
parseMap[PPDSName][varName] = Object.create(null);
}
else {
PPDSName = null;
parseMap[varName] = Object.create(null);
}
}
}
return parseMap;
}
/**
* @function pu
* @param {*} options options
* @returns {*}
*
* @throws {TypeError}
*/
function pu(options = Object.create(null)) {
const { login, password, path, debug = false, timeout = DEFAULT_CMD_TIMEOUT_MS } = options;
if (typeof login !== "string") {
throw new TypeError("login must be a string");
}
if (typeof password !== "string") {
throw new TypeError("password must be a string");
}
if (typeof path !== "string") {
throw new TypeError("path must be a string");
}
if (typeof debug !== "boolean") {
throw new TypeError("debug must be a boolean");
}
if (typeof timeout !== "number") {
throw new TypeError("timeout must be a number");
}
return async(fullAddr, args = []) => {
let addr;
let callback;
if (fullAddr instanceof NimAddr) {
addr = fullAddr.toString();
callback = fullAddr.callback;
}
else {
const sRet = fullAddr.split(ADDR_SEP);
callback = sRet.pop();
addr = sRet.length === 0 ? "hub" : sRet.join(ADDR_SEP);
}
// eslint-disable-next-line
const cmd = NIM_CMD({ login, password, path, addr, callback }).concat(args.join(" "));
if (debug) {
console.log(`cmd => ${cmd}`);
}
try {
const { stdout } = await execa(cmd, { timeout });
return parsePUStdout(stdout);
}
catch (err) {
const [, reason] = /failed:\s+(.*)/.exec(err.stdout);
if (debug) {
delete err.stdout;
console.log(err);
}
throw new Error(reason);
}
};
}
module.exports = { pu, PDS_VOID, NimAddr };