mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Changed file log messages
This commit is contained in:
+9
-18
@@ -3,8 +3,6 @@ import acornImportMeta from 'acorn-import-meta';
|
||||
import * as ESTree from 'estree';
|
||||
import chalk, { Chalk } from 'chalk';
|
||||
|
||||
import { IASTParserFacadeInputData } from './interfaces/IASTParserFacadeInputData';
|
||||
|
||||
/**
|
||||
* Facade over AST parser `acorn`
|
||||
*/
|
||||
@@ -28,23 +26,23 @@ export class ASTParserFacade {
|
||||
];
|
||||
|
||||
/**
|
||||
* @param {string} inputData
|
||||
* @param {string} sourceCode
|
||||
* @param {Options} config
|
||||
* @returns {Program}
|
||||
*/
|
||||
public static parse (inputData: IASTParserFacadeInputData, config: acorn.Options): ESTree.Program | never {
|
||||
public static parse (sourceCode: string, config: acorn.Options): ESTree.Program | never {
|
||||
const sourceTypeLength: number = ASTParserFacade.sourceTypes.length;
|
||||
|
||||
for (let i: number = 0; i < sourceTypeLength; i++) {
|
||||
try {
|
||||
return ASTParserFacade.parseType(inputData, config, ASTParserFacade.sourceTypes[i]);
|
||||
return ASTParserFacade.parseType(sourceCode, config, ASTParserFacade.sourceTypes[i]);
|
||||
} catch (error) {
|
||||
if (i < sourceTypeLength - 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new Error(ASTParserFacade.processParsingError(
|
||||
inputData,
|
||||
sourceCode,
|
||||
error.message,
|
||||
error.loc
|
||||
));
|
||||
@@ -55,17 +53,16 @@ export class ASTParserFacade {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {IASTParserFacadeInputData} inputData
|
||||
* @param {string} sourceCode
|
||||
* @param {acorn.Options} inputConfig
|
||||
* @param {acorn.Options["sourceType"]} sourceType
|
||||
* @returns {Program}
|
||||
*/
|
||||
private static parseType (
|
||||
inputData: IASTParserFacadeInputData,
|
||||
sourceCode: string,
|
||||
inputConfig: acorn.Options,
|
||||
sourceType: acorn.Options['sourceType']
|
||||
): ESTree.Program {
|
||||
const { sourceCode } = inputData;
|
||||
const comments: ESTree.Comment[] = [];
|
||||
const config: acorn.Options = {
|
||||
...inputConfig,
|
||||
@@ -85,13 +82,13 @@ export class ASTParserFacade {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {IASTParserFacadeInputData} inputData
|
||||
* @param {string} sourceCode
|
||||
* @param {string} errorMessage
|
||||
* @param {Position | null} position
|
||||
* @returns {never}
|
||||
*/
|
||||
private static processParsingError (
|
||||
inputData: IASTParserFacadeInputData,
|
||||
sourceCode: string,
|
||||
errorMessage: string,
|
||||
position: ESTree.Position | null
|
||||
): never {
|
||||
@@ -99,8 +96,6 @@ export class ASTParserFacade {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
const { sourceCode, inputFilePath } = inputData;
|
||||
|
||||
const sourceCodeLines: string[] = sourceCode.split(/\r?\n/);
|
||||
const errorLine: string | undefined = sourceCodeLines[position.line - 1];
|
||||
|
||||
@@ -108,10 +103,6 @@ export class ASTParserFacade {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
const formattedInputFilePath: string = inputFilePath
|
||||
? `${inputFilePath}, `
|
||||
: '';
|
||||
|
||||
const startErrorIndex: number = Math.max(0, position.column - ASTParserFacade.nearestSymbolsCount);
|
||||
const endErrorIndex: number = Math.min(errorLine.length, position.column + ASTParserFacade.nearestSymbolsCount);
|
||||
|
||||
@@ -121,7 +112,7 @@ export class ASTParserFacade {
|
||||
}...`;
|
||||
|
||||
throw new Error(
|
||||
`ERROR in ${formattedInputFilePath}line ${position.line}: ${errorMessage}\n${formattedPointer} ${formattedCodeSlice}`
|
||||
`ERROR at line ${position.line}: ${errorMessage}\n${formattedPointer} ${formattedCodeSlice}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import * as ESTree from 'estree';
|
||||
|
||||
import { TObfuscatedCodeFactory } from './types/container/source-code/TObfuscatedCodeFactory';
|
||||
|
||||
import { IASTParserFacadeInputData } from './interfaces/IASTParserFacadeInputData';
|
||||
import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
|
||||
import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator';
|
||||
import { ILogger } from './interfaces/logger/ILogger';
|
||||
@@ -150,12 +149,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
* @returns {Program}
|
||||
*/
|
||||
private parseCode (sourceCode: string): ESTree.Program {
|
||||
const inputData: IASTParserFacadeInputData = {
|
||||
sourceCode,
|
||||
inputFilePath: this.options.inputFilePath
|
||||
};
|
||||
|
||||
return ASTParserFacade.parse(inputData, JavaScriptObfuscator.parseOptions);
|
||||
return ASTParserFacade.parse(sourceCode, JavaScriptObfuscator.parseOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,9 +21,11 @@ import { StringArrayEncodingSanitizer } from './sanitizers/StringArrayEncodingSa
|
||||
|
||||
import { CLIUtils } from './utils/CLIUtils';
|
||||
import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
|
||||
import { Logger } from '../logger/Logger';
|
||||
import { ObfuscatedCodeWriter } from './utils/ObfuscatedCodeWriter';
|
||||
import { SourceCodeReader } from './utils/SourceCodeReader';
|
||||
import { Utils } from '../utils/Utils';
|
||||
import { LoggingPrefix } from '../enums/logger/LoggingPrefix';
|
||||
|
||||
export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
/**
|
||||
@@ -103,14 +105,12 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
|
||||
const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
|
||||
const inputFileName: string = path.basename(inputCodePath);
|
||||
const inputFilePath: string = inputCodePath;
|
||||
|
||||
return {
|
||||
...DEFAULT_PRESET,
|
||||
...configFileOptions,
|
||||
...inputCLIOptions,
|
||||
inputFileName,
|
||||
inputFilePath
|
||||
inputFileName
|
||||
};
|
||||
}
|
||||
|
||||
@@ -367,7 +367,23 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
sourceCodeData.forEach(({ filePath, content }: IFileData, index: number) => {
|
||||
const outputCodePath: string = this.obfuscatedCodeWriter.getOutputCodePath(filePath);
|
||||
|
||||
this.processSourceCode(content, filePath, outputCodePath, index);
|
||||
try {
|
||||
Logger.log(
|
||||
Logger.colorInfo,
|
||||
LoggingPrefix.CLI,
|
||||
`Obfuscating file: ${filePath}...`
|
||||
);
|
||||
|
||||
this.processSourceCode(content, filePath, outputCodePath, index);
|
||||
} catch (error) {
|
||||
Logger.log(
|
||||
Logger.colorInfo,
|
||||
LoggingPrefix.CLI,
|
||||
`Error in file: ${filePath}...`
|
||||
);
|
||||
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,7 @@ import { TInputCLIOptions } from '../../types/options/TInputCLIOptions';
|
||||
|
||||
import { IFileData } from '../../interfaces/cli/IFileData';
|
||||
|
||||
import { LoggingPrefix } from '../../enums/logger/LoggingPrefix';
|
||||
|
||||
import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
|
||||
import { Logger } from '../../logger/Logger';
|
||||
|
||||
export class SourceCodeReader {
|
||||
/**
|
||||
@@ -104,28 +101,13 @@ export class SourceCodeReader {
|
||||
&& !SourceCodeReader.isExcludedPath(filePath, excludePatterns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
*/
|
||||
private static logFilePath (filePath: string): void {
|
||||
const normalizedFilePath: string = path.normalize(filePath);
|
||||
|
||||
Logger.log(
|
||||
Logger.colorInfo,
|
||||
LoggingPrefix.CLI,
|
||||
`Obfuscating file: ${normalizedFilePath}...`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @returns {string}
|
||||
*/
|
||||
private static readFile (filePath: string): IFileData {
|
||||
SourceCodeReader.logFilePath(filePath);
|
||||
|
||||
return {
|
||||
filePath,
|
||||
filePath: path.normalize(filePath),
|
||||
content: fs.readFileSync(filePath, JavaScriptObfuscatorCLI.encoding)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
export interface IASTParserFacadeInputData {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
sourceCode: string;
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
inputFilePath?: string;
|
||||
}
|
||||
@@ -20,7 +20,6 @@ export interface IOptions {
|
||||
readonly identifiersDictionary: string[];
|
||||
readonly identifiersPrefix: string;
|
||||
readonly inputFileName: string;
|
||||
readonly inputFilePath: string;
|
||||
readonly log: boolean;
|
||||
readonly renameGlobals: boolean;
|
||||
readonly reservedNames: string[];
|
||||
|
||||
@@ -36,7 +36,7 @@ export class NodeUtils {
|
||||
*/
|
||||
public static convertCodeToStructure (code: string): ESTree.Statement[] {
|
||||
const structure: ESTree.Program = ASTParserFacade.parse(
|
||||
{ sourceCode: code },
|
||||
code,
|
||||
{
|
||||
ecmaVersion,
|
||||
sourceType: 'script'
|
||||
|
||||
@@ -144,12 +144,6 @@ export class Options implements IOptions {
|
||||
@IsString()
|
||||
public readonly inputFileName!: string;
|
||||
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
@IsString()
|
||||
public readonly inputFilePath!: string;
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,6 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
|
||||
identifiersPrefix: '',
|
||||
identifiersDictionary: [],
|
||||
inputFileName: '',
|
||||
inputFilePath: '',
|
||||
log: false,
|
||||
renameGlobals: false,
|
||||
reservedNames: [],
|
||||
|
||||
@@ -19,7 +19,6 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
|
||||
identifiersPrefix: '',
|
||||
identifiersDictionary: [],
|
||||
inputFileName: '',
|
||||
inputFilePath: '',
|
||||
log: false,
|
||||
renameGlobals: false,
|
||||
reservedNames: [],
|
||||
|
||||
Reference in New Issue
Block a user