Pārlūkot izejas kodu

bin script refactoring

sanex3339 9 gadi atpakaļ
vecāks
revīzija
97430e779b
3 mainītis faili ar 56 papildinājumiem un 52 dzēšanām
  1. 21 11
      dist/index.js
  2. 25 14
      src/cli/JavaScriptObfuscatorCLI.ts
  3. 10 27
      test/JavaScriptObfuscatorCLI.spec.ts

+ 21 - 11
dist/index.js

@@ -1083,7 +1083,6 @@ module.exports =
 	        this.data = '';
 	        this.data = '';
 	        this.rawArguments = argv;
 	        this.rawArguments = argv;
 	        this.arguments = this.rawArguments.slice(2);
 	        this.arguments = this.rawArguments.slice(2);
-	        this.inputFilePath = this.arguments[0];
 	    }
 	    }
 	
 	
 	    _createClass(JavaScriptObfuscatorCLI, [{
 	    _createClass(JavaScriptObfuscatorCLI, [{
@@ -1094,7 +1093,7 @@ module.exports =
 	                commands.outputHelp();
 	                commands.outputHelp();
 	                return;
 	                return;
 	            }
 	            }
-	            JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath);
+	            this.inputPath = this.getInputPath();
 	            this.getData();
 	            this.getData();
 	            this.processData();
 	            this.processData();
 	        }
 	        }
@@ -1117,7 +1116,16 @@ module.exports =
 	    }, {
 	    }, {
 	        key: 'getData',
 	        key: 'getData',
 	        value: function getData() {
 	        value: function getData() {
-	            this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding);
+	            this.data = fs.readFileSync(this.inputPath, JavaScriptObfuscatorCLI.encoding);
+	        }
+	    }, {
+	        key: 'getInputPath',
+	        value: function getInputPath() {
+	            var inputPath = this.arguments[0];
+	            if (!JavaScriptObfuscatorCLI.isFilePath(inputPath)) {
+	                throw new ReferenceError('First argument must be a valid file path');
+	            }
+	            return inputPath;
 	        }
 	        }
 	    }, {
 	    }, {
 	        key: 'getOutputPath',
 	        key: 'getOutputPath',
@@ -1126,7 +1134,7 @@ module.exports =
 	            if (outputPath) {
 	            if (outputPath) {
 	                return outputPath;
 	                return outputPath;
 	            }
 	            }
-	            return this.inputFilePath.split('.').map(function (value, index) {
+	            return this.inputPath.split('.').map(function (value, index) {
 	                return index === 0 ? value + '-obfuscated' : value;
 	                return index === 0 ? value + '-obfuscated' : value;
 	            }).join('.');
 	            }).join('.');
 	        }
 	        }
@@ -1153,13 +1161,6 @@ module.exports =
 	            }
 	            }
 	            return Object.assign({}, DefaultPreset_1.DEFAULT_PRESET, options);
 	            return Object.assign({}, DefaultPreset_1.DEFAULT_PRESET, options);
 	        }
 	        }
-	    }, {
-	        key: 'checkFilePath',
-	        value: function checkFilePath(filePath) {
-	            if (!fs.existsSync(filePath)) {
-	                throw new Error('Wrong input file `' + filePath + '`');
-	            }
-	        }
 	    }, {
 	    }, {
 	        key: 'getBuildVersion',
 	        key: 'getBuildVersion',
 	        value: function getBuildVersion() {
 	        value: function getBuildVersion() {
@@ -1167,6 +1168,15 @@ module.exports =
 	                encoding: JavaScriptObfuscatorCLI.encoding
 	                encoding: JavaScriptObfuscatorCLI.encoding
 	            });
 	            });
 	        }
 	        }
