diff --git a/bin/javascript-obfuscator.js b/bin/javascript-obfuscator.js index 441409cf..4fe76f7e 100755 --- a/bin/javascript-obfuscator.js +++ b/bin/javascript-obfuscator.js @@ -1,7 +1,3 @@ #!/usr/bin/env node -require('../dist/index').runCLI( - process.argv, - process.stdin, - process.stdout -); \ No newline at end of file +require('../dist/index').runCLI(process.argv); \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 36390b95..ba48f0c3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -648,8 +648,8 @@ module.exports = } }, { key: 'runCLI', - value: function runCLI(argv, stdin, stdout) { - new JavaScriptObfuscatorCLI_1.JavaScriptObfuscatorCLI(argv, stdin, stdout).run(); + value: function runCLI(argv) { + new JavaScriptObfuscatorCLI_1.JavaScriptObfuscatorCLI(argv).run(); } }, { key: 'generateCode', @@ -1071,73 +1071,74 @@ module.exports = function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var commands = __webpack_require__(43); + var fs = __webpack_require__(44); var child_process_1 = __webpack_require__(42); var DefaultPreset_1 = __webpack_require__(14); var JavaScriptObfuscator_1 = __webpack_require__(9); var JavaScriptObfuscatorCLI = function () { - function JavaScriptObfuscatorCLI(argv, stdin, stdout) { + function JavaScriptObfuscatorCLI(argv) { _classCallCheck(this, JavaScriptObfuscatorCLI); this.data = ''; - this.argv = argv; - this.stdin = stdin; - this.stdout = stdout; + this.rawArguments = argv; + this.arguments = this.rawArguments.slice(2); + this.inputFilePath = this.arguments[0]; } _createClass(JavaScriptObfuscatorCLI, [{ - key: "run", + key: 'run', value: function run() { - this.configureProcess(); this.configureCommands(); - if (!this.isDataExist()) { + if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) { commands.outputHelp(); + return; } + JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath); + this.getData(); + this.processData(); } }, { - key: "configureCommands", + key: 'configureCommands', value: function configureCommands() { - commands.version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version').usage('[options] STDIN STDOUT').option('--compact ', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection ', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval ', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput ', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals ', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames ', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', function (val) { + commands.version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version').usage(' [options]').option('-o, --output ', 'Output path for obfuscated code').option('--compact ', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtection ', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--debugProtectionInterval ', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean).option('--disableConsoleOutput ', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean).option('--encodeUnicodeLiterals ', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean).option('--reservedNames ', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', function (val) { return val.split(','); - }).option('--rotateUnicodeArray ', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending ', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArray ', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold ', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls ', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean).parse(this.argv); + }).option('--rotateUnicodeArray ', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean).option('--selfDefending ', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArray ', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean).option('--unicodeArrayThreshold ', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat).option('--wrapUnicodeArrayCalls ', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean).parse(this.rawArguments); commands.on('--help', function () { var isWindows = process.platform === 'win32', commandName = isWindows ? 'type' : 'cat'; console.log(' Examples:\n'); console.log(' %> javascript-obfuscator < in.js > out.js'); - console.log(" %> " + commandName + " in1.js in2.js | javascript-obfuscator > out.js"); + console.log(' %> ' + commandName + ' in1.js in2.js | javascript-obfuscator > out.js'); console.log(''); process.exit(); }); } }, { - key: "configureProcess", - value: function configureProcess() { - var _this = this; - - this.stdin.setEncoding(JavaScriptObfuscatorCLI.encoding); - this.stdin.on('readable', function () { - var chunk = void 0; - while (chunk = _this.stdin.read()) { - _this.data += chunk; - } - }); - this.stdin.on('end', function () { - return _this.processData(); - }); + key: 'getData', + value: function getData() { + this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding); } }, { - key: "isDataExist", - value: function isDataExist() { - return !process.env.__DIRECT__ && !this.stdin.isTTY; + key: 'getOutputPath', + value: function getOutputPath() { + var outputPath = commands.output; + if (outputPath) { + return outputPath; + } + return this.inputFilePath.split('.').map(function (value, index) { + return index === 0 ? value + '-obfuscated' : value; + }).join('.'); } }, { - key: "processData", + key: 'processData', value: function processData() { - this.stdout.write(JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions())); + fs.writeFileSync(this.getOutputPath(), JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()), { + encoding: JavaScriptObfuscatorCLI.encoding + }); } }], [{ - key: "buildOptions", + key: 'buildOptions', value: function buildOptions() { var options = {}, availableOptions = Object.keys(DefaultPreset_1.DEFAULT_PRESET); @@ -1153,14 +1154,21 @@ module.exports = return Object.assign({}, DefaultPreset_1.DEFAULT_PRESET, options); } }, { - key: "getBuildVersion", + key: 'checkFilePath', + value: function checkFilePath(filePath) { + if (!fs.existsSync(filePath)) { + throw new Error('Wrong input file `' + filePath + '`'); + } + } + }, { + key: 'getBuildVersion', value: function getBuildVersion() { - return child_process_1.execSync("npm info " + JavaScriptObfuscatorCLI.packageName + " version", { + return child_process_1.execSync('npm info ' + JavaScriptObfuscatorCLI.packageName + ' version', { encoding: JavaScriptObfuscatorCLI.encoding }); } }, { - key: "parseBoolean", + key: 'parseBoolean', value: function parseBoolean(value) { return value === 'true' || value === '1'; } @@ -2617,6 +2625,12 @@ module.exports = module.exports = require("commander"); +/***/ }, +/* 44 */ +/***/ function(module, exports) { + + module.exports = require("fs"); + /***/ } /******/ ]); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/package.json b/package.json index bca6defb..a7695808 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "test:dev": "node test/dev/test.js", "test:full": "npm run test:compile && node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- test-tmp/test/**/**.spec.js && npm run test:removeTestTmpDir", "test:coveralls": "npm run test:compile && node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- test-tmp/test/**/**.spec.js --report lcovonly && cat ./coverage/lcov.info | coveralls && npm run test:removeTestTmpDir", - "test:mocha": "npm run test:compile && node_modules/.bin/mocha test-tmp/test/**/**.spec.js && npm run test:removeTestTmpDir", + "test:mocha": "npm run test:compile && node_modules/.bin/mocha test-tmp/test/**/**.spec.js && npm run test:removeTestTmpDir", "test:removeTestTmpDir": "rm -rf test-tmp", "test": "npm run test:dev && npm run test:full", "tslint": "tslint src/**/*.ts", diff --git a/src/JavaScriptObfuscator.ts b/src/JavaScriptObfuscator.ts index bdbd2012..8992de18 100644 --- a/src/JavaScriptObfuscator.ts +++ b/src/JavaScriptObfuscator.ts @@ -36,15 +36,9 @@ export class JavaScriptObfuscator { /** * @param argv - * @param stdin - * @param stdout */ - public static runCLI ( - argv: string[], - stdin: NodeJS.ReadableStream, - stdout: NodeJS.WritableStream - ): void { - new JavaScriptObfuscatorCLI(argv, stdin, stdout).run(); + public static runCLI (argv: string[]): void { + new JavaScriptObfuscatorCLI(argv).run(); } /** diff --git a/src/cli/JavaScriptObfuscatorCLI.ts b/src/cli/JavaScriptObfuscatorCLI.ts index be6ceee5..080b3b90 100644 --- a/src/cli/JavaScriptObfuscatorCLI.ts +++ b/src/cli/JavaScriptObfuscatorCLI.ts @@ -1,5 +1,5 @@ import * as commands from 'commander'; -import * as tty from 'tty'; +import * as fs from 'fs'; import { execSync } from "child_process"; import { IOptionsPreset } from "../interfaces/IOptionsPreset"; @@ -22,7 +22,12 @@ export class JavaScriptObfuscatorCLI { /** * @type {string[]} */ - private argv: string[]; + private arguments: string[]; + + /** + * @type {string[]} + */ + private rawArguments: string[]; /** * @type {string} @@ -30,23 +35,14 @@ export class JavaScriptObfuscatorCLI { private data: string = ''; /** - * @type {NodeJS.ReadableStream} + * @type {string} */ - private stdin: NodeJS.ReadableStream; + private inputFilePath: string; - /** - * @type {NodeJS.WritableStream} - */ - private stdout: NodeJS.WritableStream; - - constructor ( - argv: string[], - stdin: NodeJS.ReadableStream, - stdout: NodeJS.WritableStream - ) { - this.argv = argv; - this.stdin = stdin; - this.stdout = stdout; + constructor (argv: string[]) { + this.rawArguments = argv; + this.arguments = this.rawArguments.slice(2); + this.inputFilePath = this.arguments[0]; } /** @@ -71,6 +67,15 @@ 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} */ @@ -89,18 +94,25 @@ export class JavaScriptObfuscatorCLI { } public run (): void { - this.configureProcess(); this.configureCommands(); - if (!this.isDataExist()) { + if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) { commands.outputHelp(); + + return; } + + JavaScriptObfuscatorCLI.checkFilePath(this.inputFilePath); + + this.getData(); + this.processData(); } private configureCommands (): void { commands .version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version') - .usage('[options] STDIN STDOUT') + .usage(' [options]') + .option('-o, --output ', 'Output path for obfuscated code') .option('--compact ', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean) .option('--debugProtection ', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean) .option('--debugProtectionInterval ', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean) @@ -112,7 +124,7 @@ export class JavaScriptObfuscatorCLI { .option('--unicodeArray ', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean) .option('--unicodeArrayThreshold ', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat) .option('--wrapUnicodeArrayCalls ', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean) - .parse(this.argv); + .parse(this.rawArguments); commands.on('--help', () => { let isWindows: boolean = process.platform === 'win32', @@ -127,30 +139,35 @@ export class JavaScriptObfuscatorCLI { }); } - private configureProcess (): void { - this.stdin.setEncoding(JavaScriptObfuscatorCLI.encoding); - - this.stdin.on('readable', () => { - let chunk: string; - - while (chunk = this.stdin.read()) { - this.data += chunk; - } - }); - - this.stdin.on('end', () => this.processData()); + private getData (): void { + this.data = fs.readFileSync(this.inputFilePath, JavaScriptObfuscatorCLI.encoding); } /** - * @returns {boolean} + * @returns {string} */ - private isDataExist (): boolean { - return !process.env.__DIRECT__ && !(this.stdin).isTTY; + private getOutputPath (): string { + let outputPath: string = (commands).output; + + if (outputPath) { + return outputPath; + } + + return this.inputFilePath + .split('.') + .map((value: string, index: number) => { + return index === 0 ? `${value}-obfuscated` : value; + }) + .join('.'); } private processData (): void { - this.stdout.write( - JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()) + fs.writeFileSync( + this.getOutputPath(), + JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()), + { + encoding: JavaScriptObfuscatorCLI.encoding + } ); } }