From 715cefa92b1140ed8dfa9c24786930c917073d9e Mon Sep 17 00:00:00 2001 From: Teng-hao Chang Date: Fri, 31 Jan 2020 15:07:36 +0800 Subject: [PATCH 1/3] Add 'outputFilePath' option to export results in Markdown format --- index.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 149bd2a..ea6809e 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const path = require('path'); const chalk = require('chalk'); const { searchFiles } = require('./lib/utils'); @@ -8,6 +9,7 @@ function UnusedPlugin(options) { this.root = options.root; this.failOnUnused = options.failOnUnused || false; this.useGitIgnore = options.useGitIgnore || true; + this.outputFilePath = options.outputFilePath; } UnusedPlugin.prototype.apply = function apply(compiler) { @@ -55,6 +57,11 @@ function display(filesByDirectory) { if (!allFiles.length) { return []; } + + let outputString = `## Unused Files +${allFiles.length} unused source files found. +`; + process.stdout.write('\n'); process.stdout.write(chalk.green('\n*** Unused Plugin ***\n')); process.stdout.write( @@ -66,12 +73,27 @@ function display(filesByDirectory) { const relative = this.root ? path.relative(this.root, directory) : directory; + process.stdout.write(chalk.blue(`\n● ${relative}\n`)); - files.forEach(file => process.stdout.write( - chalk.yellow(` • ${path.relative(directory, file)}\n`), - )); + outputString += `\n### ${relative}\n`; + + files.forEach(file => { + process.stdout.write( + chalk.yellow(` • ${path.relative(directory, file)}\n`), + ) + outputString += ` - ${path.relative(directory, file)}\n`; + }); }); process.stdout.write(chalk.green('\n*** Unused Plugin ***\n\n')); + if (this.outputFilePath) { + fs.writeFile( + this.outputFilePath, + outputString, + 'utf8', + err => console.warn(err) + ); + } + return allFiles; } From 96d512ceccca372fa9e0e58c5067f5d9a54538f6 Mon Sep 17 00:00:00 2001 From: Teng-hao Chang Date: Fri, 31 Jan 2020 15:09:28 +0800 Subject: [PATCH 2/3] Update README about new 'outputFilePath' option --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6523154..aca1e4c 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ module.exports = { exclude: ['*.test.js'], // Root directory (optional) root: __dirname, + // Export file path (optional) + outputFilePath: path.resolve(__dirname, 'UNUSED_FILES.MD'), }), ], }; @@ -39,6 +41,7 @@ module.exports = { - `root` : root directory that will be use to display relative paths instead of absolute ones (see below) - `failOnUnused`: whether or not the build should fail if unused files are found (defaults to `false`) - `useGitIgnore`: whether or not to respect `.gitignore` file (defaults to `true`) +- `outputFilePath`: Full path to the file where you want the results be printed to. With root From 33b1417e143c376cf729ff73b06efccae84bfaad Mon Sep 17 00:00:00 2001 From: Teng-hao Chang Date: Mon, 10 Feb 2020 11:08:48 +0800 Subject: [PATCH 3/3] Fix linter issues --- index.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index ea6809e..cb85b05 100644 --- a/index.js +++ b/index.js @@ -77,22 +77,17 @@ ${allFiles.length} unused source files found. process.stdout.write(chalk.blue(`\n● ${relative}\n`)); outputString += `\n### ${relative}\n`; - files.forEach(file => { + files.forEach((file) => { process.stdout.write( chalk.yellow(` • ${path.relative(directory, file)}\n`), - ) + ); outputString += ` - ${path.relative(directory, file)}\n`; }); }); process.stdout.write(chalk.green('\n*** Unused Plugin ***\n\n')); if (this.outputFilePath) { - fs.writeFile( - this.outputFilePath, - outputString, - 'utf8', - err => console.warn(err) - ); + fs.writeFile(this.outputFilePath, outputString, 'utf8'); } return allFiles;