+	    }, {
+	        key: 'isFilePath',
+	        value: function isFilePath(filePath) {
+	            try {
+	                return fs.statSync(filePath).isFile();
+	            } catch (e) {
+	                return false;
+	            }
+	        }
 	    }, {
 	    }, {
 	        key: 'parseBoolean',
 	        key: 'parseBoolean',
 	        value: function parseBoolean(value) {
 	        value: function parseBoolean(value) {

+ 25 - 14
src/cli/JavaScriptObfuscatorCLI.ts

@@ -37,12 +37,11 @@ export class JavaScriptObfuscatorCLI {
     /**
     /**
      * @type {string}
      * @type {string}
      */
      */
-    private inputFilePath: string;
+    private inputPath: string;
 
 
     constructor (argv: string[]) {
     constructor (argv: string[]) {
         this.rawArguments = argv;
         this.rawArguments = argv;
         this.arguments = this.rawArguments.slice(2);
         this.arguments = this.rawArguments.slice(2);
-        this.inputFilePath = this.arguments[0];
     }
     }
 
 
     /**
     /**
@@ -67,15 +66,6 @@ export class JavaScriptObfuscatorCLI {
         return Object.assign({}, DEFAULT_PRESET, options);
         return Object.assign({}, DEFAULT_PRESET, options);
     }
     }
 
 
-    /**
-     * @param filePath
-     */
-    private static checkFilePath (filePath: string): void {
-        if (!fs.existsSync(filePath)) {
-            throw new Error(`Wrong input file \`${filePath}\``);
-        }
-    }
-
     /**
     /**
      * @returns {string}
      * @returns {string}
      */
      */
@@ -85,6 +75,17 @@ export class JavaScriptObfuscatorCLI {
         });
         });
     }
     }
 
 
+    /**
+     * @param filePath
+     */
+    private static isFilePath (filePath: string): boolean {
+        try {
+            return fs.statSync(filePath).isFile();
+        } catch (e) {
+            return false;
+        }
+    }
+
     /**
     /**
      * @param value
      * @param value
      * @returns {boolean}
      * @returns {boolean}
@@ -102,7 +103,7 @@ export class JavaScriptObfuscatorCLI {
             return;
             return;
         }
         }
 
 
-        JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath);
+        this.inputPath = this.getInputPath();
 
 
         this.getData();
         this.getData();
         this.processData();
         this.processData();
@@ -140,7 +141,17 @@ export class JavaScriptObfuscatorCLI {
     }
     }
 
 
     private getData (): void {
     private getData (): void {
-        this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding);
+        this.data = fs.readFileSync(this.inputPath, JavaScriptObfuscatorCLI.encoding);
+    }
+
+    private getInputPath (): string {
+        let inputPath: string = this.arguments[0];
+
+        if (!JavaScriptObfuscatorCLI.isFilePath(inputPath)) {
+            throw new ReferenceError(`First argument must be a valid file path`);
+        }
+
+        return inputPath;
     }
     }
 
 
     /**
     /**
@@ -153,7 +164,7 @@ export class JavaScriptObfuscatorCLI {
             return outputPath;
             return outputPath;
         }
         }
 
 
-        return this.inputFilePath
+        return this.inputPath
             .split('.')
             .split('.')
             .map<string>((value: string, index: number) => {
             .map<string>((value: string, index: number) => {
                 return index === 0 ? `${value}-obfuscated` : value;
                 return index === 0 ? `${value}-obfuscated` : value;

+ 10 - 27
test/JavaScriptObfuscatorCLI.spec.ts

@@ -1,35 +1,18 @@
-import * as child_process from 'child_process';
-
 import { JavaScriptObfuscatorCLI } from "../src/cli/JavaScriptObfuscatorCLI";
 import { JavaScriptObfuscatorCLI } from "../src/cli/JavaScriptObfuscatorCLI";
 
 
-let assert: any = require('chai').assert,
-    stream: any = require("mock-utf8-stream"),
-    mockStdin: any = require('mock-stdin');
+let assert: any = require('chai').assert;
 
 
 describe('JavaScriptObfuscatorCLI', () => {
 describe('JavaScriptObfuscatorCLI', () => {
     describe('run (): void', () => {
     describe('run (): void', () => {
-        it('should obfuscate file with JS code', function (done: MochaDone): void {
-            let stdin: any = mockStdin.stdin(),
-                stdout: any = new stream.MockWritableStream();
-
-            this.timeout(7000);
-
-            stdin.send('test/dev/test.js');
-
-            stdout.startCapture();
-
-            let CLI: JavaScriptObfuscatorCLI = new JavaScriptObfuscatorCLI(
-                [
-                    'node',
-                    'javascript-obfuscator',
-                    '--compact',
-                    'false',
-                    '--selfDefending',
-                    'false'
-                ],
-                stdin,
-                stdout
-            );
+        it('should obfuscate file with JS code', () => {
+            let CLI: JavaScriptObfuscatorCLI = new JavaScriptObfuscatorCLI([
+                'node',
+                'javascript-obfuscator',
+                '--compact',
+                'false',
+                '--selfDefending',
+                'false'
+            ]);
 
 
             CLI.run();
             CLI.run();