Browse Source

Added logging of file name during CLI obfuscation

sanex3339 7 years ago
parent
commit
9b475eb795

File diff suppressed because it is too large
+ 0 - 0
dist/index.js


+ 24 - 1
src/cli/utils/SourceCodeReader.ts

@@ -1,6 +1,8 @@
 import * as fs from 'fs';
 import * as path from 'path';
 
+import chalk, { Chalk } from 'chalk';
+
 import { TSourceCodeData } from '../../types/cli/TSourceCodeData';
 
 import { IFileData } from '../../interfaces/cli/IFileData';
@@ -8,6 +10,16 @@ import { IFileData } from '../../interfaces/cli/IFileData';
 import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
 
 export class SourceCodeReader {
+    /**
+     * @type {Chalk}
+     */
+    private static readonly colorInfo: Chalk = chalk.cyan;
+
+    /**
+     * @type {string}
+     */
+    private static readonly loggingPrefix: string = '[javascript-obfuscator-cli]';
+
     /**
      * @param {string} inputPath
      * @returns {TSourceCodeData}
@@ -63,7 +75,7 @@ export class SourceCodeReader {
                         ...SourceCodeReader.readDirectoryRecursive(filePath)
                     );
                 } else if (SourceCodeReader.isFilePath(filePath) && SourceCodeReader.isValidFile(fileName)) {
-                    const content: string = fs.readFileSync(filePath, JavaScriptObfuscatorCLI.encoding);
+                    const content: string = SourceCodeReader.readFile(filePath);
 
                     fileData.push({ filePath, content });
                 }
@@ -81,6 +93,8 @@ export class SourceCodeReader {
             throw new ReferenceError(`Input file must have .js extension`);
         }
 
+        SourceCodeReader.logFilePath(`Obfuscating file: ${filePath}...`);
+
         return fs.readFileSync(filePath, JavaScriptObfuscatorCLI.encoding);
     }
 
@@ -92,4 +106,13 @@ export class SourceCodeReader {
         return JavaScriptObfuscatorCLI.availableInputExtensions.includes(path.extname(filePath))
             && !filePath.includes(JavaScriptObfuscatorCLI.obfuscatedFilePrefix);
     }
+
+    /**
+     * @param {string} filePath
+     */
+    private static logFilePath (filePath: string): void {
+        const normalizedFilePath: string = path.normalize(filePath);
+
+        console.log(SourceCodeReader.colorInfo(`\n${SourceCodeReader.loggingPrefix} ${normalizedFilePath}`));
+    }
 }

+ 38 - 1
test/unit-tests/cli/utils/SourceCodeReader.spec.ts

@@ -3,10 +3,12 @@ import * as mkdirp from 'mkdirp';
 import * as rimraf from 'rimraf';
 
 import { assert } from 'chai';
+import * as sinon from 'sinon';
 
-import { SourceCodeReader } from '../../../../src/cli/utils/SourceCodeReader';
 import { IFileData } from '../../../../src/interfaces/cli/IFileData';
 
+import { SourceCodeReader } from '../../../../src/cli/utils/SourceCodeReader';
+
 describe('SourceCodeReader', () => {
     const fileContent: string = 'test';
     const tmpDirectoryPath: string = 'test/tmp';
@@ -190,6 +192,41 @@ describe('SourceCodeReader', () => {
                 });
             });
         });
+
+        describe('Variant #3: logging', () => {
+            const tmpFileName: string = 'test.js';
+            const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
+            const expectedConsoleLogCallResult: boolean = true;
+            const expectedLoggingMessage: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputPath}...`;
+
+            let consoleLogSpy: sinon.SinonSpy,
+                consoleLogCallResult: boolean,
+                loggingMessageResult: string;
+
+            before(() => {
+                consoleLogSpy = sinon.spy(console, 'log');
+
+                fs.writeFileSync(inputPath, fileContent);
+                SourceCodeReader.readSourceCode(inputPath);
+
+                consoleLogCallResult = consoleLogSpy.called;
+                loggingMessageResult = consoleLogSpy.getCall(0).args[0];
+            });
+
+            it('should call `console.log`', () => {
+                assert.equal(consoleLogCallResult, expectedConsoleLogCallResult);
+            });
+
+            it('should log file name to the console', () => {
+                assert.include(loggingMessageResult, expectedLoggingMessage);
+            });
+
+
+            after(() => {
+                consoleLogSpy.restore();
+                fs.unlinkSync(inputPath);
+            });
+        });
     });
 
     after(() => {

Some files were not shown because too many files changed in this diff