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 diff --git a/index.js b/index.js index 149bd2a..cb85b05 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,22 @@ 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'); + } + return allFiles; }