CLI tests and CLI class refactoring

This commit is contained in:
sanex3339
2016-06-27 23:30:01 +03:00
parent e299456f71
commit ef5c2c6de3
3 changed files with 86 additions and 68 deletions
+33 -28
View File
@@ -1,4 +1,4 @@
import * as commands from 'commander';
import { Command } from 'commander';
import * as fs from 'fs';
import * as mkdirp from 'mkdirp';
import * as path from 'path';
@@ -33,6 +33,11 @@ export class JavaScriptObfuscatorCLI {
*/
private arguments: string[];
/**
* @type {commander.ICommand}
*/
private commands: commander.ICommand;
/**
* @type {string[]}
*/
@@ -56,28 +61,6 @@ export class JavaScriptObfuscatorCLI {
this.arguments = this.rawArguments.slice(2);
}
/**
* @returns {IOptionsPreset}
*/
private static buildOptions (): IOptionsPreset {
let options: IOptionsPreset = {},
availableOptions: string[] = Object.keys(DEFAULT_PRESET);
for (let option in commands) {
if (!commands.hasOwnProperty(option)) {
continue;
}
if (availableOptions.indexOf(option) === -1) {
continue;
}
options[option] = (<any>commands)[option];
}
return Object.assign({}, DEFAULT_PRESET, options);
}
/**
* @returns {string}
*/
@@ -110,7 +93,7 @@ export class JavaScriptObfuscatorCLI {
this.configureCommands();
if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
commands.outputHelp();
this.commands.outputHelp();
return;
}
@@ -121,8 +104,30 @@ export class JavaScriptObfuscatorCLI {
this.processData();
}
/**
* @returns {IOptionsPreset}
*/
private buildOptions (): IOptionsPreset {
let options: IOptionsPreset = {},
availableOptions: string[] = Object.keys(DEFAULT_PRESET);
for (let option in this.commands) {
if (!this.commands.hasOwnProperty(option)) {
continue;
}
if (availableOptions.indexOf(option) === -1) {
continue;
}
options[option] = (<any>this.commands)[option];
}
return Object.assign({}, DEFAULT_PRESET, options);
}
private configureCommands (): void {
commands
this.commands = new Command()
.version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version')
.usage('<inputPath> [options]')
.option('-o, --output <path>', 'Output path for obfuscated code')
@@ -139,7 +144,7 @@ export class JavaScriptObfuscatorCLI {
.option('--wrapUnicodeArrayCalls <boolean>', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean)
.parse(this.rawArguments);
commands.on('--help', () => {
this.commands.on('--help', () => {
let isWindows: boolean = process.platform === 'win32',
commandName: string = isWindows ? 'type' : 'cat';
@@ -177,7 +182,7 @@ export class JavaScriptObfuscatorCLI {
* @returns {string}
*/
private getOutputPath (): string {
let outputPath: string = (<any>commands).output;
let outputPath: string = (<any>this.commands).output;
if (outputPath) {
return outputPath;
@@ -199,7 +204,7 @@ export class JavaScriptObfuscatorCLI {
fs.writeFileSync(
this.getOutputPath(),
JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()),
JavaScriptObfuscator.obfuscate(this.data, this.buildOptions()),
{
encoding: JavaScriptObfuscatorCLI.encoding
}