mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Merge pull request #73 from psabharwal123/master
Add --config CLI option
This commit is contained in:
@@ -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`
|
||||
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -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',
|
||||
|
||||
@@ -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,
|
||||
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
compact: true,
|
||||
selfDefending: false,
|
||||
sourceMap: true
|
||||
};
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user