保誠-保戶業務員媒合平台
Tomas
2022-05-19 957a1f10a06fdbb76f1a0ba94fe44126c613fee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const chalk = require('chalk');
 
const pluginCompat = require('./util/plugin-compat');
 
const LOGGER_SEPARATOR = ':';
const DEFAULT_LOGGER_PREFIX = 'hardsource';
 
const messages = {
  'serialization--error-freezing-module': {
    short: value =>
      `Could not freeze ${value.data.moduleReadable}: ${
        value.data.errorMessage
      }`,
  },
  'serialzation--cache-incomplete': {
    short: value =>
      `Last compilation did not finish saving. Building new cache.`,
  },
  'confighash--directory-no-confighash': {
    short: value => `Config hash skipped in cache directory.`,
  },
  'confighash--new': {
    short: value =>
      `Writing new cache ${value.data.configHash.substring(0, 8)}...`,
  },
  'confighash--reused': {
    short: value =>
      `Reading from cache ${value.data.configHash.substring(0, 8)}...`,
  },
  'caches--delete-old': {
    short: value =>
      `Deleted ${value.data.deletedSizeMB} MB. Using ${
        value.data.sizeMB
      } MB of disk space.`,
  },
  'caches--keep': {
    short: value => `Using ${value.data.sizeMB} MB of disk space.`,
  },
  'environment--inputs': {
    short: value =>
      `Tracking node dependencies with: ${value.data.inputs.join(', ')}.`,
  },
  'environment--config-changed': {
    short: value => 'Configuration changed. Building new cache.',
  },
  'environment--changed': {
    short: value => `Node dependencies changed. Building new cache.`,
  },
  'environment--hardsource-changed': {
    short: value => `hard-source version changed. Building new cache.`,
  },
  'childcompiler--no-cache': {
    once: value =>
      `A child compiler has its cache disabled. Skipping child in hard-source.`,
  },
  'childcompiler--unnamed-cache': {
    once: value =>
      `A child compiler has unnamed cache. Skipping child in hard-source.`,
  },
  unrecognized: {
    short: value => value.message,
  },
};
 
const logLevels = ['error', 'warn', 'info', 'log', 'debug'];
 
const levelId = level => logLevels.indexOf(level.toLowerCase());
 
const compareLevel = (a, b) => levelId(a) - levelId(b);
 
class ChalkLoggerPlugin {
  constructor(options = {}) {
    this.options = options;
    this.once = {};
 
    // mode: 'test' or 'none'
    this.options.mode =
      this.options.mode || (process.env.NODE_ENV === 'test' ? 'test' : 'none');
    // level: 'error', 'warn', 'info', 'log', 'debug'
    this.options.level =
      this.options.level || (this.options.mode === 'test' ? 'warn' : 'debug');
  }
 
  apply(compiler) {
    const compilerHooks = pluginCompat.hooks(compiler);
 
    compilerHooks.hardSourceLog.tap('HardSource - ChalkLoggerPlugin', value => {
      if (compareLevel(this.options.level, value.level) < 0) {
        return;
      }
 
      let headerColor = chalk.white;
      let color = chalk.white;
      if (value.level === 'error') {
        headerColor = chalk.red;
      } else if (value.level === 'warn') {
        headerColor = chalk.yellow;
      } else if (value.level === 'info') {
        headerColor = chalk.white;
      } else {
        headerColor = color = chalk.gray;
      }
 
      const header = headerColor(
        `[${DEFAULT_LOGGER_PREFIX}${LOGGER_SEPARATOR}${compiler.__hardSource_shortConfigHash ||
          value.from}]`,
      );
 
      // Always use warn or error so that output goes to stderr.
      const consoleFn = value.level === 'error' ? console.error : console.warn;
 
      let handle = messages[value.data.id];
      if (!handle) {
        handle = messages.unrecognized;
      }
 
      if (handle) {
        if (handle.once) {
          if (!this.once[value.data.id]) {
            this.once[value.data.id] = true;
            consoleFn.call(console, header, color(handle.once(value)));
          }
        } else if (handle.short) {
          consoleFn.call(console, header, color(handle.short(value)));
        }
      } else {
        consoleFn.call(console, header, color(value.message));
      }
    });
  }
}
 
module.exports = ChalkLoggerPlugin;