Browse Source

bin script refactoring

sanex3339 9 years ago
parent
commit
963ce9d03e
4 changed files with 32 additions and 31 deletions
  1. 13 15
      dist/index.js
  2. 0 3
      package.json
  3. 13 11
      src/cli/JavaScriptObfuscatorCLI.ts
  4. 6 2
      test/JavaScriptObfuscatorCLI.spec.ts

+ 13 - 15
dist/index.js

@@ -1070,10 +1070,10 @@ module.exports =
 	
 	function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 	
-	var commands = __webpack_require__(42);
+	var commands = __webpack_require__(43);
+	var child_process_1 = __webpack_require__(42);
 	var DefaultPreset_1 = __webpack_require__(14);
 	var JavaScriptObfuscator_1 = __webpack_require__(9);
-	var child_process_1 = __webpack_require__(45);
 	
 	var JavaScriptObfuscatorCLI = function () {
 	    function JavaScriptObfuscatorCLI(argv, stdin, stdout) {
@@ -1101,14 +1101,11 @@ module.exports =
 	                return val.split(',');
 	            }).option('--rotateUnicodeArray <boolean>', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold <number>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls <boolean>', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean).parse(this.argv);
 	            commands.on('--help', function () {
-	                var isWindows = process.platform === 'win32';
+	                var isWindows = process.platform === 'win32',
+	                    commandName = isWindows ? 'type' : 'cat';
 	                console.log('  Examples:\n');
 	                console.log('    %> javascript-obfuscator < in.js > out.js');
-	                if (isWindows) {
-	                    console.log('    %> type in1.js in2.js | javascript-obfuscator > out.js');
-	                } else {
-	                    console.log('    %> cat in1.js in2.js | javascript-obfuscator > out.js');
-	                }
+	                console.log("    %> " + commandName + " in1.js in2.js | javascript-obfuscator > out.js");
 	                console.log('');
 	                process.exit();
 	            });
@@ -1118,7 +1115,7 @@ module.exports =
 	        value: function configureProcess() {
 	            var _this = this;
 	
-	            this.stdin.setEncoding('utf-8');
+	            this.stdin.setEncoding(JavaScriptObfuscatorCLI.encoding);
 	            this.stdin.on('readable', function () {
 	                var chunk = void 0;
 	                while (chunk = _this.stdin.read()) {
@@ -1158,7 +1155,9 @@ module.exports =
 	    }, {
 	        key: "getBuildVersion",
 	        value: function getBuildVersion() {
-	            return String(child_process_1.execSync("npm info " + JavaScriptObfuscatorCLI.packageName + " version"));
+	            return child_process_1.execSync("npm info " + JavaScriptObfuscatorCLI.packageName + " version", {
+	                encoding: JavaScriptObfuscatorCLI.encoding
+	            });
 	        }
 	    }, {
 	        key: "parseBoolean",
@@ -1170,6 +1169,7 @@ module.exports =
 	    return JavaScriptObfuscatorCLI;
 	}();
 	
+	JavaScriptObfuscatorCLI.encoding = 'utf8';
 	JavaScriptObfuscatorCLI.packageName = 'javascript-obfuscator';
 	exports.JavaScriptObfuscatorCLI = JavaScriptObfuscatorCLI;
 
@@ -2609,15 +2609,13 @@ module.exports =
 /* 42 */
 /***/ function(module, exports) {
 
-	module.exports = require("commander");
+	module.exports = require("child_process");
 
 /***/ },
-/* 43 */,
-/* 44 */,
-/* 45 */
+/* 43 */
 /***/ function(module, exports) {
 
-	module.exports = require("child_process");
+	module.exports = require("commander");
 
 /***/ }
 /******/ ]);

+ 0 - 3
package.json

@@ -33,9 +33,6 @@
     "istanbul": "^1.0.0-alpha.2",
     "lite-server": "^1.3.1",
     "mocha": "^2.5.3",
-    "mock-cli": "^0.1.2",
-    "mock-stdin": "^0.3.0",
-    "mock-utf8-stream": "^0.1.1",
     "ts-loader": "^0.8.2",
     "ts-node": "^0.9.1",
     "tslint": "^3.11.0",

+ 13 - 11
src/cli/JavaScriptObfuscatorCLI.ts

@@ -1,14 +1,19 @@
 import * as commands from 'commander';
 import * as tty from 'tty';
+import { execSync } from "child_process";
 
 import { IOptionsPreset } from "../interfaces/IOptionsPreset";
 
 import { DEFAULT_PRESET } from "../preset-options/DefaultPreset";
 
 import { JavaScriptObfuscator } from "../JavaScriptObfuscator";
-import {execSync} from "child_process";
 
 export class JavaScriptObfuscatorCLI {
+    /**
+     * @type {BufferEncoding}
+     */
+    private static encoding: BufferEncoding = 'utf8';
+
     /**
      * @type {string}
      */
@@ -70,7 +75,9 @@ export class JavaScriptObfuscatorCLI {
      * @returns {string}
      */
     private static getBuildVersion (): string {
-        return String(execSync(`npm info ${JavaScriptObfuscatorCLI.packageName} version`));
+        return execSync(`npm info ${JavaScriptObfuscatorCLI.packageName} version`, {
+            encoding: JavaScriptObfuscatorCLI.encoding
+        });
     }
 
     /**
@@ -108,17 +115,12 @@ export class JavaScriptObfuscatorCLI {
             .parse(this.argv);
 
         commands.on('--help', () => {
-            let isWindows: boolean = process.platform === 'win32';
+            let isWindows: boolean = process.platform === 'win32',
+                commandName: string = isWindows ? 'type' : 'cat';
 
             console.log('  Examples:\n');
             console.log('    %> javascript-obfuscator < in.js > out.js');
-
-            if (isWindows) {
-                console.log('    %> type in1.js in2.js | javascript-obfuscator > out.js');
-            } else {
-                console.log('    %> cat in1.js in2.js | javascript-obfuscator > out.js');
-            }
-
+            console.log(`    %> ${commandName} in1.js in2.js | javascript-obfuscator > out.js`);
             console.log('');
 
             process.exit();
@@ -126,7 +128,7 @@ export class JavaScriptObfuscatorCLI {
     }
 
     private configureProcess (): void {
-        this.stdin.setEncoding('utf-8');
+        this.stdin.setEncoding(JavaScriptObfuscatorCLI.encoding);
 
         this.stdin.on('readable', () => {
             let chunk: string;

+ 6 - 2
test/JavaScriptObfuscatorCLI.spec.ts

@@ -1,3 +1,5 @@
+import * as child_process from 'child_process';
+
 import { JavaScriptObfuscatorCLI } from "../src/cli/JavaScriptObfuscatorCLI";
 
 let assert: any = require('chai').assert,
@@ -12,7 +14,9 @@ describe('JavaScriptObfuscatorCLI', () => {
 
             this.timeout(7000);
 
-            stdout.captureData();
+            stdin.send('test/dev/test.js');
+
+            stdout.startCapture();
 
             let CLI: JavaScriptObfuscatorCLI = new JavaScriptObfuscatorCLI(
                 [
@@ -23,7 +27,7 @@ describe('JavaScriptObfuscatorCLI', () => {
                     '--selfDefending',
                     'false'
                 ],
-                stdin.send(new Buffer('var test = \'abc\';')),
+                stdin,
                 stdout
             );