浏览代码

Now Logger has public static `log` method

sanex3339 7 年之前
父节点
当前提交
b9815fc9e7
共有 4 个文件被更改,包括 45 次插入50 次删除
  1. 0 0
      dist/index.js
  2. 9 14
      src/cli/utils/SourceCodeReader.ts
  3. 4 0
      src/enums/logger/LoggingPrefix.ts
  4. 32 36
      src/logger/Logger.ts

文件差异内容过多而无法显示
+ 0 - 0
dist/index.js


+ 9 - 14
src/cli/utils/SourceCodeReader.ts

@@ -1,25 +1,16 @@
 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';
 
+import { LoggingPrefix } from '../../enums/logger/LoggingPrefix';
+
 import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
+import { Logger } from '../../logger/Logger';
 
 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}
@@ -93,7 +84,7 @@ export class SourceCodeReader {
             throw new ReferenceError(`Input file must have .js extension`);
         }
 
-        SourceCodeReader.logFilePath(`Obfuscating file: ${filePath}...`);
+        SourceCodeReader.logFilePath(filePath);
 
         return fs.readFileSync(filePath, JavaScriptObfuscatorCLI.encoding);
     }
@@ -113,6 +104,10 @@ export class SourceCodeReader {
     private static logFilePath (filePath: string): void {
         const normalizedFilePath: string = path.normalize(filePath);
 
-        console.log(SourceCodeReader.colorInfo(`\n${SourceCodeReader.loggingPrefix} ${normalizedFilePath}`));
+        Logger.log(
+            Logger.colorInfo,
+            LoggingPrefix.CLI,
+            `Obfuscating file: ${normalizedFilePath}...`
+        );
     }
 }

+ 4 - 0
src/enums/logger/LoggingPrefix.ts

@@ -0,0 +1,4 @@
+export enum LoggingPrefix {
+    Base = '[javascript-obfuscator]',
+    CLI = '[javascript-obfuscator-cli]'
+}

+ 32 - 36
src/logger/Logger.ts

@@ -1,40 +1,30 @@
-import { inject, injectable, postConstruct } from 'inversify';
+import { inject, injectable } from 'inversify';
 import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
 
 import chalk, { Chalk } from 'chalk';
 
-import { IInitializable } from '../interfaces/IInitializable';
 import { ILogger } from '../interfaces/logger/ILogger';
 import { IOptions } from '../interfaces/options/IOptions';
 
 import { LoggingMessage } from '../enums/logger/LoggingMessage';
-
-import { initializable } from '../decorators/Initializable';
+import { LoggingPrefix } from '../enums/logger/LoggingPrefix';
 
 @injectable()
-export class Logger implements ILogger, IInitializable {
-    /**
-     * @type {string}
-     */
-    private static readonly loggingPrefix: string = '[javascript-obfuscator]';
-
+export class Logger implements ILogger {
     /**
      * @type {Chalk}
      */
-    @initializable()
-    private colorInfo!: Chalk;
+    public static readonly colorInfo: Chalk = chalk.cyan;
 
     /**
      * @type {Chalk}
      */
-    @initializable()
-    private colorSuccess!: Chalk;
+    public static readonly colorSuccess: Chalk = chalk.green;
 
     /**
      * @type {Chalk}
      */
-    @initializable()
-    private colorWarn!: Chalk;
+    public static readonly colorWarn: Chalk = chalk.yellow;
 
     /**
      * @type {IOptions}
@@ -50,50 +40,56 @@ export class Logger implements ILogger, IInitializable {
         this.options = options;
     }
 
-    @postConstruct()
-    public initialize (): void {
-        this.colorInfo = chalk.cyan;
-        this.colorSuccess = chalk.green;
-        this.colorWarn = chalk.yellow;
-    }
-
     /**
-     * @param {LoggingMessage} loggingMessage
+     * @param {Chalk} loggingLevelColor
+     * @param {LoggingPrefix} loggingPrefix
+     * @param {string} loggingMessage
      * @param {string | number} value
      */
-    public info (loggingMessage: LoggingMessage, value?: string | number): void {
-        this.log(this.colorInfo, loggingMessage, value);
+    public static log (
+        loggingLevelColor: Chalk,
+        loggingPrefix: LoggingPrefix,
+        loggingMessage: string,
+        value?: string | number,
+    ): void {
+        const processedMessage: string = loggingLevelColor(`\n${loggingPrefix} ${loggingMessage}`);
+
+        !value ? console.log(processedMessage) : console.log(processedMessage, value);
     }
 
     /**
      * @param {LoggingMessage} loggingMessage
      * @param {string | number} value
      */
-    public success (loggingMessage: LoggingMessage, value?: string | number): void {
-        this.log(this.colorSuccess, loggingMessage, value);
+    public info (loggingMessage: LoggingMessage, value?: string | number): void {
+        if (!this.options.log) {
+            return;
+        }
+
+        Logger.log(Logger.colorInfo, LoggingPrefix.Base, loggingMessage, value);
     }
 
     /**
      * @param {LoggingMessage} loggingMessage
      * @param {string | number} value
      */
-    public warn (loggingMessage: LoggingMessage, value?: string | number): void {
-        this.log(this.colorWarn, loggingMessage, value);
+    public success (loggingMessage: LoggingMessage, value?: string | number): void {
+        if (!this.options.log) {
+            return;
+        }
+
+        Logger.log(Logger.colorSuccess, LoggingPrefix.Base, loggingMessage, value);
     }
 
     /**
-     *
-     * @param {Chalk} loggingLevelColor
      * @param {LoggingMessage} loggingMessage
      * @param {string | number} value
      */
-    private log (loggingLevelColor: Chalk, loggingMessage: LoggingMessage, value?: string | number): void {
+    public warn (loggingMessage: LoggingMessage, value?: string | number): void {
         if (!this.options.log) {
             return;
         }
 
-        const processedMessage: string = loggingLevelColor(`\n${Logger.loggingPrefix} ${loggingMessage}`);
-
-        !value ? console.log(processedMessage) : console.log(processedMessage, value);
+        Logger.log(Logger.colorWarn, LoggingPrefix.Base, loggingMessage, value);
     }
 }

部分文件因为文件数量过多而无法显示