Эх сурвалжийг харах

shell/exec & shell/spawn: Pipe STDIN ermöglicht

Christian Kahlau 3 жил өмнө
parent
commit
3e1944552c

+ 1 - 0
file/index.d.ts

@@ -1,4 +1,5 @@
 /// <reference types="node" />
+/// <reference types="node" />
 import fs from 'fs';
 export declare function readFile(path: string, encoding?: BufferEncoding): Promise<string>;
 export declare function readJsonFile<T>(path: string, encoding?: BufferEncoding): Promise<T>;

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

@@ -1 +1 @@
-{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,cAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,CAW/F;AAED,wBAAsB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,cAAuB,GAAG,OAAO,CAAC,CAAC,CAAC,CAUjG;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAOzD;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBlG;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAO7D;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,CAoBvE"}
+{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,cAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,CAW/F;AAED,wBAAsB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,cAAuB,GAAG,OAAO,CAAC,CAAC,CAAC,CAUjG;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAOzD;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBlG;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAO7D;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,CAoBvE"}

+ 2 - 2
package.json

@@ -1,6 +1,6 @@
 {
   "name": "node-utils",
-  "version": "1.0.5",
+  "version": "1.0.6",
   "description": "Shared common library for JavaScript in NodeJS",
   "repository": {
     "type": "git",
@@ -16,4 +16,4 @@
     "@types/node": "^14.14.31",
     "typescript": "^4.2.2"
   }
-}
+}

+ 2 - 2
shell/index.d.ts

@@ -1,4 +1,4 @@
 /// <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>;
+export declare function exec(command: string, stdout?: ((...args: any[]) => void) | NodeJS.WriteStream, stderr?: ((...args: any[]) => void) | NodeJS.WriteStream, stdin?: NodeJS.WriteStream | 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.WriteStream | 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,OAAA,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,mBAqDzI;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,OAAA,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACjD,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACjD,KAAK,CAAC,EAAE,MAAM,CAAC,WAAW,GAAG,MAAM,mBAgEpC;AAED,wBAAgB,KAAK,CACnB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACjD,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,EACjD,KAAK,CAAC,EAAE,MAAM,CAAC,WAAW,GAAG,MAAM,iBAkEpC"}

+ 64 - 10
shell/index.js

@@ -3,13 +3,13 @@ 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) {
         try {
-            var stdoutbuf_1 = "";
-            var stderrbuf_1 = "";
+            var stdoutbuf_1 = '';
+            var stderrbuf_1 = '';
             // EXEC CHILD PROCESS
-            var p = child_process_1.exec(command, { maxBuffer: MAX_BUFFER }, function (err, out) {
+            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')
@@ -18,9 +18,20 @@ function exec(command, stdout, stderr) {
                     stderr(stderrbuf_1);
                 resolve(out);
             });
+            // 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", function (chunk) {
+                p.stdout.on('data', function (chunk) {
                     stdoutbuf_1 += chunk;
                     var i = -1;
                     while ((i = stdoutbuf_1.indexOf('\n')) >= 0) {
@@ -37,7 +48,7 @@ function exec(command, stdout, stderr) {
             }
             // PIPE STDERR
             if (typeof stderr === 'function') {
-                p.stderr.on("data", function (chunk) {
+                p.stderr.on('data', function (chunk) {
                     stderrbuf_1 += chunk;
                     var i = -1;
                     while ((i = stderrbuf_1.indexOf('\n')) >= 0) {
@@ -59,14 +70,57 @@ function exec(command, stdout, stderr) {
     });
 }
 exports.exec = exec;
-function spawn(command, args, stdout, stderr) {
+function spawn(command, args, stdout, stderr, stdin) {
     return new Promise(function (resolve, reject) {
         try {
-            var p = child_process_1.spawn(command, args);
-            if (stdout)
+            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') {
+                p.stdout.on('data', function (chunk) {
+                    stdoutbuf_2 += chunk;
+                    var 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);
+                        }
+                    }
+                });
+            }
+            else if (typeof stdout !== 'undefined') {
                 p.stdout.pipe(stdout);
-            if (stderr)
+            }
+            // PIPE STDERR
+            if (typeof stderr === 'function') {
+                p.stderr.on('data', function (chunk) {
+                    stderrbuf_2 += chunk;
+                    var 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);
+                        }
+                    }
+                });
+            }
+            else if (typeof stderr !== 'undefined') {
                 p.stderr.pipe(stderr);
+            }
             p.on('close', function (code, sig) {
                 if (!code)
                     resolve();

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
shell/index.js.map


+ 129 - 64
shell/index.ts

@@ -2,80 +2,145 @@ 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) => void) | NodeJS.WriteStream, stderr?: ((...args) => void) | NodeJS.WriteStream) {
-    return new Promise<string>((resolve, reject) => {
-        try {
-            let stdoutbuf = "";
-            let stderrbuf = "";
+export function exec(
+  command: string,
+  stdout?: ((...args) => void) | NodeJS.WriteStream,
+  stderr?: ((...args) => void) | NodeJS.WriteStream,
+  stdin?: NodeJS.WriteStream | 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 (err) return reject(err);
 
-                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);
 
-                resolve(out);
-            });
+        resolve(out);
+      });
 
-            // PIPE STDOUT
+      // 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') {
-                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);
+              stdout(line);
             }
+          }
+        });
+      } else if (typeof stdout !== 'undefined') {
+        p.stdout.pipe(stdout);
+      }
 
-            // PIPE STDERR
+      // 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') {
-                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);
+              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) => void) | NodeJS.WriteStream,
+  stderr?: ((...args) => void) | NodeJS.WriteStream,
+  stdin?: NodeJS.WriteStream | string
+) {
+  return new Promise<void>((resolve, reject) => {
+    try {
+      let stdoutbuf = '';
+      let stderrbuf = '';
+
+      const p = shell_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);
+      }
 
-            if (stdout) p.stdout.pipe(stdout);
-            if (stderr) p.stderr.pipe(stderr);
+      // 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);
+      }
 
-            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 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);
+    }
+  });
+}

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно