Browse Source

Fixed require of external user config file

sanex3339 7 years ago
parent
commit
15c755e705

+ 5 - 1
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 ===
+v0.10.1
+---
+* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/76.
+
 v0.10.0
 ---
 * **New option:** `deadCodeInjection`. With this option random blocks of dead code will add to the obfuscated code.
@@ -10,7 +14,7 @@ v0.10.0
 * **Breaking change:** `escapeUnicodeSequence` option now disabled by default.
 * `controlFlowFlattening` now affects string literal nodes.
 * Increased runtime performance with `rc4` `stringArrayEncoding`.
-* Added support for async functions
+* Added support for async functions.
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/71
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/65
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/60

File diff suppressed because it is too large
+ 0 - 0
dist/index.js


+ 2 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.10.0",
+  "version": "0.10.1",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",
@@ -49,6 +49,7 @@
     "@types/node": "8.0.17",
     "@types/sinon": "2.3.3",
     "@types/string-template": "1.0.2",
+    "@types/webpack-env": "1.13.0",
     "awesome-typescript-loader": "3.2.2",
     "babel-cli": "6.24.1",
     "babel-loader": "7.1.1",

+ 3 - 3
src/cli/JavaScriptObfuscatorCLI.ts

@@ -51,10 +51,10 @@ export class JavaScriptObfuscatorCLI {
     }
 
     /**
-     * @param options
+     * @param {Object} options
      * @returns {TInputOptions}
      */
-    private static sanitizeOptions (options: {[key: string]: any}): TInputOptions {
+    private static sanitizeOptions (options: Object): TInputOptions {
         const filteredOptions: TInputOptions = {};
         const availableOptions: string[] = Object.keys(DEFAULT_PRESET);
 
@@ -99,7 +99,7 @@ export class JavaScriptObfuscatorCLI {
             ? path.resolve(this.commands.config, '.')
             : '';
         const configFileOptions: TInputOptions = configFileLocation
-            ? JavaScriptObfuscatorCLI.sanitizeOptions(require(configFileLocation))
+            ? JavaScriptObfuscatorCLI.sanitizeOptions(CLIUtils.getUserConfig(configFileLocation))
             : {};
 
         return {

+ 12 - 0
src/cli/utils/CLIUtils.ts

@@ -73,6 +73,18 @@ export class CLIUtils {
         );
     }
 
+    /**
+     * @param {string} configPath
+     * @returns {Object}
+     */
+    public static getUserConfig (configPath: string): Object {
+        const nativeRequire: Function = typeof __non_webpack_require__ === 'function'
+            ? __non_webpack_require__
+            : require;
+
+        return nativeRequire(configPath);
+    }
+
     /**
      * @param {string} filePath
      * @returns {boolean}

+ 23 - 0
test/unit-tests/cli/utils/CLIUtils.spec.ts

@@ -3,6 +3,8 @@ import * as mkdirp from 'mkdirp';
 
 import { assert } from 'chai';
 
+import { TInputOptions } from '../../../../src/types/options/TInputOptions';
+
 import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
 
 describe('CLIUtils', () => {
@@ -46,6 +48,27 @@ describe('CLIUtils', () => {
         });
     });
 
+    describe('getUserConfig (configPath: string): Object', () => {
+        const configDirName: string = 'test/fixtures';
+        const configFileName: string = 'config.js';
+        const configFilePath: string = `../../../${configDirName}/${configFileName}`;
+        const expectedResult: TInputOptions = {
+            compact: true,
+            selfDefending: false,
+            sourceMap: true
+        };
+
+        let result: Object;
+
+        before(() => {
+            result = CLIUtils.getUserConfig(configFilePath);
+        });
+
+        it('should return object with user configuration', () => {
+            assert.deepEqual(result, expectedResult);
+        });
+    });
+
     describe('validateInputPath (inputPath: string): void', () => {
         describe('`inputPath` is a valid path', () => {
             const tmpFileName: string = 'test.js';

+ 10 - 6
yarn.lock

@@ -6,6 +6,10 @@
   version "4.0.1"
   resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.0.1.tgz#37fea779617cfec3fd2b19a0247e8bbdd5133bf6"
 
+"@types/[email protected]":
+  version "0.4.31"
+  resolved "https://registry.yarnpkg.com/@types/chalk/-/chalk-0.4.31.tgz#a31d74241a6b1edbb973cf36d97a2896834a51f9"
+
 "@types/[email protected]":
   version "0.7.33"
   resolved "https://registry.yarnpkg.com/@types/chance/-/chance-0.7.33.tgz#9666ae5c8f602a14a67e5b608dabf1a7853a1e28"
@@ -52,11 +56,7 @@
   version "2.2.41"
   resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.41.tgz#e27cf0817153eb9f2713b2d3f6c68f1e1c3ca608"
 
-"@types/node@*":
-  version "8.0.15"
-  resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.15.tgz#8f23f8a4642ced357704d048010876fc0c7f179a"
-
-"@types/[email protected]":
+"@types/node@*", "@types/[email protected]":
   version "8.0.17"
   resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.17.tgz#677bc8c118cfb76013febb62ede1f31d2c7222a1"
 
@@ -68,6 +68,10 @@
   version "1.0.2"
   resolved "https://registry.yarnpkg.com/@types/string-template/-/string-template-1.0.2.tgz#363b273c9b456705e3111e3571e9248f6474eba4"
 
+"@types/webpack-env@^1.13.0":
+  version "1.13.0"
+  resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.13.0.tgz#3044381647e11ee973c5af2e925323930f691d80"
+
 abbrev@1, [email protected]:
   version "1.0.9"
   resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135"
@@ -933,7 +937,7 @@ [email protected], chalk@^1.0.0, chalk@^1.1.0, chalk@^1.1.1:
     strip-ansi "^3.0.0"
     supports-color "^2.0.0"
 
-chalk@^2.0.0:
+[email protected], chalk@^2.0.0:
   version "2.0.1"
   resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.0.1.tgz#dbec49436d2ae15f536114e76d14656cdbc0f44d"
   dependencies:

Some files were not shown because too many files changed in this diff