Browse Source

Merge remote-tracking branch 'sirius/master'

Christian Kahlau 1 year ago
parent
commit
d16bb5238b
7 changed files with 234 additions and 103 deletions
  1. 1 1
      file/index.js
  2. 1 1
      package.json
  3. 3 2
      shell/index.d.ts
  4. 1 1
      shell/index.d.ts.map
  5. 92 33
      shell/index.js
  6. 0 0
      shell/index.js.map
  7. 136 65
      shell/index.ts

+ 1 - 1
file/index.js

@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
     function verb(n) { return function (v) { return step([n, v]); }; }
     function step(op) {
         if (f) throw new TypeError("Generator is already executing.");
-        while (_) try {
+        while (g && (g = 0, op[0] && (_ = 0)), _) try {
             if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
             if (y = 0, t) op = [op[0] & 2, t.value];
             switch (op[0]) {

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "node-utils",
-  "version": "1.0.5",
+  "version": "1.0.7",
   "description": "Shared common library for JavaScript in NodeJS",
   "repository": {
     "type": "git",

+ 3 - 2
shell/index.d.ts

@@ -1,4 +1,5 @@
 /// <reference types="node" />
-export declare function exec(command: string, stdout?: ((...args: any[]) => void) | NodeJS.WriteStream, stderr?: ((...args: any[]) => void) | NodeJS.WriteStream): Promise<string>;
-export declare function spawn(command: string, args: string[], stdout?: NodeJS.WriteStream, stderr?: NodeJS.WriteStream): Promise<void>;
+/// <reference types="node" />
+export declare function exec(command: string, stdout?: ((...args: any[]) => void) | NodeJS.WriteStream, stderr?: ((...args: any[]) => void) | NodeJS.WriteStream, stdin?: NodeJS.ReadableStream | string): Promise<string>;
+export declare function spawn(command: string, args: string[], stdout?: ((...args: any[]) => void) | NodeJS.WriteStream, stderr?: ((...args: any[]) => void) | NodeJS.WriteStream, stdin?: NodeJS.ReadableStream | string): Promise<void>;
 //# sourceMappingURL=index.d.ts.map

+ 1 - 1
shell/index.d.ts.map

@@ -1 +1 @@
-{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAIA,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,mBAqDvJ;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,WAAW,iBAqB9G"}
+{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAIA,wBAAgB,IAAI,CAClB,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACxD,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACxD,KAAK,CAAC,EAAE,MAAM,CAAC,cAAc,GAAG,MAAM,mBAsEvC;AAED,wBAAgB,KAAK,CACnB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACxD,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACxD,KAAK,CAAC,EAAE,MAAM,CAAC,cAAc,GAAG,MAAM,iBAkEvC"}

+ 92 - 33
shell/index.js

@@ -3,30 +3,104 @@ Object.defineProperty(exports, "__esModule", { value: true });
 exports.spawn = exports.exec = void 0;
 var child_process_1 = require("child_process");
 var MAX_BUFFER = 10 * Math.pow(2, 20);
-function exec(command, stdout, stderr) {
+function exec(command, stdout, stderr, stdin) {
     return new Promise(function (resolve, reject) {
-        var _a, _b, _c, _d;
         try {
-            var stdoutbuf_1 = "";
-            var stderrbuf_1 = "";
+            var stdoutbuf_1 = '';
+            var stderrbuf_1 = '';
             // EXEC CHILD PROCESS
             var p = (0, child_process_1.exec)(command, { maxBuffer: MAX_BUFFER }, function (err, out) {
-                if (err)
-                    return reject(err);
                 if (stdoutbuf_1.length > 0 && typeof stdout === 'function')
                     stdout(stdoutbuf_1);
                 if (stderrbuf_1.length > 0 && typeof stderr === 'function')
                     stderr(stderrbuf_1);
+                if (err)
+                    return reject(err);
                 resolve(out);
             });
+            // PIPE STDIN (?)
+            if (p.stdin) {
+                if (typeof stdin === 'string') {
+                    p.stdin.setDefaultEncoding('utf-8');
+                    if (!stdin.endsWith('\n'))
+                        stdin += '\n';
+                    p.stdin.write(stdin);
+                    p.stdin.end();
+                }
+                else if (typeof stdin !== 'undefined') {
+                    stdin.pipe(p.stdin);
+                }
+            }
+            // PIPE STDOUT
+            if (p.stdout) {
+                if (typeof stdout === 'function') {
+                    p.stdout.on('data', function (chunk) {
+                        stdoutbuf_1 += chunk;
+                        var i = -1;
+                        while ((i = stdoutbuf_1.indexOf('\n')) >= 0) {
+                            var line = stdoutbuf_1.substring(0, i);
+                            stdoutbuf_1 = stdoutbuf_1.substring(i + 1);
+                            if (typeof stdout === 'function') {
+                                stdout(line);
+                            }
+                        }
+                    });
+                }
+                else if (typeof stdout !== 'undefined') {
+                    p.stdout.pipe(stdout);
+                }
+            }
+            // PIPE STDERR
+            if (p.stderr) {
+                if (typeof stderr === 'function') {
+                    p.stderr.on('data', function (chunk) {
+                        stderrbuf_1 += chunk;
+                        var i = -1;
+                        while ((i = stderrbuf_1.indexOf('\n')) >= 0) {
+                            var line = stderrbuf_1.substring(0, i);
+                            stderrbuf_1 = stderrbuf_1.substring(i + 1);
+                            if (typeof stderr === 'function') {
+                                stderr(line);
+                            }
+                        }
+                    });
+                }
+                else if (typeof stderr !== 'undefined') {
+                    p.stderr.pipe(stderr);
+                }
+            }
+        }
+        catch (err) {
+            reject(err);
+        }
+    });
+}
+exports.exec = exec;
+function spawn(command, args, stdout, stderr, stdin) {
+    return new Promise(function (resolve, reject) {
+        try {
+            var stdoutbuf_2 = '';
+            var stderrbuf_2 = '';
+            var p = (0, child_process_1.spawn)(command, args);
+            // PIPE STDIN (?)
+            if (typeof stdin === 'string') {
+                p.stdin.setDefaultEncoding('utf-8');
+                if (!stdin.endsWith('\n'))
+                    stdin += '\n';
+                p.stdin.write(stdin);
+                p.stdin.end();
+            }
+            else if (typeof stdin !== 'undefined') {
+                stdin.pipe(p.stdin);
+            }
             // PIPE STDOUT
             if (typeof stdout === 'function') {
-                (_a = p.stdout) === null || _a === void 0 ? void 0 : _a.on("data", function (chunk) {
-                    stdoutbuf_1 += chunk;
+                p.stdout.on('data', function (chunk) {
+                    stdoutbuf_2 += chunk;
                     var i = -1;
-                    while ((i = stdoutbuf_1.indexOf('\n')) >= 0) {
-                        var line = stdoutbuf_1.substring(0, i);
-                        stdoutbuf_1 = stdoutbuf_1.substring(i + 1);
+                    while ((i = stdoutbuf_2.indexOf('\n')) >= 0) {
+                        var line = stdoutbuf_2.substring(0, i);
+                        stdoutbuf_2 = stdoutbuf_2.substring(i + 1);
                         if (typeof stdout === 'function') {
                             stdout(line);
                         }
@@ -34,16 +108,16 @@ function exec(command, stdout, stderr) {
                 });
             }
             else if (typeof stdout !== 'undefined') {
-                (_b = p.stdout) === null || _b === void 0 ? void 0 : _b.pipe(stdout);
+                p.stdout.pipe(stdout);
             }
             // PIPE STDERR
             if (typeof stderr === 'function') {
-                (_c = p.stderr) === null || _c === void 0 ? void 0 : _c.on("data", function (chunk) {
-                    stderrbuf_1 += chunk;
+                p.stderr.on('data', function (chunk) {
+                    stderrbuf_2 += chunk;
                     var i = -1;
-                    while ((i = stderrbuf_1.indexOf('\n')) >= 0) {
-                        var line = stderrbuf_1.substring(0, i);
-                        stderrbuf_1 = stderrbuf_1.substring(i + 1);
+                    while ((i = stderrbuf_2.indexOf('\n')) >= 0) {
+                        var line = stderrbuf_2.substring(0, i);
+                        stderrbuf_2 = stderrbuf_2.substring(i + 1);
                         if (typeof stderr === 'function') {
                             stderr(line);
                         }
@@ -51,23 +125,8 @@ function exec(command, stdout, stderr) {
                 });
             }
             else if (typeof stderr !== 'undefined') {
-                (_d = p.stderr) === null || _d === void 0 ? void 0 : _d.pipe(stderr);
-            }
-        }
-        catch (err) {
-            reject(err);
-        }
-    });
-}
-exports.exec = exec;
-function spawn(command, args, stdout, stderr) {
-    return new Promise(function (resolve, reject) {
-        try {
-            var p = (0, child_process_1.spawn)(command, args);
-            if (stdout)
-                p.stdout.pipe(stdout);
-            if (stderr)
                 p.stderr.pipe(stderr);
+            }
             p.on('close', function (code, sig) {
                 if (!code)
                     resolve();

File diff suppressed because it is too large
+ 0 - 0
shell/index.js.map


+ 136 - 65
shell/index.ts

@@ -2,80 +2,151 @@ import { exec as shell_exec, spawn as shell_spawn } from 'child_process';
 
 const MAX_BUFFER = 10 * Math.pow(2, 20);
 
-export function exec(command: string, stdout?: ((...args: any[]) => void) | NodeJS.WriteStream, stderr?: ((...args: any[]) => void) | NodeJS.WriteStream) {
-    return new Promise<string>((resolve, reject) => {
-        try {
-            let stdoutbuf = "";
-            let stderrbuf = "";
+export function exec(
+  command: string,
+  stdout?: ((...args: any[]) => void) | NodeJS.WriteStream,
+  stderr?: ((...args: any[]) => void) | NodeJS.WriteStream,
+  stdin?: NodeJS.ReadableStream | string
+) {
+  return new Promise<string>((resolve, reject) => {
+    try {
+      let stdoutbuf = '';
+      let stderrbuf = '';
 
-            // EXEC CHILD PROCESS
-            const p = shell_exec(command, { maxBuffer: MAX_BUFFER }, (err, out) => {
-                if (err) return reject(err);
+      // EXEC CHILD PROCESS
+      const p = shell_exec(command, { maxBuffer: MAX_BUFFER }, (err, out) => {
+        if (stdoutbuf.length > 0 && typeof stdout === 'function') stdout(stdoutbuf);
+        if (stderrbuf.length > 0 && typeof stderr === 'function') stderr(stderrbuf);
 
-                if (stdoutbuf.length > 0 && typeof stdout === 'function') stdout(stdoutbuf);
-                if (stderrbuf.length > 0 && typeof stderr === 'function') stderr(stderrbuf);
+        if (err) return reject(err);
 
-                resolve(out);
-            });
+        resolve(out);
+      });
 
-            // PIPE STDOUT
-            if (typeof stdout === 'function') {
-                p.stdout?.on("data", chunk => {
-                    stdoutbuf += chunk;
-                    let i = -1;
-                    while ((i = stdoutbuf.indexOf('\n')) >= 0) {
-                        const line = stdoutbuf.substring(0, i);
-                        stdoutbuf = stdoutbuf.substring(i + 1);
-                        if (typeof stdout === 'function') {
-                            stdout(line);
-                        }
-                    }
-                });
-            } else if (typeof stdout !== 'undefined') {
-                p.stdout?.pipe(stdout);
+      // PIPE STDIN (?)
+      if (p.stdin) {
+        if (typeof stdin === 'string') {
+          p.stdin.setDefaultEncoding('utf-8');
+          if (!stdin.endsWith('\n')) stdin += '\n';
+          p.stdin.write(stdin);
+          p.stdin.end();
+        } else if (typeof stdin !== 'undefined') {
+          stdin.pipe(p.stdin);
+        }
+      }
+
+      // PIPE STDOUT
+      if (p.stdout) {
+        if (typeof stdout === 'function') {
+          p.stdout.on('data', (chunk) => {
+            stdoutbuf += chunk;
+            let i = -1;
+            while ((i = stdoutbuf.indexOf('\n')) >= 0) {
+              const line = stdoutbuf.substring(0, i);
+              stdoutbuf = stdoutbuf.substring(i + 1);
+              if (typeof stdout === 'function') {
+                stdout(line);
+              }
             }
+          });
+        } else if (typeof stdout !== 'undefined') {
+          p.stdout.pipe(stdout);
+        }
+      }
 
-            // PIPE STDERR
-            if (typeof stderr === 'function') {
-                p.stderr?.on("data", chunk => {
-                    stderrbuf += chunk;
-                    let i = -1;
-                    while ((i = stderrbuf.indexOf('\n')) >= 0) {
-                        const line = stderrbuf.substring(0, i);
-                        stderrbuf = stderrbuf.substring(i + 1);
-                        if (typeof stderr === 'function') {
-                            stderr(line);
-                        }
-                    }
-                });
-            } else if (typeof stderr !== 'undefined') {
-                p.stderr?.pipe(stderr);
+      // PIPE STDERR
+      if (p.stderr) {
+        if (typeof stderr === 'function') {
+          p.stderr.on('data', (chunk) => {
+            stderrbuf += chunk;
+            let i = -1;
+            while ((i = stderrbuf.indexOf('\n')) >= 0) {
+              const line = stderrbuf.substring(0, i);
+              stderrbuf = stderrbuf.substring(i + 1);
+              if (typeof stderr === 'function') {
+                stderr(line);
+              }
             }
-        } catch (err) {
-            reject(err);
+          });
+        } else if (typeof stderr !== 'undefined') {
+          p.stderr.pipe(stderr);
         }
-    });
+      }
+    } catch (err) {
+      reject(err);
+    }
+  });
 }
 
-export function spawn(command: string, args: string[], stdout?: NodeJS.WriteStream, stderr?: NodeJS.WriteStream) {
-    return new Promise<void>((resolve, reject) => {
-        try {
-            const p = shell_spawn(command, args);
+export function spawn(
+  command: string,
+  args: string[],
+  stdout?: ((...args: any[]) => void) | NodeJS.WriteStream,
+  stderr?: ((...args: any[]) => void) | NodeJS.WriteStream,
+  stdin?: NodeJS.ReadableStream | string
+) {
+  return new Promise<void>((resolve, reject) => {
+    try {
+      let stdoutbuf = '';
+      let stderrbuf = '';
 
-            if (stdout) p.stdout.pipe(stdout);
-            if (stderr) p.stderr.pipe(stderr);
+      const p = shell_spawn(command, args);
 
-            p.on('close', (code, sig) => {
-                if (!code) resolve();
-                else reject();
-            });
-            p.on('error', reject);
-            p.on('exit', (code, sig) => {
-                if (!code) resolve();
-                else reject();
-            });
-        } catch (err) {
-            reject(err);
-        }
-    });
-}
+      // PIPE STDIN (?)
+      if (typeof stdin === 'string') {
+        p.stdin.setDefaultEncoding('utf-8');
+        if (!stdin.endsWith('\n')) stdin += '\n';
+        p.stdin.write(stdin);
+        p.stdin.end();
+      } else if (typeof stdin !== 'undefined') {
+        stdin.pipe(p.stdin);
+      }
+
+      // PIPE STDOUT
+      if (typeof stdout === 'function') {
+        p.stdout.on('data', (chunk) => {
+          stdoutbuf += chunk;
+          let i = -1;
+          while ((i = stdoutbuf.indexOf('\n')) >= 0) {
+            const line = stdoutbuf.substring(0, i);
+            stdoutbuf = stdoutbuf.substring(i + 1);
+            if (typeof stdout === 'function') {
+              stdout(line);
+            }
+          }
+        });
+      } else if (typeof stdout !== 'undefined') {
+        p.stdout.pipe(stdout);
+      }
+
+      // PIPE STDERR
+      if (typeof stderr === 'function') {
+        p.stderr.on('data', (chunk) => {
+          stderrbuf += chunk;
+          let i = -1;
+          while ((i = stderrbuf.indexOf('\n')) >= 0) {
+            const line = stderrbuf.substring(0, i);
+            stderrbuf = stderrbuf.substring(i + 1);
+            if (typeof stderr === 'function') {
+              stderr(line);
+            }
+          }
+        });
+      } else if (typeof stderr !== 'undefined') {
+        p.stderr.pipe(stderr);
+      }
+
+      p.on('close', (code, sig) => {
+        if (!code) resolve();
+        else reject();
+      });
+      p.on('error', reject);
+      p.on('exit', (code, sig) => {
+        if (!code) resolve();
+        else reject();
+      });
+    } catch (err) {
+      reject(err);
+    }
+  });
+}

Some files were not shown because too many files changed in this diff