@j-o-r/sh 1.0.4 → 1.0.5
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 +1 -1
- package/lib/SH.js +2 -76
- package/package.json +2 -2
- package/types/SH.d.ts +0 -8
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ This class is returned by the `SH` function. Here's a summary of its methods and
|
|
|
94
94
|
|
|
95
95
|
- Create a command from a string
|
|
96
96
|
```javascript
|
|
97
|
-
const command =
|
|
97
|
+
const command = "uname -r";
|
|
98
98
|
const content = await SH`${command}`.run();
|
|
99
99
|
console.log(content);
|
|
100
100
|
```
|
package/lib/SH.js
CHANGED
|
@@ -45,37 +45,6 @@ import SHDispatch from './SHDispatch.js';
|
|
|
45
45
|
* const command = await SH`echo 'Hello, world!'`.run();
|
|
46
46
|
*/
|
|
47
47
|
|
|
48
|
-
/**
|
|
49
|
-
* escape paramater commands
|
|
50
|
-
* @param {string} arg
|
|
51
|
-
* @returns {string}
|
|
52
|
-
*/
|
|
53
|
-
const quote = (arg) => {
|
|
54
|
-
if (/^[a-z0-9/_.\-@:=]+$/i.test(arg) || arg === '') {
|
|
55
|
-
return arg;
|
|
56
|
-
}
|
|
57
|
-
return (`'` +
|
|
58
|
-
arg
|
|
59
|
-
.replace(/\\/g, '\\\\')
|
|
60
|
-
.replace(/'/g, "\\'")
|
|
61
|
-
.replace(/\f/g, '\\f')
|
|
62
|
-
.replace(/\n/g, '\\n')
|
|
63
|
-
.replace(/\r/g, '\\r')
|
|
64
|
-
.replace(/\t/g, '\\t')
|
|
65
|
-
.replace(/\v/g, '\\v')
|
|
66
|
-
.replace(/\0/g, '\\0') +
|
|
67
|
-
`'`);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Escape CLI arguments
|
|
71
|
-
* @param {string[]} arg
|
|
72
|
-
* @retruns {string}
|
|
73
|
-
*/
|
|
74
|
-
const sanitizeArg = (arg) => {
|
|
75
|
-
const s = `${arg}`;
|
|
76
|
-
return quote(s);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
48
|
/**
|
|
80
49
|
* 4ms, 5s || 5
|
|
81
50
|
* @param {number|string} d
|
|
@@ -104,57 +73,15 @@ const SH = new Proxy(function(pieces, ...args) {
|
|
|
104
73
|
while (i < args.length) {
|
|
105
74
|
let s;
|
|
106
75
|
if (Array.isArray(args[i])) {
|
|
107
|
-
s = args[i].map((x) =>
|
|
76
|
+
s = args[i].map((x) => x).join(' ');
|
|
108
77
|
}
|
|
109
78
|
else {
|
|
110
|
-
s =
|
|
79
|
+
s = args[i];
|
|
111
80
|
}
|
|
112
81
|
cmd += s + pieces[++i];
|
|
113
82
|
}
|
|
114
83
|
return new SHDispatch(cmd);
|
|
115
84
|
}, {});
|
|
116
|
-
/**
|
|
117
|
-
* Splits a command string into an array of arguments, handling quoted strings.
|
|
118
|
-
* This fixes a problem when a command is like this: SH`${command}`
|
|
119
|
-
*
|
|
120
|
-
* @param {string} command - The command string to split.
|
|
121
|
-
* @returns {string[]} - The array of command arguments.
|
|
122
|
-
*/
|
|
123
|
-
const args = (command) => {
|
|
124
|
-
const args = [];
|
|
125
|
-
let currentArg = '';
|
|
126
|
-
let insideQuotes = false;
|
|
127
|
-
let quoteChar = null;
|
|
128
|
-
|
|
129
|
-
for (let i = 0; i < command.length; i++) {
|
|
130
|
-
const char = command[i];
|
|
131
|
-
|
|
132
|
-
if (char === '"' || char === "'") {
|
|
133
|
-
if (insideQuotes && quoteChar === char && command[i - 1] !== '\\') {
|
|
134
|
-
insideQuotes = false;
|
|
135
|
-
quoteChar = null;
|
|
136
|
-
} else if (!insideQuotes) {
|
|
137
|
-
insideQuotes = true;
|
|
138
|
-
quoteChar = char;
|
|
139
|
-
} else {
|
|
140
|
-
currentArg += char;
|
|
141
|
-
}
|
|
142
|
-
} else if (char === ' ' && !insideQuotes) {
|
|
143
|
-
if (currentArg.length > 0) {
|
|
144
|
-
args.push(currentArg);
|
|
145
|
-
currentArg = '';
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
currentArg += char;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (currentArg.length > 0) {
|
|
153
|
-
args.push(currentArg);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return args.map(arg => arg.replace(/\\(['"])/g, '$1'));
|
|
157
|
-
}
|
|
158
85
|
/**
|
|
159
86
|
* Create a async context in an sync block
|
|
160
87
|
* @param {function} callback - async function
|
|
@@ -286,7 +213,6 @@ function* expBackoff(max = '60s', rand = '100ms') {
|
|
|
286
213
|
}
|
|
287
214
|
export {
|
|
288
215
|
SH,
|
|
289
|
-
args,
|
|
290
216
|
cd,
|
|
291
217
|
sleep,
|
|
292
218
|
retry,
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@j-o-r/sh",
|
|
3
3
|
"author": "Jorrit Duin <j-o-r@duin.work>",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.5",
|
|
6
6
|
"description": "Execute shell commands on Linux-based systems from javascript",
|
|
7
7
|
"main": "lib/SH.js",
|
|
8
8
|
"types": "types/SH.d.ts",
|
|
@@ -46,4 +46,4 @@
|
|
|
46
46
|
"process-promise",
|
|
47
47
|
"process-output"
|
|
48
48
|
]
|
|
49
|
-
}
|
|
49
|
+
}
|
package/types/SH.d.ts
CHANGED
|
@@ -6,14 +6,6 @@ export type Shell = Function;
|
|
|
6
6
|
export const SH: Shell & {
|
|
7
7
|
(pieces: TemplateStringsArray, ...args: any): SHDispatch;
|
|
8
8
|
};
|
|
9
|
-
/**
|
|
10
|
-
* Splits a command string into an array of arguments, handling quoted strings.
|
|
11
|
-
* This fixes a problem when a command is like this: SH`${command}`
|
|
12
|
-
*
|
|
13
|
-
* @param {string} command - The command string to split.
|
|
14
|
-
* @returns {string[]} - The array of command arguments.
|
|
15
|
-
*/
|
|
16
|
-
export function args(command: string): string[];
|
|
17
9
|
/**
|
|
18
10
|
* Change working directory
|
|
19
11
|
* @param {string} dir
|