bin script refactoring

This commit is contained in:
sanex3339
2016-06-25 17:38:13 +03:00
parent a62e6c07f2
commit 97430e779b
3 changed files with 56 additions and 52 deletions
+25 -14
View File
@@ -37,12 +37,11 @@ export class JavaScriptObfuscatorCLI {
/**
* @type {string}
*/
private inputFilePath: string;
private inputPath: string;
constructor (argv: string[]) {
this.rawArguments = argv;
this.arguments = this.rawArguments.slice(2);
this.inputFilePath = this.arguments[0];
}
/**
@@ -67,15 +66,6 @@ export class JavaScriptObfuscatorCLI {
return Object.assign({}, DEFAULT_PRESET, options);
}
/**
* @param filePath
*/
private static checkFilePath (filePath: string): void {
if (!fs.existsSync(filePath)) {
throw new Error(`Wrong input file \`${filePath}\``);
}
}
/**
* @returns {string}
*/
@@ -85,6 +75,17 @@ export class JavaScriptObfuscatorCLI {
});
}
/**
* @param filePath
*/
private static isFilePath (filePath: string): boolean {
try {
return fs.statSync(filePath).isFile();
} catch (e) {
return false;
}
}
/**
* @param value
* @returns {boolean}
@@ -102,7 +103,7 @@ export class JavaScriptObfuscatorCLI {
return;
}
JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath);
this.inputPath = this.getInputPath();
this.getData();
this.processData();
@@ -140,7 +141,17 @@ export class JavaScriptObfuscatorCLI {
}
private getData (): void {
this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding);
this.data = fs.readFileSync(this.inputPath, JavaScriptObfuscatorCLI.encoding);
}
private getInputPath (): string {
let inputPath: string = this.arguments[0];
if (!JavaScriptObfuscatorCLI.isFilePath(inputPath)) {
throw new ReferenceError(`First argument must be a valid file path`);
}
return inputPath;
}
/**
@@ -153,7 +164,7 @@ export class JavaScriptObfuscatorCLI {
return outputPath;
}
return this.inputFilePath
return this.inputPath
.split('.')
.map<string>((value: string, index: number) => {
return index === 0 ? `${value}-obfuscated` : value;