浏览代码

Initial Commit; shell utils: exec/spawn

Christian Kahlau 4 年之前
当前提交
ec6f12e84a
共有 15 个文件被更改,包括 234 次插入0 次删除
  1. 2 0
      .gitignore
  2. 7 0
      .prettierrc.js
  3. 4 0
      .vscode/settings.json
  4. 3 0
      index.d.ts
  5. 1 0
      index.d.ts.map
  6. 10 0
      index.js
  7. 1 0
      index.js.map
  8. 6 0
      index.ts
  9. 19 0
      package.json
  10. 4 0
      shell/index.d.ts
  11. 1 0
      shell/index.d.ts.map
  12. 84 0
      shell/index.js
  13. 0 0
      shell/index.js.map
  14. 77 0
      shell/index.ts
  15. 15 0
      tsconfig.json

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+node_modules
+package-lock.json

+ 7 - 0
.prettierrc.js

@@ -0,0 +1,7 @@
+module.exports = {
+  jsxSingleQuote: true,
+  singleQuote: true,
+  printWidth: 150,
+  tabWidth: 2,
+  trailingComma: 'none'
+};

+ 4 - 0
.vscode/settings.json

@@ -0,0 +1,4 @@
+{
+    "editor.formatOnSave": true,
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
+}

+ 3 - 0
index.d.ts

@@ -0,0 +1,3 @@
+export declare namespace NodeUtils {
+}
+//# sourceMappingURL=index.d.ts.map

+ 1 - 0
index.d.ts.map

@@ -0,0 +1 @@
+{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAEA,yBAAiB,SAAS,CAAC;CAG1B"}

+ 10 - 0
index.js

@@ -0,0 +1,10 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.NodeUtils = void 0;
+var shell_1 = require("./shell");
+var NodeUtils;
+(function (NodeUtils) {
+    shell_1.exec;
+    shell_1.spawn;
+})(NodeUtils = exports.NodeUtils || (exports.NodeUtils = {}));
+//# sourceMappingURL=index.js.map

+ 1 - 0
index.js.map

@@ -0,0 +1 @@
+{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,iCAAsC;AAEtC,IAAiB,SAAS,CAGzB;AAHD,WAAiB,SAAS;IACtB,YAAI,CAAC;IACL,aAAK,CAAC;AACV,CAAC,EAHgB,SAAS,GAAT,iBAAS,KAAT,iBAAS,QAGzB"}

+ 6 - 0
index.ts

@@ -0,0 +1,6 @@
+import { exec, spawn } from './shell';
+
+export namespace NodeUtils {
+    exec;
+    spawn;
+}

+ 19 - 0
package.json

@@ -0,0 +1,19 @@
+{
+  "name": "node-utils",
+  "version": "1.0.0",
+  "description": "Shared common library for JavaScript in NodeJS",
+  "repository": {
+    "type": "git",
+    "url": "https://bitbucket.siriusonline.de/scm/npm/node-utils.git"
+  },
+  "main": "index.js",
+  "scripts": {
+    "build": "tsc"
+  },
+  "author": "Christian Kahlau, sirius-net GmbH",
+  "license": "ISC",
+  "devDependencies": {
+    "@types/node": "^14.14.31",
+    "typescript": "^4.2.2"
+  }
+}

+ 4 - 0
shell/index.d.ts

@@ -0,0 +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>;
+//# sourceMappingURL=index.d.ts.map

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

@@ -0,0 +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"}

+ 84 - 0
shell/index.js

@@ -0,0 +1,84 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.spawn = exports.exec = void 0;
+var child_process_1 = require("child_process");
+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);
+                    }
+                }
+            });
+        }
+        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);
+                    }
+                }
+            });
+        }
+        else if (typeof stderr !== 'undefined') {
+            p.stderr.pipe(stderr);
+        }
+    });
+}
+exports.exec = exec;
+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);
+            p.on('close', function (code, sig) {
+                if (!code)
+                    resolve();
+                else
+                    reject();
+            });
+            p.on('error', reject);
+            p.on('exit', function (code, sig) {
+                if (!code)
+                    resolve();
+                else
+                    reject();
+            });
+        }
+        catch (err) {
+            console.error(err);
+        }
+    });
+}
+exports.spawn = spawn;
+//# sourceMappingURL=index.js.map

文件差异内容过多而无法显示
+ 0 - 0
shell/index.js.map


+ 77 - 0
shell/index.ts

@@ -0,0 +1,77 @@
+import { ChildProcessWithoutNullStreams, 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 = "";
+
+        // 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);
+
+            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 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);
+        }
+    });
+}
+
+export function spawn(command, args, stdout, stderr) {
+    return new Promise<void>((resolve, reject) => {
+        try {
+            const p = shell_spawn(command, args);
+
+            p.stdout.pipe(stdout);
+            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) {
+            console.error(err);
+        }
+    });
+}

+ 15 - 0
tsconfig.json

@@ -0,0 +1,15 @@
+{
+  "compilerOptions": {
+    "lib": ["ES2020"],
+    "target": "es5",
+    "module": "commonjs",
+    "declaration": true,
+    "declarationMap": true,
+    "sourceMap": true,
+    "rootDir": "./",
+    "strict": false,
+    "esModuleInterop": true,
+    "skipLibCheck": true,
+    "forceConsistentCasingInFileNames": true
+  }
+}

部分文件因为文件数量过多而无法显示