Parcourir la source

type declarations at shell/spawn

Christian Kahlau il y a 4 ans
Parent
commit
569390a99e
6 fichiers modifiés avec 103 ajouts et 91 suppressions
  1. 1 1
      package.json
  2. 1 1
      shell/index.d.ts
  3. 1 1
      shell/index.d.ts.map
  4. 52 45
      shell/index.js
  5. 0 0
      shell/index.js.map
  6. 48 43
      shell/index.ts

+ 1 - 1
package.json

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

+ 1 - 1
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: any, args: any, stdout: any, stderr: any): Promise<void>;
+export declare function spawn(command: string, args: string[], stdout?: NodeJS.WriteStream, stderr?: NodeJS.WriteStream): 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":";AAKA,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,GAAG,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,GAAG,MAAM,CAAC,WAAW,mBAiDnI;AAED,wBAAgB,KAAK,CAAC,OAAO,KAAA,EAAE,IAAI,KAAA,EAAE,MAAM,KAAA,EAAE,MAAM,KAAA,iBAoBlD"}
+{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAKA,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,GAAG,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,OAAA,KAAK,IAAI,GAAG,MAAM,CAAC,WAAW,mBAqDnI;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"}

+ 52 - 45
shell/index.js

@@ -6,51 +6,56 @@ var child_process_2 = require("child_process");
 var MAX_BUFFER = 10 * Math.pow(2, 20);
 function exec(command, stdout, stderr) {
     return new Promise(function (resolve, reject) {
-        var stdoutbuf = "";
-        var stderrbuf = "";
-        // EXEC CHILD PROCESS
-        var p = child_process_1.exec(command, { maxBuffer: MAX_BUFFER }, function (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);
-            resolve(out);
-        });
-        // PIPE STDOUT
-        if (typeof stdout === 'function') {
-            p.stdout.on("data", function (chunk) {
-                stdoutbuf += chunk;
-                var i = -1;
-                while ((i = stdoutbuf.indexOf('\n')) >= 0) {
-                    var line = stdoutbuf.substring(0, i);
-                    stdoutbuf = stdoutbuf.substring(i + 1);
-                    if (typeof stdout === 'function') {
-                        stdout(line);
-                    }
-                }
+        try {
+            var stdoutbuf_1 = "";
+            var stderrbuf_1 = "";
+            // EXEC CHILD PROCESS
+            var p = 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);
+                resolve(out);
             });
-        }
-        else if (typeof stdout !== 'undefined') {
-            p.stdout.pipe(stdout);
-        }
-        // PIPE STDERR
-        if (typeof stderr === 'function') {
-            p.stderr.on("data", function (chunk) {
-                stderrbuf += chunk;
-                var i = -1;
-                while ((i = stderrbuf.indexOf('\n')) >= 0) {
-                    var line = stderrbuf.substring(0, i);
-                    stderrbuf = stderrbuf.substring(i + 1);
-                    if (typeof stderr === 'function') {
-                        stderr(line);
+            // PIPE 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 (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);
+            }
         }
-        else if (typeof stderr !== 'undefined') {
-            p.stderr.pipe(stderr);
+        catch (err) {
+            reject(err);
         }
     });
 }
@@ -59,8 +64,10 @@ function spawn(command, args, stdout, stderr) {
     return new Promise(function (resolve, reject) {
         try {
             var p = child_process_2.spawn(command, args);
-            p.stdout.pipe(stdout);
-            p.stderr.pipe(stderr);
+            if (stdout)
+                p.stdout.pipe(stdout);
+            if (stderr)
+                p.stderr.pipe(stderr);
             p.on('close', function (code, sig) {
                 if (!code)
                     resolve();
@@ -76,7 +83,7 @@ function spawn(command, args, stdout, stderr) {
             });
         }
         catch (err) {
-            console.error(err);
+            reject(err);
         }
     });
 }

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
shell/index.js.map


+ 48 - 43
shell/index.ts

@@ -1,66 +1,71 @@
-import { ChildProcessWithoutNullStreams, exec as shell_exec } from 'child_process';
+import { exec as shell_exec } from 'child_process';
 import { 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) => {
-        let stdoutbuf = "";
-        let stderrbuf = "";
+        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
-        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);
+            // 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);
-        }
+                });
+            } 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);
+            // 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);
+                });
+            } else if (typeof stderr !== 'undefined') {
+                p.stderr.pipe(stderr);
+            }
+        } catch (err) {
+            reject(err);
         }
     });
 }
 
-export function spawn(command, args, stdout, stderr) {
+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);
 
-            p.stdout.pipe(stdout);
-            p.stderr.pipe(stderr);
+            if (stdout) p.stdout.pipe(stdout);
+            if (stderr) p.stderr.pipe(stderr);
+
             p.on('close', (code, sig) => {
                 if (!code) resolve();
                 else reject();
@@ -71,7 +76,7 @@ export function spawn(command, args, stdout, stderr) {
                 else reject();
             });
         } catch (err) {
-            console.error(err);
+            reject(err);
         }
     });
 }

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff