Selaa lähdekoodia

Add --config CLI option that accepts name of js / json file that contains all config instead of passing everything in CLI

psabharwal 7 vuotta sitten
vanhempi
commit
61ab7a9956

+ 6 - 0
README.md

@@ -238,6 +238,7 @@ Following options are available for the JS Obfuscator:
     -o, --output
 
     --compact <boolean>
+    --config <string>
     --controlFlowFlattening <boolean>
     --controlFlowFlatteningThreshold <number>
     --deadCodeInjection <boolean>
@@ -265,6 +266,11 @@ Type: `boolean` Default: `true`
 
 Compact code output on one line.
 
+### `config`
+Type: `string` Default: ``
+
+Name of JS/JSON config file which contains options mentioned above. These will be overridden by options passed directly to CLI
+
 ### `controlFlowFlattening`
 Type: `boolean` Default: `false`
 

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 0 - 0
dist/index.js


+ 23 - 9
src/cli/JavaScriptObfuscatorCLI.ts

@@ -71,11 +71,25 @@ export class JavaScriptObfuscatorCLI {
      * @returns {TInputOptions}
      */
     private buildOptions (): TInputOptions {
-        const inputOptions: TInputOptions = {};
+        const inputOptions: TInputOptions = this.sanitizeOptions(this.commands);
+        const configFileLocation: string = this.commands.config ?
+            path.resolve(this.commands.config, '.') : '';
+        const configFileOptions: TInputOptions = configFileLocation ?
+            this.sanitizeOptions(require(configFileLocation)) : {};
+
+        return {
+            ...DEFAULT_PRESET,
+            ...configFileOptions,
+            ...inputOptions
+        };
+    }
+
+    private sanitizeOptions (options: any): TInputOptions {
+        const filteredOptions: TInputOptions = {};
         const availableOptions: string[] = Object.keys(DEFAULT_PRESET);
 
-        for (const option in this.commands) {
-            if (!this.commands.hasOwnProperty(option)) {
+        for (const option in options) {
+            if (!options.hasOwnProperty(option)) {
                 continue;
             }
 
@@ -83,13 +97,9 @@ export class JavaScriptObfuscatorCLI {
                 continue;
             }
 
-            (<any>inputOptions)[option] = (<any>this.commands)[option];
+            (<any>filteredOptions)[option] = (<any>options)[option];
         }
-
-        return {
-            ...DEFAULT_PRESET,
-            ...inputOptions
-        };
+        return filteredOptions;
     }
 
     private configureCommands (): void {
@@ -105,6 +115,10 @@ export class JavaScriptObfuscatorCLI {
                 'Disable one line output code compacting',
                 BooleanSanitizer
             )
+            .option(
+                '--config <boolean>',
+                'Name of js / json config file'
+            )
             .option(
                 '--controlFlowFlattening <boolean>',
                 'Enables control flow flattening',

+ 1 - 0
src/options/presets/Default.ts

@@ -4,6 +4,7 @@ import { SourceMapMode } from '../../enums/SourceMapMode';
 
 export const DEFAULT_PRESET: TInputOptions = Object.freeze({
     compact: true,
+    config: '',
     controlFlowFlattening: false,
     controlFlowFlatteningThreshold: 0.75,
     deadCodeInjection: false,

+ 5 - 0
test/fixtures/config.js

@@ -0,0 +1,5 @@
+module.exports = {
+	compact: true,
+	selfDefending: false,
+	sourceMap: true
+};

+ 91 - 0
test/functional-tests/javascript-obfuscator-cli/JavaScriptObfuscatorCLI.spec.ts

@@ -17,6 +17,10 @@ describe('JavaScriptObfuscatorCLI', function (): void {
     const outputDirName: string = 'test/tmp';
     const outputFileName: string = 'sample-obfuscated.js';
     const outputFilePath: string = `${outputDirName}/${outputFileName}`;
+    const configDirName: string = 'test/fixtures';
+    const configFileName: string = 'config.js';
+    const configFilePath: string = `${configDirName}/${configFileName}`;
+
 
     describe('run (): void', () => {
         before(() => {
@@ -377,6 +381,93 @@ describe('JavaScriptObfuscatorCLI', function (): void {
             });
         });
 
+        describe('`--config` option is set', () => {
+            const outputSourceMapPath: string = `${outputFilePath}.map`;
+
+            let isFileExist: boolean,
+                sourceMapObject: any;
+
+            before(() => {
+                JavaScriptObfuscator.runCLI([
+                    'node',
+                    'javascript-obfuscator',
+                    fixtureFilePath,
+                    '--output',
+                    outputFilePath,
+                    '--config',
+                    configFilePath
+                ]);
+
+                try {
+                    const content: string = fs.readFileSync(outputSourceMapPath, {encoding: 'utf8'});
+
+                    isFileExist = true;
+                    sourceMapObject = JSON.parse(content);
+                } catch (e) {
+                    isFileExist = false;
+                }
+            });
+
+            it('should create file with source map in the same directory as output file', () => {
+                assert.equal(isFileExist, true);
+            });
+
+            it('source map from created file should contains property `version`', () => {
+                assert.property(sourceMapObject, 'version');
+            });
+
+            it('source map from created file should contains property `sources`', () => {
+                assert.property(sourceMapObject, 'sources');
+            });
+
+            it('source map from created file should contains property `names`', () => {
+                assert.property(sourceMapObject, 'names');
+            });
+
+            after(() => {
+                fs.unlinkSync(outputFilePath);
+                fs.unlinkSync(outputSourceMapPath);
+            });
+        });
+
+        describe('`--config` option is set but overridden by CLI option', () => {
+            const outputSourceMapPath: string = `${outputFilePath}.map`;
+
+            let isFileExist: boolean,
+                sourceMapObject: any;
+
+            before(() => {
+                JavaScriptObfuscator.runCLI([
+                    'node',
+                    'javascript-obfuscator',
+                    fixtureFilePath,
+                    '--output',
+                    outputFilePath,
+                    '--config',
+                    configFilePath,
+                    '--sourceMap',
+                    'false',
+                ]);
+
+                try {
+                    const content: string = fs.readFileSync(outputSourceMapPath, {encoding: 'utf8'});
+
+                    isFileExist = true;
+                    sourceMapObject = JSON.parse(content);
+                } catch (e) {
+                    isFileExist = false;
+                }
+            });
+
+            it('should create file without source map in the same directory as output file', () => {
+                assert.equal(isFileExist, false);
+            });
+
+            after(() => {
+                fs.unlinkSync(outputFilePath);
+            });
+        });
+
         after(() => {
             fs.rmdirSync(outputDirName);
         });

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä