diff --git a/dist/index.js b/dist/index.js index ba48f0c3..ed518c75 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1083,7 +1083,6 @@ module.exports = this.data = ''; this.rawArguments = argv; this.arguments = this.rawArguments.slice(2); - this.inputFilePath = this.arguments[0]; } _createClass(JavaScriptObfuscatorCLI, [{ @@ -1094,7 +1093,7 @@ module.exports = commands.outputHelp(); return; } - JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath); + this.inputPath = this.getInputPath(); this.getData(); this.processData(); } @@ -1117,7 +1116,16 @@ module.exports = }, { key: 'getData', value: function getData() { - this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding); + this.data = fs.readFileSync(this.inputPath, JavaScriptObfuscatorCLI.encoding); + } + }, { + key: 'getInputPath', + value: function getInputPath() { + var inputPath = this.arguments[0]; + if (!JavaScriptObfuscatorCLI.isFilePath(inputPath)) { + throw new ReferenceError('First argument must be a valid file path'); + } + return inputPath; } }, { key: 'getOutputPath', @@ -1126,7 +1134,7 @@ module.exports = if (outputPath) { return outputPath; } - return this.inputFilePath.split('.').map(function (value, index) { + return this.inputPath.split('.').map(function (value, index) { return index === 0 ? value + '-obfuscated' : value; }).join('.'); } @@ -1153,13 +1161,6 @@ module.exports = } return Object.assign({}, DefaultPreset_1.DEFAULT_PRESET, options); } - }, { - key: 'checkFilePath', - value: function checkFilePath(filePath) { - if (!fs.existsSync(filePath)) { - throw new Error('Wrong input file `' + filePath + '`'); - } - } }, { key: 'getBuildVersion', value: function getBuildVersion() { @@ -1167,6 +1168,15 @@ module.exports = encoding: JavaScriptObfuscatorCLI.encoding }); } + }, { + key: 'isFilePath', + value: function isFilePath(filePath) { + try { + return fs.statSync(filePath).isFile(); + } catch (e) { + return false; + } + } }, { key: 'parseBoolean', value: function parseBoolean(value) { diff --git a/src/cli/JavaScriptObfuscatorCLI.ts b/src/cli/JavaScriptObfuscatorCLI.ts index 080b3b90..d117f6a9 100644 --- a/src/cli/JavaScriptObfuscatorCLI.ts +++ b/src/cli/JavaScriptObfuscatorCLI.ts @@ -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((value: string, index: number) => { return index === 0 ? `${value}-obfuscated` : value; diff --git a/test/JavaScriptObfuscatorCLI.spec.ts b/test/JavaScriptObfuscatorCLI.spec.ts index e0cdb9c2..1a65f258 100644 --- a/test/JavaScriptObfuscatorCLI.spec.ts +++ b/test/JavaScriptObfuscatorCLI.spec.ts @@ -1,35 +1,18 @@ -import * as child_process from 'child_process'; - import { JavaScriptObfuscatorCLI } from "../src/cli/JavaScriptObfuscatorCLI"; -let assert: any = require('chai').assert, - stream: any = require("mock-utf8-stream"), - mockStdin: any = require('mock-stdin'); +let assert: any = require('chai').assert; describe('JavaScriptObfuscatorCLI', () => { describe('run (): void', () => { - it('should obfuscate file with JS code', function (done: MochaDone): void { - let stdin: any = mockStdin.stdin(), - stdout: any = new stream.MockWritableStream(); - - this.timeout(7000); - - stdin.send('test/dev/test.js'); - - stdout.startCapture(); - - let CLI: JavaScriptObfuscatorCLI = new JavaScriptObfuscatorCLI( - [ - 'node', - 'javascript-obfuscator', - '--compact', - 'false', - '--selfDefending', - 'false' - ], - stdin, - stdout - ); + it('should obfuscate file with JS code', () => { + let CLI: JavaScriptObfuscatorCLI = new JavaScriptObfuscatorCLI([ + 'node', + 'javascript-obfuscator', + '--compact', + 'false', + '--selfDefending', + 'false' + ]); CLI.run();