Browse Source

Abhändigkeiten aus node-utils direkt ins script integriert; node-utils entfernt

Christian Kahlau 3 years ago
parent
commit
1c8f16dcf3
3 changed files with 175 additions and 17 deletions
  1. 0 1
      package.json
  2. 93 11
      scripts/init.js
  3. 82 5
      scripts/init.ts

+ 0 - 1
package.json

@@ -22,7 +22,6 @@
   },
   "devDependencies": {
     "@types/express": "^4.17.13",
-    "node-utils": "git+https://bitbucket.siriusonline.de/scm/npm/node-utils.git#1.0.5",
     "typescript": "^4.6.4"
   }
 }

+ 93 - 11
scripts/init.js

@@ -12,35 +12,117 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
     return (mod && mod.__esModule) ? mod : { "default": mod };
 };
 Object.defineProperty(exports, "__esModule", { value: true });
+const child_process_1 = require("child_process");
 const fs_1 = __importDefault(require("fs"));
-const shell_1 = require("node-utils/shell");
-const file_1 = require("node-utils/file");
 const path_1 = __importDefault(require("path"));
 const util_1 = __importDefault(require("util"));
+const MAX_BUFFER = 10 * Math.pow(2, 20);
+function exec(command, stdout, stderr) {
+    return new Promise((resolve, reject) => {
+        try {
+            let stdoutbuf = '';
+            let stderrbuf = '';
+            // EXEC CHILD PROCESS
+            const p = child_process_1.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);
+            }
+        }
+        catch (err) {
+            reject(err);
+        }
+    });
+}
+exports.exec = exec;
 const cli = {
     blue: v => `\x1b[34m${v}\x1b[0m`,
     green: v => `\x1b[32m${v}\x1b[0m`,
     yellow: v => `\x1b[33m${v}\x1b[0m`,
     red: v => `\x1b[31m${v}\x1b[0m`
 };
+function readFile(path, encoding = 'utf8') {
+    return __awaiter(this, void 0, void 0, function* () {
+        return new Promise((resolve, reject) => {
+            fs_1.default.readFile(path, { encoding }, (err, data) => {
+                if (err)
+                    return reject(err);
+                try {
+                    resolve(data.toString());
+                }
+                catch (e) {
+                    reject(e);
+                }
+            });
+        });
+    });
+}
+function readJsonFile(path, encoding = 'utf8') {
+    return __awaiter(this, void 0, void 0, function* () {
+        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
+            try {
+                resolve(JSON.parse(yield readFile(path, encoding)));
+            }
+            catch (e) {
+                reject(e);
+            }
+        }));
+    });
+}
 (() => __awaiter(void 0, void 0, void 0, function* () {
     try {
         const projectRootDir = path_1.default.resolve(process.cwd());
         if (!fs_1.default.existsSync(path_1.default.resolve(projectRootDir, 'package.json'))) {
-            yield shell_1.exec('npm init -y', process.stdout, process.stderr);
+            yield exec('npm init -y', process.stdout, process.stderr);
         }
         const cloneDir = path_1.default.resolve(projectRootDir, '.clone');
         if (fs_1.default.existsSync(cloneDir)) {
             yield util_1.default.promisify(fs_1.default.rm)(cloneDir, { force: true, recursive: true });
         }
-        yield shell_1.exec(`git clone ssh://git@bitbucket.siriusonline.de:7999/tsc/express-starter.git "${cloneDir}"`, process.stdout, process.stderr);
-        const pkgJson = yield file_1.readJsonFile(path_1.default.resolve(cloneDir, 'package.json'));
+        yield exec(`git clone ssh://git@bitbucket.siriusonline.de:7999/tsc/express-starter.git "${cloneDir}"`, process.stdout, process.stderr);
+        const pkgJson = yield readJsonFile(path_1.default.resolve(cloneDir, 'package.json'));
         let dependencies = Object.keys(pkgJson.dependencies).join('" "');
-        yield shell_1.exec(`npm install "${dependencies}"`, process.stdout, process.stderr);
-        dependencies = Object.keys(pkgJson.devDependencies)
-            .filter(d => d !== 'node-utils')
-            .join('" "');
-        yield shell_1.exec(`npm install --save-dev "${dependencies}"`, process.stdout, process.stderr);
+        yield exec(`npm install "${dependencies}"`, process.stdout, process.stderr);
+        dependencies = Object.keys(pkgJson.devDependencies).join('" "');
+        yield exec(`npm install --save-dev "${dependencies}"`, process.stdout, process.stderr);
         const copyFiles = ['.env', '.gitignore', '.prettierrc.js', 'tsconfig.json', '.vscode/settings.json', 'src'];
         for (const cpFile of copyFiles) {
             const sourceFile = path_1.default.resolve(cloneDir, cpFile);
@@ -55,7 +137,7 @@ const cli = {
                 yield util_1.default.promisify(fs_1.default.copyFile)(sourceFile, targetFile);
             }
             else {
-                yield shell_1.exec(`cp -r "${sourceFile}" "${targetFile}"`);
+                yield exec(`cp -r "${sourceFile}" "${targetFile}"`);
             }
         }
         yield util_1.default.promisify(fs_1.default.rm)(cloneDir, { force: true, recursive: true });

+ 82 - 5
scripts/init.ts

@@ -1,9 +1,65 @@
+import { exec as shell_exec } from 'child_process';
 import fs from 'fs';
-import { exec } from 'node-utils/shell';
-import { readJsonFile } from 'node-utils/file';
 import path from 'path';
 import util from 'util';
 
+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 = '';
+
+      // 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);
+      }
+    } catch (err) {
+      reject(err);
+    }
+  });
+}
+
 const cli = {
   blue: v => `\x1b[34m${v}\x1b[0m`,
   green: v => `\x1b[32m${v}\x1b[0m`,
@@ -11,6 +67,29 @@ const cli = {
   red: v => `\x1b[31m${v}\x1b[0m`
 };
 
+async function readFile(path: string, encoding: BufferEncoding = 'utf8'): Promise<string> {
+  return new Promise<string>((resolve, reject) => {
+    fs.readFile(path, { encoding }, (err, data) => {
+      if (err) return reject(err);
+      try {
+        resolve(data.toString());
+      } catch (e) {
+        reject(e);
+      }
+    });
+  });
+}
+
+async function readJsonFile<T>(path: string, encoding: BufferEncoding = 'utf8'): Promise<T> {
+  return new Promise<T>(async (resolve, reject) => {
+    try {
+      resolve(JSON.parse(await readFile(path, encoding)) as T);
+    } catch (e) {
+      reject(e);
+    }
+  });
+}
+
 (async () => {
   try {
     const projectRootDir = path.resolve(process.cwd());
@@ -30,9 +109,7 @@ const cli = {
 
     let dependencies = Object.keys(pkgJson.dependencies).join('" "');
     await exec(`npm install "${dependencies}"`, process.stdout, process.stderr);
-    dependencies = Object.keys(pkgJson.devDependencies)
-      .filter(d => d !== 'node-utils')
-      .join('" "');
+    dependencies = Object.keys(pkgJson.devDependencies).join('" "');
     await exec(`npm install --save-dev "${dependencies}"`, process.stdout, process.stderr);
 
     const copyFiles = ['.env', '.gitignore', '.prettierrc.js', 'tsconfig.json', '.vscode/settings.json', 'src'];