Added logging of file name during CLI obfuscation

This commit is contained in:
sanex3339
2018-02-19 23:58:12 +03:00
parent a3f9835876
commit 9b475eb795
3 changed files with 63 additions and 3 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+24 -1
View File
@@ -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}`));
}
}
@@ -3,9 +3,11 @@ import * as mkdirp from 'mkdirp';
import * as rimraf from 'rimraf';
import { assert } from 'chai';
import * as sinon from 'sinon';
import { IFileData } from '../../../../src/interfaces/cli/IFileData';
import { SourceCodeReader } from '../../../../src/cli/utils/SourceCodeReader';
import { IFileData } from '../../../../src/interfaces/cli/IFileData';
describe('SourceCodeReader', () => {
const fileContent: string = 'test';
@@ -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(() => {