[6a3a178] | 1 | /*
|
---|
| 2 | * MIT License http://opensource.org/licenses/MIT
|
---|
| 3 | * Author: Ben Holloway @bholloway
|
---|
| 4 | */
|
---|
| 5 | 'use strict';
|
---|
| 6 |
|
---|
| 7 | const path = require('path');
|
---|
| 8 |
|
---|
| 9 | const PACKAGE_NAME = require('../../package.json').name;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Paths are formatted to have posix style path separators and those within the CWD are made relative to CWD.
|
---|
| 13 | *
|
---|
| 14 | * @param {string} absolutePath An absolute path to format
|
---|
| 15 | * @returns {string} the formatted path
|
---|
| 16 | */
|
---|
| 17 | const pathToString = (absolutePath) => {
|
---|
| 18 | if (absolutePath === '') {
|
---|
| 19 | return '-empty-';
|
---|
| 20 | } else {
|
---|
| 21 | const relative = path.relative(process.cwd(), absolutePath).split(path.sep);
|
---|
| 22 | const segments =
|
---|
| 23 | (relative[0] !== '..') ? ['.'].concat(relative).filter(Boolean) :
|
---|
| 24 | (relative.lastIndexOf('..') < 2) ? relative :
|
---|
| 25 | absolutePath.split(path.sep);
|
---|
| 26 | return segments.join('/');
|
---|
| 27 | }
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | exports.pathToString = pathToString;
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Format a debug message.
|
---|
| 34 | *
|
---|
| 35 | * @param {string} filename The file being processed by webpack
|
---|
| 36 | * @param {string} uri A uri path, relative or absolute
|
---|
| 37 | * @param {Array<{base:string,joined:string,isSuccess:boolean}>} attempts An array of attempts, possibly empty
|
---|
| 38 | * @return {string} Formatted message
|
---|
| 39 | */
|
---|
| 40 | const formatJoinMessage = (filename, uri, attempts) => {
|
---|
| 41 | const attemptToCells = (_, i, array) => {
|
---|
| 42 | const { base: prev } = (i === 0) ? {} : array[i-1];
|
---|
| 43 | const { base: curr, joined } = array[i];
|
---|
| 44 | return [(curr === prev) ? '' : pathToString(curr), pathToString(joined)];
|
---|
| 45 | };
|
---|
| 46 |
|
---|
| 47 | const formatCells = (lines) => {
|
---|
| 48 | const maxWidth = lines.reduce((max, [cellA]) => Math.max(max, cellA.length), 0);
|
---|
| 49 | return lines.map(([cellA, cellB]) => [cellA.padEnd(maxWidth), cellB]).map((cells) => cells.join(' --> '));
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | return [PACKAGE_NAME + ': ' + pathToString(filename) + ': ' + uri]
|
---|
| 53 | .concat(attempts.length === 0 ? '-empty-' : formatCells(attempts.map(attemptToCells)))
|
---|
| 54 | .concat(attempts.some(({ isSuccess }) => isSuccess) ? 'FOUND' : 'NOT FOUND')
|
---|
| 55 | .join('\n ');
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | exports.formatJoinMessage = formatJoinMessage;
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * A factory for a log function predicated on the given debug parameter.
|
---|
| 62 | *
|
---|
| 63 | * The logging function created accepts a function that formats a message and parameters that the function utilises.
|
---|
| 64 | * Presuming the message function may be expensive we only call it if logging is enabled.
|
---|
| 65 | *
|
---|
| 66 | * The log messages are de-duplicated based on the parameters, so it is assumed they are simple types that stringify
|
---|
| 67 | * well.
|
---|
| 68 | *
|
---|
| 69 | * @param {function|boolean} debug A boolean or debug function
|
---|
| 70 | * @return {function(function, array):void} A logging function possibly degenerate
|
---|
| 71 | */
|
---|
| 72 | const createDebugLogger = (debug) => {
|
---|
| 73 | const log = !!debug && ((typeof debug === 'function') ? debug : console.log);
|
---|
| 74 | const cache = {};
|
---|
| 75 | return log ?
|
---|
| 76 | ((msgFn, params) => {
|
---|
| 77 | const key = Function.prototype.toString.call(msgFn) + JSON.stringify(params);
|
---|
| 78 | if (!cache[key]) {
|
---|
| 79 | cache[key] = true;
|
---|
| 80 | log(msgFn.apply(null, params));
|
---|
| 81 | }
|
---|
| 82 | }) :
|
---|
| 83 | (() => undefined);
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | exports.createDebugLogger = createDebugLogger;
|
---|