@j-o-r/sh 1.1.16 → 1.1.18

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/lib/SH.js CHANGED
@@ -90,8 +90,13 @@ const parseDuration = (d) => {
90
90
  }
91
91
  throw new Error(`Unknown duration: "${d}".`);
92
92
  }
93
-
94
- /** @type {Shell & { (pieces: TemplateStringsArray, ...args: *): SHDispatch }} */
93
+ /**
94
+ * A template-tag that returns an SHDispatch.
95
+ * @typedef {(pieces: TemplateStringsArray, ...args: unknown[]) => SHDispatch} SHTag
96
+ */
97
+ /**
98
+ * @type {Shell & SHTag}
99
+ */
95
100
  const SH = new Proxy(function(pieces, ...args) {
96
101
  if (pieces.some((p) => p == undefined)) {
97
102
  throw new Error(`Malformed command ${pieces}`);
@@ -99,9 +104,12 @@ const SH = new Proxy(function(pieces, ...args) {
99
104
  let cmd = pieces[0], i = 0;
100
105
  while (i < args.length) {
101
106
  let s;
107
+
102
108
  if (Array.isArray(args[i])) {
103
109
  s = args[i].map(/** @param {string} x */(x) => {
104
- let str = String(x);
110
+ // Trim every element
111
+ let str = String(x).trim();
112
+ str = str.replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t');
105
113
  // If the string contains special characters, wrap in single quotes
106
114
  if (str.match(/[ "'$`(){}[\]]/)) {
107
115
  // Escape single quotes within the string
@@ -110,15 +118,6 @@ const SH = new Proxy(function(pieces, ...args) {
110
118
  return str;
111
119
  }).join(' ');
112
120
  } else {
113
- // let str = String(args[i]);
114
- // if (str.match(/[ "'$`(){}[\]]/)) {
115
- // s = `'${str.replace(/'/g, `'\\''`)}'`;
116
- // } else {
117
- // s = str;
118
- // }
119
- // Leave string UNALTERED
120
- // Should be escaped in the code and is the responsibility of the developer
121
- // SH`do something "${filename}"`;
122
121
  s = String(args[i]);
123
122
  }
124
123
  cmd += s + pieces[++i];
@@ -146,10 +145,12 @@ const within = async (callback) => {
146
145
 
147
146
  /**
148
147
  * This function reads the standard input (stdin) from the current process.
148
+ * @returns {Promise<string>}
149
149
  * @example
150
150
  * const content = await stdin();
151
151
  */
152
152
  const readIn = async () => {
153
+ if (process.stdin.isTTY) return ''; // nothing was piped
153
154
  let buf = '';
154
155
  process.stdin.setEncoding('utf8');
155
156
  for await (const chunk of process.stdin) {
package/lib/SHExecute.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { spawnSync, spawn, exec } from 'node:child_process';
2
- import { nextTick } from 'node:process';
3
2
 
4
3
  /**
5
4
  * Kills a process and all child processes of a given process ID in Linux/Posix.
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.1.16",
5
+ "version": "1.1.18",
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",
@@ -13,7 +13,7 @@
13
13
  "test": "npm run test:sh",
14
14
  "test:sh": "scenarios/sh.js",
15
15
  "publish": "npm run release && npm publish --access public",
16
- "release": "npm pack --pack-destination=release",
16
+ "release": "npm run types && npm pack --pack-destination=release",
17
17
  "types": "rm types/* && tsc -p tsc.json"
18
18
  },
19
19
  "repository": {
package/types/SH.d.ts CHANGED
@@ -7,10 +7,18 @@ export type ResolveCallback = Function;
7
7
  * Creates a new SHDispatch object that represents a command to be executed.
8
8
  */
9
9
  export type Shell = Function;
10
- /** @type {Shell & { (pieces: TemplateStringsArray, ...args: *): SHDispatch }} */
11
- export const SH: Shell & {
12
- (pieces: TemplateStringsArray, ...args: any): SHDispatch;
13
- };
10
+ /**
11
+ * A template-tag that returns an SHDispatch.
12
+ */
13
+ export type SHTag = (pieces: TemplateStringsArray, ...args: unknown[]) => SHDispatch;
14
+ /**
15
+ * A template-tag that returns an SHDispatch.
16
+ * @typedef {(pieces: TemplateStringsArray, ...args: unknown[]) => SHDispatch} SHTag
17
+ */
18
+ /**
19
+ * @type {Shell & SHTag}
20
+ */
21
+ export const SH: Shell & SHTag;
14
22
  /**
15
23
  * Change working directory
16
24
  * @param {string} dir
@@ -47,6 +55,7 @@ export function sleep(duration: string | number): Promise<any>;
47
55
  export function retry(count: number, a: string | number | typeof expBackoff | Function, b?: Function): Promise<any>;
48
56
  /**
49
57
  * This function reads the standard input (stdin) from the current process.
58
+ * @returns {Promise<string>}
50
59
  * @example
51
60
  * const content = await stdin();
52
61
  */