Explorar o código

CLI class refactoring
WIP tests for CLI class

sanex3339 %!s(int64=9) %!d(string=hai) anos
pai
achega
afff873f31

+ 17 - 0
dist/index.js

@@ -1072,6 +1072,8 @@ module.exports =
 	
 	var commands = __webpack_require__(43);
 	var fs = __webpack_require__(44);
+	var mkdirp = __webpack_require__(46);
+	var path = __webpack_require__(45);
 	var child_process_1 = __webpack_require__(42);
 	var DefaultPreset_1 = __webpack_require__(14);
 	var JavaScriptObfuscator_1 = __webpack_require__(9);
@@ -1141,6 +1143,9 @@ module.exports =
 	    }, {
 	        key: 'processData',
 	        value: function processData() {
+	            var outputPath = this.getOutputPath(),
+	                dirName = path.dirname(outputPath);
+	            mkdirp.sync(dirName);
 	            fs.writeFileSync(this.getOutputPath(), JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()), {
 	                encoding: JavaScriptObfuscatorCLI.encoding
 	            });
@@ -2641,6 +2646,18 @@ module.exports =
 
 	module.exports = require("fs");
 
+/***/ },
+/* 45 */
+/***/ function(module, exports) {
+
+	module.exports = require("path");
+
+/***/ },
+/* 46 */
+/***/ function(module, exports) {
+
+	module.exports = require("mkdirp");
+
 /***/ }
 /******/ ]);
 //# sourceMappingURL=index.js.map

+ 1 - 0
package.json

@@ -22,6 +22,7 @@
     "escodegen": "^1.8.0",
     "esprima": "^2.7.2",
     "estraverse": "^4.2.0",
+    "mkdirp": "^0.5.1",
     "source-map-support": "^0.4.0"
   },
   "devDependencies": {

+ 7 - 0
src/cli/JavaScriptObfuscatorCLI.ts

@@ -1,5 +1,7 @@
 import * as commands from 'commander';
 import * as fs from 'fs';
+import * as mkdirp from 'mkdirp';
+import * as path from 'path';
 import { execSync } from "child_process";
 
 import { IOptionsPreset } from "../interfaces/IOptionsPreset";
@@ -173,6 +175,11 @@ export class JavaScriptObfuscatorCLI {
     }
 
     private processData (): void {
+        let outputPath: string = this.getOutputPath(),
+            dirName: string = path.dirname(outputPath);
+
+        mkdirp.sync(dirName);
+
         fs.writeFileSync(
             this.getOutputPath(),
             JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()),

+ 22 - 2
test/JavaScriptObfuscatorCLI.spec.ts

@@ -1,13 +1,26 @@
+import * as fs from 'fs';
+
 import { JavaScriptObfuscatorCLI } from "../src/cli/JavaScriptObfuscatorCLI";
 
 let assert: any = require('chai').assert;
 
 describe('JavaScriptObfuscatorCLI', () => {
+    let CLI: JavaScriptObfuscatorCLI,
+        fixturesDirName: string = 'test/fixtures',
+        tmpDirName: string = 'test/tmp',
+        fixtureFileName: string = 'sample.js',
+        fixtureFilePath: string = `${fixturesDirName}/${fixtureFileName}`,
+        outputFileName: string = 'sample-obfuscated.js',
+        outputFilePath: string = `${tmpDirName}/${outputFileName}`;
+
     describe('run (): void', () => {
-        it('should obfuscate file with JS code', () => {
-            let CLI: JavaScriptObfuscatorCLI = new JavaScriptObfuscatorCLI([
+        beforeEach(() => {
+            CLI = new JavaScriptObfuscatorCLI([
                 'node',
                 'javascript-obfuscator',
+                fixtureFilePath,
+                '--output',
+                outputFilePath,
                 '--compact',
                 'false',
                 '--selfDefending',
@@ -15,8 +28,15 @@ describe('JavaScriptObfuscatorCLI', () => {
             ]);
 
             CLI.run();
+        });
 
+        it('should obfuscate file with JS code', () => {
             assert.equal(1, 1);
         });
+
+        afterEach(() => {
+            fs.unlinkSync(outputFilePath);
+            fs.rmdirSync(tmpDirName);
+        });
     });
 });

+ 1 - 0
typings.json

@@ -9,6 +9,7 @@
     "esprima": "github:DefinitelyTyped/DefinitelyTyped/esprima/esprima.d.ts",
     "escodegen": "github:DefinitelyTyped/DefinitelyTyped/escodegen/escodegen.d.ts",
     "estraverse": "github:DefinitelyTyped/DefinitelyTyped/estraverse/estraverse.d.ts",
+    "mkdirp": "github:DefinitelyTyped/DefinitelyTyped/mkdirp/mkdirp.d.ts",
     "mocha": "github:DefinitelyTyped/DefinitelyTyped/mocha/mocha.d.ts",
     "chai": "github:DefinitelyTyped/DefinitelyTyped/chai/chai.d.ts"
   }

+ 1 - 0
typings/browser.d.ts

@@ -5,5 +5,6 @@
 /// <reference path="browser/ambient/esprima/index.d.ts" />
 /// <reference path="browser/ambient/estraverse/index.d.ts" />
 /// <reference path="browser/ambient/estree/index.d.ts" />
+/// <reference path="browser/ambient/mkdirp/index.d.ts" />
 /// <reference path="browser/ambient/mocha/index.d.ts" />
 /// <reference path="browser/ambient/node/index.d.ts" />

+ 17 - 0
typings/browser/ambient/mkdirp/index.d.ts

@@ -0,0 +1,17 @@
+// Generated by typings
+// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/mkdirp/mkdirp.d.ts
+// Type definitions for mkdirp 0.3.0
+// Project: http://github.com/substack/node-mkdirp
+// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
+// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
+
+declare module 'mkdirp' {
+
+	function mkdirp(dir: string, cb: (err: any, made: string) => void): void;
+	function mkdirp(dir: string, flags: any, cb: (err: any, made: string) => void): void;
+
+	namespace mkdirp {
+		function sync(dir: string, flags?: any): string;
+	}
+	export = mkdirp;
+}

+ 1 - 0
typings/main.d.ts

@@ -5,5 +5,6 @@
 /// <reference path="main/ambient/esprima/index.d.ts" />
 /// <reference path="main/ambient/estraverse/index.d.ts" />
 /// <reference path="main/ambient/estree/index.d.ts" />
+/// <reference path="main/ambient/mkdirp/index.d.ts" />
 /// <reference path="main/ambient/mocha/index.d.ts" />
 /// <reference path="main/ambient/node/index.d.ts" />

+ 17 - 0
typings/main/ambient/mkdirp/index.d.ts

@@ -0,0 +1,17 @@
+// Generated by typings
+// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/mkdirp/mkdirp.d.ts
+// Type definitions for mkdirp 0.3.0
+// Project: http://github.com/substack/node-mkdirp
+// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
+// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
+
+declare module 'mkdirp' {
+
+	function mkdirp(dir: string, cb: (err: any, made: string) => void): void;
+	function mkdirp(dir: string, flags: any, cb: (err: any, made: string) => void): void;
+
+	namespace mkdirp {
+		function sync(dir: string, flags?: any): string;
+	}
+	export = mkdirp;
+}