浏览代码

bin script refactoring

sanex3339 9 年之前
父节点
当前提交
a62e6c07f2
共有 5 个文件被更改,包括 109 次插入88 次删除
  1. 1 5
      bin/javascript-obfuscator.js
  2. 50 36
      dist/index.js
  3. 1 1
      package.json
  4. 2 8
      src/JavaScriptObfuscator.ts
  5. 55 38
      src/cli/JavaScriptObfuscatorCLI.ts

+ 1 - 5
bin/javascript-obfuscator.js

@@ -1,7 +1,3 @@
 #!/usr/bin/env node
 
-require('../dist/index').runCLI(
-    process.argv,
-    process.stdin,
-    process.stdout
-);
+require('../dist/index').runCLI(process.argv);

+ 50 - 36
dist/index.js

@@ -648,8 +648,8 @@ module.exports =
 	        }
 	    }, {
 	        key: 'runCLI',
-	        value: function runCLI(argv, stdin, stdout) {
-	            new JavaScriptObfuscatorCLI_1.JavaScriptObfuscatorCLI(argv, stdin, stdout).run();
+	        value: function runCLI(argv) {
+	            new JavaScriptObfuscatorCLI_1.JavaScriptObfuscatorCLI(argv).run();
 	        }
 	    }, {
 	        key: 'generateCode',
@@ -1071,73 +1071,74 @@ module.exports =
 	function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 	
 	var commands = __webpack_require__(43);
+	var fs = __webpack_require__(44);
 	var child_process_1 = __webpack_require__(42);
 	var DefaultPreset_1 = __webpack_require__(14);
 	var JavaScriptObfuscator_1 = __webpack_require__(9);
 	
 	var JavaScriptObfuscatorCLI = function () {
-	    function JavaScriptObfuscatorCLI(argv, stdin, stdout) {
+	    function JavaScriptObfuscatorCLI(argv) {
 	        _classCallCheck(this, JavaScriptObfuscatorCLI);
 	
 	        this.data = '';
-	        this.argv = argv;
-	        this.stdin = stdin;
-	        this.stdout = stdout;
+	        this.rawArguments = argv;
+	        this.arguments = this.rawArguments.slice(2);
+	        this.inputFilePath = this.arguments[0];
 	    }
 	
 	    _createClass(JavaScriptObfuscatorCLI, [{
-	        key: "run",
+	        key: 'run',
 	        value: function run() {
-	            this.configureProcess();
 	            this.configureCommands();
-	            if (!this.isDataExist()) {
+	            if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
 	                commands.outputHelp();
+	                return;
 	            }
+	            JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath);
+	            this.getData();
+	            this.processData();
 	        }
 	    }, {
-	        key: "configureCommands",
+	        key: 'configureCommands',
 	        value: function configureCommands() {
-	            commands.version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version').usage('[options] STDIN STDOUT').option('--compact <boolean>', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames <list>', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', function (val) {
+	            commands.version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version').usage('<inputPath> [options]').option('-o, --output <path>', 'Output path for obfuscated code').option('--compact <boolean>', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames <list>', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', function (val) {
 	                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);
+	            }).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.rawArguments);
 	            commands.on('--help', function () {
 	                var isWindows = process.platform === 'win32',
 	                    commandName = isWindows ? 'type' : 'cat';
 	                console.log('  Examples:\n');
 	                console.log('    %> javascript-obfuscator < in.js > out.js');
-	                console.log("    %> " + commandName + " in1.js in2.js | javascript-obfuscator > out.js");
+	                console.log('    %> ' + commandName + ' in1.js in2.js | javascript-obfuscator > out.js');
 	                console.log('');
 	                process.exit();
 	            });
 	        }
 	    }, {
-	        key: "configureProcess",
-	        value: function configureProcess() {
-	            var _this = this;
-	
-	            this.stdin.setEncoding(JavaScriptObfuscatorCLI.encoding);
-	            this.stdin.on('readable', function () {
-	                var chunk = void 0;
-	                while (chunk = _this.stdin.read()) {
-	                    _this.data += chunk;
-	                }
-	            });
-	            this.stdin.on('end', function () {
-	                return _this.processData();
-	            });
+	        key: 'getData',
+	        value: function getData() {
+	            this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding);
 	        }
 	    }, {
-	        key: "isDataExist",
-	        value: function isDataExist() {
-	            return !process.env.__DIRECT__ && !this.stdin.isTTY;
+	        key: 'getOutputPath',
+	        value: function getOutputPath() {
+	            var outputPath = commands.output;
+	            if (outputPath) {
+	                return outputPath;
+	            }
+	            return this.inputFilePath.split('.').map(function (value, index) {
+	                return index === 0 ? value + '-obfuscated' : value;
+	            }).join('.');
 	        }
 	    }, {
-	        key: "processData",
+	        key: 'processData',
 	        value: function processData() {
-	            this.stdout.write(JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()));
+	            fs.writeFileSync(this.getOutputPath(), JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()), {
+	                encoding: JavaScriptObfuscatorCLI.encoding
+	            });
 	        }
 	    }], [{
-	        key: "buildOptions",
+	        key: 'buildOptions',
 	        value: function buildOptions() {
 	            var options = {},
 	                availableOptions = Object.keys(DefaultPreset_1.DEFAULT_PRESET);
@@ -1153,14 +1154,21 @@ module.exports =
 	            return Object.assign({}, DefaultPreset_1.DEFAULT_PRESET, options);
 	        }
 	    }, {
-	        key: "getBuildVersion",
+	        key: 'checkFilePath',
+	        value: function checkFilePath(filePath) {
+	            if (!fs.existsSync(filePath)) {
+	                throw new Error('Wrong input file `' + filePath + '`');
+	            }
+	        }
+	    }, {
+	        key: 'getBuildVersion',
 	        value: function getBuildVersion() {
-	            return child_process_1.execSync("npm info " + JavaScriptObfuscatorCLI.packageName + " version", {
+	            return child_process_1.execSync('npm info ' + JavaScriptObfuscatorCLI.packageName + ' version', {
 	                encoding: JavaScriptObfuscatorCLI.encoding
 	            });
 	        }
 	    }, {
-	        key: "parseBoolean",
+	        key: 'parseBoolean',
 	        value: function parseBoolean(value) {
 	            return value === 'true' || value === '1';
 	        }
@@ -2617,6 +2625,12 @@ module.exports =
 
 	module.exports = require("commander");
 
+/***/ },
+/* 44 */
+/***/ function(module, exports) {
+
+	module.exports = require("fs");
+
 /***/ }
 /******/ ]);
 //# sourceMappingURL=index.js.map

+ 1 - 1
package.json

@@ -54,7 +54,7 @@
     "test:dev": "node test/dev/test.js",
     "test:full": "npm run test:compile && node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- test-tmp/test/**/**.spec.js && npm run test:removeTestTmpDir",
     "test:coveralls": "npm run test:compile && node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- test-tmp/test/**/**.spec.js --report lcovonly && cat ./coverage/lcov.info | coveralls && npm run test:removeTestTmpDir",
-    "test:mocha": "npm run test:compile && node_modules/.bin/mocha test-tmp/test/**/**.spec.js  && npm run test:removeTestTmpDir",
+    "test:mocha": "npm run test:compile && node_modules/.bin/mocha test-tmp/test/**/**.spec.js && npm run test:removeTestTmpDir",
     "test:removeTestTmpDir": "rm -rf test-tmp",
     "test": "npm run test:dev && npm run test:full",
     "tslint": "tslint src/**/*.ts",

+ 2 - 8
src/JavaScriptObfuscator.ts

@@ -36,15 +36,9 @@ export class JavaScriptObfuscator {
 
     /**
      * @param argv
-     * @param stdin
-     * @param stdout
      */
-    public static runCLI (
-        argv: string[],
-        stdin: NodeJS.ReadableStream,
-        stdout: NodeJS.WritableStream
-    ): void {
-        new JavaScriptObfuscatorCLI(argv, stdin, stdout).run();
+    public static runCLI (argv: string[]): void {
+        new JavaScriptObfuscatorCLI(argv).run();
     }
 
     /**

+ 55 - 38
src/cli/JavaScriptObfuscatorCLI.ts

@@ -1,5 +1,5 @@
 import * as commands from 'commander';
-import * as tty from 'tty';
+import * as fs from 'fs';
 import { execSync } from "child_process";
 
 import { IOptionsPreset } from "../interfaces/IOptionsPreset";
@@ -22,31 +22,27 @@ export class JavaScriptObfuscatorCLI {
     /**
      * @type {string[]}
      */
-    private argv: string[];
+    private arguments: string[];
 
     /**
-     * @type {string}
+     * @type {string[]}
      */
-    private data: string = '';
+    private rawArguments: string[];
 
     /**
-     * @type {NodeJS.ReadableStream}
+     * @type {string}
      */
-    private stdin: NodeJS.ReadableStream;
+    private data: string = '';
 
     /**
-     * @type {NodeJS.WritableStream}
+     * @type {string}
      */
-    private stdout: NodeJS.WritableStream;
-
-    constructor (
-        argv: string[],
-        stdin: NodeJS.ReadableStream,
-        stdout: NodeJS.WritableStream
-    ) {
-        this.argv = argv;
-        this.stdin = stdin;
-        this.stdout = stdout;
+    private inputFilePath: string;
+
+    constructor (argv: string[]) {
+        this.rawArguments = argv;
+        this.arguments = this.rawArguments.slice(2);
+        this.inputFilePath = this.arguments[0];
     }
 
     /**
@@ -71,6 +67,15 @@ export class JavaScriptObfuscatorCLI {
         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}
      */
@@ -89,18 +94,25 @@ export class JavaScriptObfuscatorCLI {
     }
 
     public run (): void {
-        this.configureProcess();
         this.configureCommands();
 
-        if (!this.isDataExist()) {
+        if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
             commands.outputHelp();
+
+            return;
         }
+
+        JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath);
+
+        this.getData();
+        this.processData();
     }
 
     private configureCommands (): void {
         commands
             .version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version')
-            .usage('[options] STDIN STDOUT')
+            .usage('<inputPath> [options]')
+            .option('-o, --output <path>', 'Output path for obfuscated code')
             .option('--compact <boolean>', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean)
             .option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean)
             .option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean)
@@ -112,7 +124,7 @@ export class JavaScriptObfuscatorCLI {
             .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);
+            .parse(this.rawArguments);
 
         commands.on('--help', () => {
             let isWindows: boolean = process.platform === 'win32',
@@ -127,30 +139,35 @@ export class JavaScriptObfuscatorCLI {
         });
     }
 
-    private configureProcess (): void {
-        this.stdin.setEncoding(JavaScriptObfuscatorCLI.encoding);
-
-        this.stdin.on('readable', () => {
-            let chunk: string;
-
-            while (chunk = <string>this.stdin.read()) {
-                this.data += chunk;
-            }
-        });
-
-        this.stdin.on('end', () => this.processData());
+    private getData (): void {
+        this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding);
     }
 
     /**
-     * @returns {boolean}
+     * @returns {string}
      */
-    private isDataExist (): boolean {
-        return !process.env.__DIRECT__ && !(<tty.ReadStream>this.stdin).isTTY;
+    private getOutputPath (): string {
+        let outputPath: string = (<any>commands).output;
+
+        if (outputPath) {
+            return outputPath;
+        }
+
+        return this.inputFilePath
+            .split('.')
+            .map<string>((value: string, index: number) => {
+                return index === 0 ? `${value}-obfuscated` : value;
+            })
+            .join('.');
     }
 
     private processData (): void {
-        this.stdout.write(
-            JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions())
+        fs.writeFileSync(
+            this.getOutputPath(),
+            JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()),
+            {
+                encoding: JavaScriptObfuscatorCLI.encoding
+            }
         );
     }
 }