From 7074213c51f25c74802d27ea07d24cafb37165dd Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 15 Aug 2020 15:36:14 +0530 Subject: [PATCH 01/12] refactor: use logger --- packages/migrate/package.json | 1 + packages/migrate/src/index.ts | 25 ++++++++++--------- packages/webpack-cli/lib/groups/BasicGroup.js | 4 +-- packages/webpack-cli/lib/utils/logger.js | 3 ++- test/migrate/config/migrate-config.test.js | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/packages/migrate/package.json b/packages/migrate/package.json index 344d77cb861..41adf2fc9a3 100644 --- a/packages/migrate/package.json +++ b/packages/migrate/package.json @@ -20,6 +20,7 @@ "p-lazy": "3.0.0" }, "peerDependencies": { + "webpack-cli": "4.x.x", "webpack": "4.x.x || 5.x.x" }, "devDependencies": { diff --git a/packages/migrate/src/index.ts b/packages/migrate/src/index.ts index 279ad45176c..0b74593f461 100644 --- a/packages/migrate/src/index.ts +++ b/packages/migrate/src/index.ts @@ -2,6 +2,7 @@ import { green, red } from 'colorette'; import { Change, diffLines } from 'diff'; import fs from 'fs'; import inquirer from 'inquirer'; +import logger from 'webpack-cli/lib/utils/logger'; import Listr from 'listr'; import pLazy = require('p-lazy'); import path from 'path'; @@ -92,9 +93,9 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom diffOutput.forEach((diffLine: Change): void => { if (diffLine.added) { - process.stdout.write(green(`+ ${diffLine.value}`)); + console.log(green(`+ ${diffLine.value}`)); } else if (diffLine.removed) { - process.stdout.write(red(`- ${diffLine.value}`)); + console.log(red(`- ${diffLine.value}`)); } }); @@ -121,7 +122,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom }, ]); } else { - console.error(red('✖ Migration aborted')); + logger.error('✖ Migration aborted'); } }, ) @@ -138,24 +139,24 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom // eslint-disable-next-line @typescript-eslint/no-explicit-any const webpackOptionsValidationErrors: any = validate(outputConfig); if (webpackOptionsValidationErrors.length) { - console.error(red("\n✖ Your configuration validation wasn't successful \n")); + logger.error("\n✖ Your configuration validation wasn't successful \n"); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore - console.error(new WebpackOptionsValidationError(webpackOptionsValidationErrors)); + logger.error(new WebpackOptionsValidationError(webpackOptionsValidationErrors)); } } - console.info(green(`\n✔︎ New webpack config file is at ${outputConfigPath}.`)); - console.info(green('✔︎ Heads up! Updating to the latest version could contain breaking changes.')); + logger.success(`\n✔︎ New webpack config file is at ${outputConfigPath}.`); + logger.success('✔︎ Heads up! Updating to the latest version could contain breaking changes.'); - console.info(green('✔︎ Plugin and loader dependencies may need to be updated.')); + logger.success('✔︎ Plugin and loader dependencies may need to be updated.'); }, ); }) .catch((err: object): void => { const errMsg = '\n ✖ ︎Migration aborted due to some errors: \n'; - console.error(red(errMsg)); - console.error(err); + logger.error(errMsg); + logger.error(err); process.exitCode = 1; }); } @@ -175,7 +176,7 @@ export default function migrate(...args: string[]): void | Promise { const filePaths = args; if (!filePaths.length) { const errMsg = '\n ✖ Please specify a path to your webpack config \n '; - console.error(red(errMsg)); + logger.error(errMsg); return; } @@ -194,7 +195,7 @@ export default function migrate(...args: string[]): void | Promise { ]) .then((ans: { confirmPath: boolean }): void | Promise => { if (!ans.confirmPath) { - console.error(red('✖ ︎Migration aborted due to no output path')); + logger.error('✖ ︎Migration aborted due to no output path'); return; } outputConfigPath = path.resolve(process.cwd(), filePaths[0]); diff --git a/packages/webpack-cli/lib/groups/BasicGroup.js b/packages/webpack-cli/lib/groups/BasicGroup.js index 588c3758726..cc99b8f8cc3 100644 --- a/packages/webpack-cli/lib/groups/BasicGroup.js +++ b/packages/webpack-cli/lib/groups/BasicGroup.js @@ -1,5 +1,5 @@ const GroupHelper = require('../utils/GroupHelper'); -const { red } = require('colorette'); +const logger = require('../utils/logger'); const { core, groups } = require('../utils/cli-flags'); class BasicGroup extends GroupHelper { @@ -39,7 +39,7 @@ class BasicGroup extends GroupHelper { if (arg === 'entry') { options[arg] = this.resolveFilePath(args[arg], 'index.js'); if (options[arg].length === 0) { - process.stdout.write(red('\nError: you provided an invalid entry point.\n')); + logger.error('\nError: you provided an invalid entry point.\n'); } } }); diff --git a/packages/webpack-cli/lib/utils/logger.js b/packages/webpack-cli/lib/utils/logger.js index c0c8e61cbdb..d2e90e04a81 100644 --- a/packages/webpack-cli/lib/utils/logger.js +++ b/packages/webpack-cli/lib/utils/logger.js @@ -1,9 +1,10 @@ -const { red, cyan, yellow } = require('colorette'); +const { red, cyan, yellow, green } = require('colorette'); module.exports = { error: (val) => console.error(`[webpack-cli] ${red(val)}`), warn: (val) => console.warn(`[webpack-cli] ${yellow(val)}`), info: (val) => console.info(`[webpack-cli] ${cyan(val)}`), + success: (val) => console.log(`[webpack-cli] ${green(val)}`), log: (val) => console.log(`[webpack-cli] ${val}`), help: (val) => console.log(val), }; diff --git a/test/migrate/config/migrate-config.test.js b/test/migrate/config/migrate-config.test.js index f657e7e9cb9..e364bd5bb08 100644 --- a/test/migrate/config/migrate-config.test.js +++ b/test/migrate/config/migrate-config.test.js @@ -32,7 +32,7 @@ describe('migrate command', () => { it('should throw an error if the user refused to overwrite the source file and no output path is provided', async () => { const { stderr } = await runAndGetWatchProc(__dirname, ['migrate', 'webpack.config.js'], false, 'n'); - expect(stderr).toBe(red('✖ ︎Migration aborted due to no output path')); + expect(stderr).toContain(`[webpack-cli] ${red('✖ ︎Migration aborted due to no output path')}`); }); it('should prompt for config validation when an output path is provided', async () => { From dfe4ae4d41cea4d9bf3d2627e9b2292df734a3e3 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Mon, 17 Aug 2020 23:20:32 +0530 Subject: [PATCH 02/12] fix: ci --- packages/webpack-cli/lib/groups/BasicGroup.js | 2 +- test/entry/flag-entry/entry-with-flag.test.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/webpack-cli/lib/groups/BasicGroup.js b/packages/webpack-cli/lib/groups/BasicGroup.js index cc99b8f8cc3..27361bbe289 100644 --- a/packages/webpack-cli/lib/groups/BasicGroup.js +++ b/packages/webpack-cli/lib/groups/BasicGroup.js @@ -39,7 +39,7 @@ class BasicGroup extends GroupHelper { if (arg === 'entry') { options[arg] = this.resolveFilePath(args[arg], 'index.js'); if (options[arg].length === 0) { - logger.error('\nError: you provided an invalid entry point.\n'); + logger.error('Error: you provided an invalid entry point.'); } } }); diff --git a/test/entry/flag-entry/entry-with-flag.test.js b/test/entry/flag-entry/entry-with-flag.test.js index e4c7ab02b66..d667c006021 100644 --- a/test/entry/flag-entry/entry-with-flag.test.js +++ b/test/entry/flag-entry/entry-with-flag.test.js @@ -40,8 +40,7 @@ describe('entry flag', () => { }); it('should throw error for invalid entry file', () => { - const { stderr, stdout } = run(__dirname, ['--entry', './src/test.js']); + const { stderr } = run(__dirname, ['--entry', './src/test.js']); expect(stderr).toBeTruthy(); - expect(stdout).toContain('Error: you provided an invalid entry point.'); }); }); From 45451cb9459fe5ce6fca5bc08367625d852f6ada Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 19 Aug 2020 11:28:31 +0530 Subject: [PATCH 03/12] refactor(utils): use logger --- packages/utils/__tests__/run-prettier.test.ts | 9 ++++----- packages/utils/package.json | 3 ++- packages/utils/src/run-prettier.ts | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/utils/__tests__/run-prettier.test.ts b/packages/utils/__tests__/run-prettier.test.ts index d39b3cfb774..8a04fa6ab3a 100644 --- a/packages/utils/__tests__/run-prettier.test.ts +++ b/packages/utils/__tests__/run-prettier.test.ts @@ -8,13 +8,12 @@ import { runPrettier } from '../src/run-prettier'; const outputPath = path.join(__dirname, 'test-assets'); const outputFile = path.join(outputPath, 'test.js'); -const stdoutSpy = jest.spyOn(process.stdout, 'write'); +const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(); describe('runPrettier', () => { beforeEach(() => { rimraf.sync(outputPath); fs.mkdirSync(outputPath); - stdoutSpy.mockClear(); }); afterAll(() => { @@ -27,7 +26,7 @@ describe('runPrettier', () => { const data = fs.readFileSync(outputFile, 'utf8'); expect(data).toContain("console.log('1');\n"); - expect(stdoutSpy.mock.calls.length).toEqual(0); + expect(consoleSpy).toHaveBeenCalledTimes(0); }); it('prettier should fail on invalid JS, with file still written', () => { @@ -36,7 +35,7 @@ describe('runPrettier', () => { const data = fs.readFileSync(outputFile, 'utf8'); expect(data).toContain('"'); - expect(stdoutSpy.mock.calls.length).toEqual(1); - expect(stdoutSpy.mock.calls[0][0]).toContain('WARNING: Could not apply prettier'); + expect(consoleSpy).toHaveBeenCalledTimes(1); + expect(consoleSpy.mock.calls[0][0]).toContain('WARNING: Could not apply prettier'); }); }); diff --git a/packages/utils/package.json b/packages/utils/package.json index 3d7f268a869..fd391605a2d 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -21,7 +21,8 @@ "yeoman-generator": "4.7.2" }, "peerDependencies": { - "webpack": "4.x.x || 5.x.x" + "webpack": "4.x.x || 5.x.x", + "webpack-cli": "4.x.x || 5.x.x" }, "devDependencies": { "@types/got": "9.6.9", diff --git a/packages/utils/src/run-prettier.ts b/packages/utils/src/run-prettier.ts index 13523d47659..cb7ac89aa39 100644 --- a/packages/utils/src/run-prettier.ts +++ b/packages/utils/src/run-prettier.ts @@ -2,6 +2,8 @@ import { yellow } from 'colorette'; import fs from 'fs'; import prettier from 'prettier'; +import logger from 'webpack-cli/lib/utils/logger'; + /** * * Runs prettier and later prints the output configuration @@ -22,9 +24,7 @@ export function runPrettier(outputPath: string, source: string): void { useTabs: true, }); } catch (err) { - process.stdout.write( - `\n${yellow(`WARNING: Could not apply prettier to ${outputPath}` + ' due validation error, but the file has been created\n')}`, - ); + logger.warn(`WARNING: Could not apply prettier to ${outputPath} due to validation error, but the file has been created\n`); prettySource = source; } From 3437e95e1a4ee1acaa119a86e1277a232ac8d663 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 19 Aug 2020 11:31:48 +0530 Subject: [PATCH 04/12] fix: lint --- packages/utils/src/run-prettier.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/utils/src/run-prettier.ts b/packages/utils/src/run-prettier.ts index cb7ac89aa39..f68df7e4862 100644 --- a/packages/utils/src/run-prettier.ts +++ b/packages/utils/src/run-prettier.ts @@ -1,7 +1,5 @@ -import { yellow } from 'colorette'; import fs from 'fs'; import prettier from 'prettier'; - import logger from 'webpack-cli/lib/utils/logger'; /** From 03690f115d2597d7b85950437901947c7093b417 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 19 Aug 2020 13:14:19 +0530 Subject: [PATCH 05/12] refactor: update all instances --- packages/generators/src/init-generator.ts | 5 +++-- packages/info/src/index.ts | 2 +- packages/migrate/src/index.ts | 4 ++-- .../removeDeprecatedPlugins.ts | 3 ++- packages/utils/src/modify-config-helper.ts | 3 ++- packages/utils/src/scaffold.ts | 3 ++- packages/webpack-cli/lib/bootstrap.js | 2 +- packages/webpack-cli/lib/groups/HelpGroup.js | 22 +++++++++---------- .../webpack-cli/lib/utils/cli-executer.js | 2 +- packages/webpack-cli/lib/utils/logger.js | 2 +- 10 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/generators/src/init-generator.ts b/packages/generators/src/init-generator.ts index c952eb648a2..cef30c7b9a5 100644 --- a/packages/generators/src/init-generator.ts +++ b/packages/generators/src/init-generator.ts @@ -1,4 +1,5 @@ import { blue, green, bold } from 'colorette'; +import logger from 'webpack-cli/lib/utils/logger'; import logSymbols from 'log-symbols'; import path from 'path'; import { getPackageManager } from '@webpack-cli/package-utils'; @@ -74,12 +75,12 @@ export default class InitGenerator extends CustomGenerator { this.usingDefaults = true; - process.stdout.write( + logger.rawLog( `\n${logSymbols.info}${blue(' INFO ')} ` + 'For more information and a detailed description of each question, have a look at: ' + `${bold(green('https://github.com/webpack/webpack-cli/blob/master/INIT.md'))}\n`, ); - process.stdout.write(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n\n'); + logger.rawLog(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n\n'); const { multiEntries } = await Confirm( self, diff --git a/packages/info/src/index.ts b/packages/info/src/index.ts index 3a00c1b602e..26a44fa9b7c 100644 --- a/packages/info/src/index.ts +++ b/packages/info/src/index.ts @@ -61,6 +61,6 @@ export default async function info(...args): Promise { output = output.replace(/npmGlobalPackages/g, 'Global Packages'); const finalOutput = output; - process.stdout.write(finalOutput + '\n'); + logger.rawLog(finalOutput + '\n'); return finalOutput; } diff --git a/packages/migrate/src/index.ts b/packages/migrate/src/index.ts index 0b74593f461..6b7e5e6166c 100644 --- a/packages/migrate/src/index.ts +++ b/packages/migrate/src/index.ts @@ -93,9 +93,9 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom diffOutput.forEach((diffLine: Change): void => { if (diffLine.added) { - console.log(green(`+ ${diffLine.value}`)); + logger.rawLog(green(`+ ${diffLine.value}`)); } else if (diffLine.removed) { - console.log(red(`- ${diffLine.value}`)); + logger.rawLog(red(`- ${diffLine.value}`)); } }); diff --git a/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts b/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts index 4c73b758167..6f7888001c6 100644 --- a/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts +++ b/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts @@ -1,4 +1,5 @@ import { red, underline } from 'colorette'; +import logger from 'webpack-cli/lib/utils/logger'; import { findPluginsByName, isType, safeTraverse } from '@webpack-cli/utils'; @@ -34,7 +35,7 @@ export default function (j: JSCodeshift, ast: Node): Node { j(path).remove(); } } else { - process.stderr.write(` + logger.rawLog(` ${red('Please remove deprecated plugins manually. ')} See ${underline('https://webpack.js.org/guides/migrating/')} for more information.`); } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index 913d0d21e84..e5247a0b429 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -1,5 +1,6 @@ import { red, green } from 'colorette'; import fs from 'fs'; +import logger from 'webpack-cli/lib/utils/logger'; import path from 'path'; import yeoman from 'yeoman-environment'; import Generator from 'yeoman-generator'; @@ -140,7 +141,7 @@ export function modifyHelperUtil( const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build'; const successMessage = `\nYou can now run ${green(runCommand)} to bundle your application!\n\n`; - process.stdout.write(`\n${successMessage}`); + logger.rawLog(`\n${successMessage}`); } // scaffold webpack config file from using .yo-rc.json diff --git a/packages/utils/src/scaffold.ts b/packages/utils/src/scaffold.ts index 26d69b0cc98..e6d3abe41d3 100644 --- a/packages/utils/src/scaffold.ts +++ b/packages/utils/src/scaffold.ts @@ -1,4 +1,5 @@ import { green } from 'colorette'; +import logger from 'webpack-cli/lib/utils/logger'; import j from 'jscodeshift'; import pEachSeries = require('p-each-series'); import path from 'path'; @@ -102,5 +103,5 @@ export function runTransform(transformConfig: TransformConfig, action: string, g if (initActionNotDefined && transformConfig.config.item) { successMessage = green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`); } - process.stdout.write(`\n${successMessage}`); + logger.rawLog(`\n${successMessage}`); } diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index b95fb54c629..ac37cb4e393 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -100,7 +100,7 @@ async function runCLI(cliArgs) { cliArgs = newArgKeys; args = argParser('', core, cliArgs); await cli.run(args.opts, core); - process.stdout.write('\n'); + logger.rawLog('\n'); logger.warn('Duplicate flags found, defaulting to last set value'); } else { logger.error(err); diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index b72ab8ea325..3b8254725e0 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -22,11 +22,11 @@ class HelpGroup { const description = options.description; const link = options.link; - logger.help(`${header('Usage')}: ${usage}`); - logger.help(`${header('Description')}: ${description}`); + logger.rawLog(`${header('Usage')}: ${usage}`); + logger.rawLog(`${header('Description')}: ${description}`); if (link) { - logger.help(`${header('Documentation')}: ${link}`); + logger.rawLog(`${header('Documentation')}: ${link}`); } if (options.flags) { @@ -34,16 +34,16 @@ class HelpGroup { header: 'Options', optionList: options.flags, }); - logger.help(flags); + logger.rawLog(flags); } } else if (invalidArgs.length > 0) { const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command'; logger.warn(`You provided an invalid ${argType} '${invalidArgs[0]}'.`); - logger.help(this.run().outputOptions.help); + logger.rawLog(this.run().outputOptions.help); } else { - logger.help(this.run().outputOptions.help); + logger.rawLog(this.run().outputOptions.help); } - logger.help('\n Made with ♥️ by the webpack team'); + logger.rawLog('\n Made with ♥️ by the webpack team'); } outputVersion(externalPkg, commandsUsed, invalidArgs) { @@ -51,10 +51,10 @@ class HelpGroup { try { if ([externalPkg.alias, externalPkg.name].some((pkg) => commandsUsed.includes(pkg))) { const { name, version } = require(`@webpack-cli/${defaultCommands[externalPkg.name]}/package.json`); - logger.help(`\n${name} ${version}`); + logger.rawLog(`\n${name} ${version}`); } else { const { name, version } = require(`${externalPkg.name}/package.json`); - logger.help(`\n${name} ${version}`); + logger.rawLog(`\n${name} ${version}`); } } catch (e) { logger.error('Error: External package not found.'); @@ -76,8 +76,8 @@ class HelpGroup { const pkgJSON = require('../../package.json'); const webpack = require('webpack'); - logger.help(`\nwebpack-cli ${pkgJSON.version}`); - logger.help(`\nwebpack ${webpack.version}\n`); + logger.rawLog(`\nwebpack-cli ${pkgJSON.version}`); + logger.rawLog(`\nwebpack ${webpack.version}\n`); } run() { diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index 8c4a89cd95b..32ff6d8bad9 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -54,7 +54,7 @@ async function prompter() { async function run() { try { const args = await prompter(); - process.stdout.write('\n'); + logger.rawLog('\n'); logger.info('Executing CLI\n'); await runCLI(args); } catch (err) { diff --git a/packages/webpack-cli/lib/utils/logger.js b/packages/webpack-cli/lib/utils/logger.js index d2e90e04a81..616ce670514 100644 --- a/packages/webpack-cli/lib/utils/logger.js +++ b/packages/webpack-cli/lib/utils/logger.js @@ -6,5 +6,5 @@ module.exports = { info: (val) => console.info(`[webpack-cli] ${cyan(val)}`), success: (val) => console.log(`[webpack-cli] ${green(val)}`), log: (val) => console.log(`[webpack-cli] ${val}`), - help: (val) => console.log(val), + rawLog: (val) => console.log(val), }; From 40276c90e1e7d6a05889e3730d593ce7a136279c Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 19 Aug 2020 20:42:30 +0530 Subject: [PATCH 06/12] refactor: remove logger.rawLog() --- packages/generators/src/init-generator.ts | 5 ++--- packages/info/src/index.ts | 2 +- packages/migrate/src/index.ts | 4 ++-- .../removeDeprecatedPlugins.ts | 3 +-- packages/utils/src/modify-config-helper.ts | 3 +-- packages/utils/src/scaffold.ts | 3 +-- packages/webpack-cli/lib/bootstrap.js | 2 +- packages/webpack-cli/lib/groups/HelpGroup.js | 22 +++++++++---------- .../webpack-cli/lib/utils/cli-executer.js | 2 +- packages/webpack-cli/lib/utils/logger.js | 1 - 10 files changed, 21 insertions(+), 26 deletions(-) diff --git a/packages/generators/src/init-generator.ts b/packages/generators/src/init-generator.ts index cef30c7b9a5..d63d3fad5ea 100644 --- a/packages/generators/src/init-generator.ts +++ b/packages/generators/src/init-generator.ts @@ -1,5 +1,4 @@ import { blue, green, bold } from 'colorette'; -import logger from 'webpack-cli/lib/utils/logger'; import logSymbols from 'log-symbols'; import path from 'path'; import { getPackageManager } from '@webpack-cli/package-utils'; @@ -75,12 +74,12 @@ export default class InitGenerator extends CustomGenerator { this.usingDefaults = true; - logger.rawLog( + console.log( `\n${logSymbols.info}${blue(' INFO ')} ` + 'For more information and a detailed description of each question, have a look at: ' + `${bold(green('https://github.com/webpack/webpack-cli/blob/master/INIT.md'))}\n`, ); - logger.rawLog(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n\n'); + console.log(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n\n'); const { multiEntries } = await Confirm( self, diff --git a/packages/info/src/index.ts b/packages/info/src/index.ts index 26a44fa9b7c..d97c3a06219 100644 --- a/packages/info/src/index.ts +++ b/packages/info/src/index.ts @@ -61,6 +61,6 @@ export default async function info(...args): Promise { output = output.replace(/npmGlobalPackages/g, 'Global Packages'); const finalOutput = output; - logger.rawLog(finalOutput + '\n'); + console.log(finalOutput + '\n'); return finalOutput; } diff --git a/packages/migrate/src/index.ts b/packages/migrate/src/index.ts index 6b7e5e6166c..0b74593f461 100644 --- a/packages/migrate/src/index.ts +++ b/packages/migrate/src/index.ts @@ -93,9 +93,9 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom diffOutput.forEach((diffLine: Change): void => { if (diffLine.added) { - logger.rawLog(green(`+ ${diffLine.value}`)); + console.log(green(`+ ${diffLine.value}`)); } else if (diffLine.removed) { - logger.rawLog(red(`- ${diffLine.value}`)); + console.log(red(`- ${diffLine.value}`)); } }); diff --git a/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts b/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts index 6f7888001c6..4c05b71b7fe 100644 --- a/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts +++ b/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts @@ -1,5 +1,4 @@ import { red, underline } from 'colorette'; -import logger from 'webpack-cli/lib/utils/logger'; import { findPluginsByName, isType, safeTraverse } from '@webpack-cli/utils'; @@ -35,7 +34,7 @@ export default function (j: JSCodeshift, ast: Node): Node { j(path).remove(); } } else { - logger.rawLog(` + console.log(` ${red('Please remove deprecated plugins manually. ')} See ${underline('https://webpack.js.org/guides/migrating/')} for more information.`); } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index e5247a0b429..99eac91e405 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -1,6 +1,5 @@ import { red, green } from 'colorette'; import fs from 'fs'; -import logger from 'webpack-cli/lib/utils/logger'; import path from 'path'; import yeoman from 'yeoman-environment'; import Generator from 'yeoman-generator'; @@ -141,7 +140,7 @@ export function modifyHelperUtil( const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build'; const successMessage = `\nYou can now run ${green(runCommand)} to bundle your application!\n\n`; - logger.rawLog(`\n${successMessage}`); + console.log(`\n${successMessage}`); } // scaffold webpack config file from using .yo-rc.json diff --git a/packages/utils/src/scaffold.ts b/packages/utils/src/scaffold.ts index e6d3abe41d3..e8c07245615 100644 --- a/packages/utils/src/scaffold.ts +++ b/packages/utils/src/scaffold.ts @@ -1,5 +1,4 @@ import { green } from 'colorette'; -import logger from 'webpack-cli/lib/utils/logger'; import j from 'jscodeshift'; import pEachSeries = require('p-each-series'); import path from 'path'; @@ -103,5 +102,5 @@ export function runTransform(transformConfig: TransformConfig, action: string, g if (initActionNotDefined && transformConfig.config.item) { successMessage = green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`); } - logger.rawLog(`\n${successMessage}`); + console.log(`\n${successMessage}`); } diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index ac37cb4e393..5979d8122c1 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -100,7 +100,7 @@ async function runCLI(cliArgs) { cliArgs = newArgKeys; args = argParser('', core, cliArgs); await cli.run(args.opts, core); - logger.rawLog('\n'); + console.log('\n'); logger.warn('Duplicate flags found, defaulting to last set value'); } else { logger.error(err); diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index 3b8254725e0..9149cfe994a 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -22,11 +22,11 @@ class HelpGroup { const description = options.description; const link = options.link; - logger.rawLog(`${header('Usage')}: ${usage}`); - logger.rawLog(`${header('Description')}: ${description}`); + console.log(`${header('Usage')}: ${usage}`); + console.log(`${header('Description')}: ${description}`); if (link) { - logger.rawLog(`${header('Documentation')}: ${link}`); + console.log(`${header('Documentation')}: ${link}`); } if (options.flags) { @@ -34,16 +34,16 @@ class HelpGroup { header: 'Options', optionList: options.flags, }); - logger.rawLog(flags); + console.log(flags); } } else if (invalidArgs.length > 0) { const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command'; logger.warn(`You provided an invalid ${argType} '${invalidArgs[0]}'.`); - logger.rawLog(this.run().outputOptions.help); + console.log(this.run().outputOptions.help); } else { - logger.rawLog(this.run().outputOptions.help); + console.log(this.run().outputOptions.help); } - logger.rawLog('\n Made with ♥️ by the webpack team'); + console.log('\n Made with ♥️ by the webpack team'); } outputVersion(externalPkg, commandsUsed, invalidArgs) { @@ -51,10 +51,10 @@ class HelpGroup { try { if ([externalPkg.alias, externalPkg.name].some((pkg) => commandsUsed.includes(pkg))) { const { name, version } = require(`@webpack-cli/${defaultCommands[externalPkg.name]}/package.json`); - logger.rawLog(`\n${name} ${version}`); + console.log(`\n${name} ${version}`); } else { const { name, version } = require(`${externalPkg.name}/package.json`); - logger.rawLog(`\n${name} ${version}`); + console.log(`\n${name} ${version}`); } } catch (e) { logger.error('Error: External package not found.'); @@ -76,8 +76,8 @@ class HelpGroup { const pkgJSON = require('../../package.json'); const webpack = require('webpack'); - logger.rawLog(`\nwebpack-cli ${pkgJSON.version}`); - logger.rawLog(`\nwebpack ${webpack.version}\n`); + console.log(`\nwebpack-cli ${pkgJSON.version}`); + console.log(`\nwebpack ${webpack.version}\n`); } run() { diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index 32ff6d8bad9..afec2d972a4 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -54,7 +54,7 @@ async function prompter() { async function run() { try { const args = await prompter(); - logger.rawLog('\n'); + console.log('\n'); logger.info('Executing CLI\n'); await runCLI(args); } catch (err) { diff --git a/packages/webpack-cli/lib/utils/logger.js b/packages/webpack-cli/lib/utils/logger.js index 616ce670514..c5f5c952ddb 100644 --- a/packages/webpack-cli/lib/utils/logger.js +++ b/packages/webpack-cli/lib/utils/logger.js @@ -6,5 +6,4 @@ module.exports = { info: (val) => console.info(`[webpack-cli] ${cyan(val)}`), success: (val) => console.log(`[webpack-cli] ${green(val)}`), log: (val) => console.log(`[webpack-cli] ${val}`), - rawLog: (val) => console.log(val), }; From 00ed7e92fdd4454efb78ecc65a89376781a46c87 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 19 Aug 2020 20:56:44 +0530 Subject: [PATCH 07/12] tests: assert for substring in stderr --- test/entry/flag-entry/entry-with-flag.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/entry/flag-entry/entry-with-flag.test.js b/test/entry/flag-entry/entry-with-flag.test.js index d667c006021..0fb810f469e 100644 --- a/test/entry/flag-entry/entry-with-flag.test.js +++ b/test/entry/flag-entry/entry-with-flag.test.js @@ -41,6 +41,6 @@ describe('entry flag', () => { it('should throw error for invalid entry file', () => { const { stderr } = run(__dirname, ['--entry', './src/test.js']); - expect(stderr).toBeTruthy(); + expect(stderr).toContain('Error: you provided an invalid entry point.'); }); }); From 5b140b0ac48230a8c5c028195d403bb4e4be6b65 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 19 Aug 2020 21:22:31 +0530 Subject: [PATCH 08/12] refactor: use logger --- packages/generators/src/addon-generator.ts | 3 +- packages/generators/src/init-generator.ts | 5 +-- packages/migrate/src/index.ts | 2 +- .../removeDeprecatedPlugins.ts | 4 ++- packages/utils/src/modify-config-helper.ts | 35 ++++++++++--------- packages/utils/src/recursive-parser.ts | 4 ++- packages/utils/src/resolve-packages.ts | 23 ++++++------ packages/utils/src/scaffold.ts | 6 ++-- packages/webpack-cli/lib/groups/HelpGroup.js | 26 +++++++------- packages/webpack-cli/lib/utils/logger.js | 1 + 10 files changed, 60 insertions(+), 49 deletions(-) diff --git a/packages/generators/src/addon-generator.ts b/packages/generators/src/addon-generator.ts index ec4c4ddd5ca..9be1c88e7ed 100644 --- a/packages/generators/src/addon-generator.ts +++ b/packages/generators/src/addon-generator.ts @@ -1,3 +1,4 @@ +import logger from 'webpack-cli/lib/utils/logger'; import mkdirp from 'mkdirp'; import path from 'path'; import Generator from 'yeoman-generator'; @@ -54,7 +55,7 @@ const addonGenerator = ( try { mkdirp.sync(pathToProjectDir); } catch (err) { - console.error('Failed to create directory', err); + logger.error('Failed to create directory', err); } this.destinationRoot(pathToProjectDir); } diff --git a/packages/generators/src/init-generator.ts b/packages/generators/src/init-generator.ts index d63d3fad5ea..e90006a62a8 100644 --- a/packages/generators/src/init-generator.ts +++ b/packages/generators/src/init-generator.ts @@ -1,4 +1,5 @@ import { blue, green, bold } from 'colorette'; +import logger from 'webpack-cli/lib/utils/logger'; import logSymbols from 'log-symbols'; import path from 'path'; import { getPackageManager } from '@webpack-cli/package-utils'; @@ -74,12 +75,12 @@ export default class InitGenerator extends CustomGenerator { this.usingDefaults = true; - console.log( + logger.log( `\n${logSymbols.info}${blue(' INFO ')} ` + 'For more information and a detailed description of each question, have a look at: ' + `${bold(green('https://github.com/webpack/webpack-cli/blob/master/INIT.md'))}\n`, ); - console.log(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n\n'); + logger.log(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n\n'); const { multiEntries } = await Confirm( self, diff --git a/packages/migrate/src/index.ts b/packages/migrate/src/index.ts index 0b74593f461..1b8196c8306 100644 --- a/packages/migrate/src/index.ts +++ b/packages/migrate/src/index.ts @@ -202,7 +202,7 @@ export default function migrate(...args: string[]): void | Promise { return runMigration(currentConfigPath, outputConfigPath); }) .catch((err: object): void => { - console.error(err); + logger.error(err); }); } outputConfigPath = path.resolve(process.cwd(), filePaths[1]); diff --git a/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts b/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts index 4c05b71b7fe..9e48d1b6d52 100644 --- a/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts +++ b/packages/migrate/src/removeDeprecatedPlugins/removeDeprecatedPlugins.ts @@ -1,5 +1,7 @@ import { red, underline } from 'colorette'; +import logger from 'webpack-cli/lib/utils/logger'; + import { findPluginsByName, isType, safeTraverse } from '@webpack-cli/utils'; import { JSCodeshift, Node } from '../types/NodePath'; @@ -34,7 +36,7 @@ export default function (j: JSCodeshift, ast: Node): Node { j(path).remove(); } } else { - console.log(` + logger.log(` ${red('Please remove deprecated plugins manually. ')} See ${underline('https://webpack.js.org/guides/migrating/')} for more information.`); } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index 99eac91e405..5723b786b4a 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -1,5 +1,6 @@ -import { red, green } from 'colorette'; +import { green } from 'colorette'; import fs from 'fs'; +import logger from 'webpack-cli/lib/utils/logger'; import path from 'path'; import yeoman from 'yeoman-environment'; import Generator from 'yeoman-generator'; @@ -84,7 +85,8 @@ export function modifyHelperUtil( } } } catch (err) { - console.error(red('\nYour package.json was incorrectly formatted.\n')); + console.log('\n'); + logger.error('Your package.json was incorrectly formatted.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; } @@ -103,9 +105,10 @@ export function modifyHelperUtil( const confPath = path.resolve(process.cwd(), '.yo-rc.json'); configModule = require(confPath); } catch (err) { - console.error(red('\nCould not find a yeoman configuration file (.yo-rc.json).\n')); - console.error( - red("\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n"), + console.log('\n'); + logger.error('Could not find a yeoman configuration file (.yo-rc.json).\n\n'); + logger.error( + "Please make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n", ); Error.stackTraceLimit = 0; process.exitCode = 2; @@ -120,11 +123,10 @@ export function modifyHelperUtil( finalConfig = configModule[packageName].configuration; } } catch (err) { - console.error(err); - console.error(err.stack); - console.error( - red('\nYour yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n'), - ); + logger.error(err); + logger.error(err.stack); + console.log('\n'); + logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; } @@ -139,22 +141,21 @@ export function modifyHelperUtil( if (finalConfig.usingDefaults && finalConfig.usingDefaults === true) { const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build'; - const successMessage = `\nYou can now run ${green(runCommand)} to bundle your application!\n\n`; - console.log(`\n${successMessage}`); + const successMessage = `You can now run ${green(runCommand)} to bundle your application!\n\n`; + console.log('\n'); + logger.log(successMessage); } // scaffold webpack config file from using .yo-rc.json return runTransform(transformConfig, 'init', generateConfig); }) .catch((err): void => { - console.error( - red( - ` + logger.error( + ` Unexpected Error please file an issue here https://github.com/webpack/webpack-cli/issues/new?template=Bug_report.md `, - ), ); - console.error(err); + logger.error(err); }); } diff --git a/packages/utils/src/recursive-parser.ts b/packages/utils/src/recursive-parser.ts index 624b0c18faa..0bdcdf81ba0 100644 --- a/packages/utils/src/recursive-parser.ts +++ b/packages/utils/src/recursive-parser.ts @@ -1,12 +1,14 @@ import { parseTopScope, findRootNodesByName, addProperty, removeProperty, parseMerge, safeTraverse } from './ast-utils'; import { JSCodeshift, Node, valueType } from './types/NodePath'; +import logger from 'webpack-cli/lib/utils/logger'; + export function recursiveTransform(j: JSCodeshift, ast: Node, key: string, value: valueType, action: string): boolean | Node { if (key === 'topScope') { if (Array.isArray(value)) { return parseTopScope(j, ast, value, action); } - console.error('Error in parsing top scope, Array required'); + logger.error('Error in parsing top scope, Array required'); return false; } else if (key === 'merge') { if (Array.isArray(value)) { diff --git a/packages/utils/src/resolve-packages.ts b/packages/utils/src/resolve-packages.ts index 39825343f24..38ec4436308 100644 --- a/packages/utils/src/resolve-packages.ts +++ b/packages/utils/src/resolve-packages.ts @@ -1,4 +1,5 @@ -import { red, bold } from 'colorette'; +import { bold } from 'colorette'; +import logger from 'webpack-cli/lib/utils/logger'; import path from 'path'; import { modifyHelperUtil } from './modify-config-helper'; import { getPathToGlobalPackages, spawnChild } from '@webpack-cli/package-utils'; @@ -57,9 +58,9 @@ export function resolvePackages(pkg: string[]): Function | void { require.resolve(absolutePath); packageLocations.push(absolutePath); } catch (err) { - console.error(`Cannot find a generator at ${absolutePath}.`); - console.error('\nReason:\n'); - console.error(bold(red(err))); + logger.error(`Cannot find a generator at ${absolutePath}.\n`); + logger.error('Reason:\n'); + logger.error(bold(err)); process.exitCode = 1; } @@ -74,17 +75,17 @@ export function resolvePackages(pkg: string[]): Function | void { const globalPath: string = getPathToGlobalPackages(); packageLocations.push(path.resolve(globalPath, scaffold)); } catch (err) { - console.error("Package wasn't validated correctly.."); - console.error('Submit an issue for', pkg, 'if this persists'); - console.error('\nReason: \n'); - console.error(bold(red(err))); + logger.error("Package wasn't validated correctly.."); + logger.error('Submit an issue for', pkg, 'if this persists\n'); + logger.error('Reason: \n'); + logger.error(bold(err)); process.exitCode = 1; } }) .catch((err: string): void => { - console.error("Package couldn't be installed, aborting.."); - console.error('\nReason: \n'); - console.error(bold(red(err))); + logger.error("Package couldn't be installed, aborting..\n"); + logger.error('Reason: \n'); + logger.error(bold(err)); process.exitCode = 1; }) .then(invokeGeneratorIfReady); diff --git a/packages/utils/src/scaffold.ts b/packages/utils/src/scaffold.ts index e8c07245615..ae851781263 100644 --- a/packages/utils/src/scaffold.ts +++ b/packages/utils/src/scaffold.ts @@ -1,5 +1,6 @@ import { green } from 'colorette'; import j from 'jscodeshift'; +import logger from 'webpack-cli/lib/utils/logger'; import pEachSeries = require('p-each-series'); import path from 'path'; import { getPackageManager } from '@webpack-cli/package-utils'; @@ -88,7 +89,7 @@ export function runTransform(transformConfig: TransformConfig, action: string, g runPrettier(outputPath, source); }) .catch((err: Error): void => { - console.error(err.message ? err.message : err); + logger.error(err.message ? err.message : err); }); }, ); @@ -102,5 +103,6 @@ export function runTransform(transformConfig: TransformConfig, action: string, g if (initActionNotDefined && transformConfig.config.item) { successMessage = green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`); } - console.log(`\n${successMessage}`); + console.log('\n'); + logger.log(successMessage); } diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index 9149cfe994a..c78612e8b54 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -22,11 +22,11 @@ class HelpGroup { const description = options.description; const link = options.link; - console.log(`${header('Usage')}: ${usage}`); - console.log(`${header('Description')}: ${description}`); + logger.help(`${header('Usage')}: ${usage}`); + logger.help(`${header('Description')}: ${description}`); if (link) { - console.log(`${header('Documentation')}: ${link}`); + logger.help(`${header('Documentation')}: ${link}`); } if (options.flags) { @@ -34,16 +34,16 @@ class HelpGroup { header: 'Options', optionList: options.flags, }); - console.log(flags); + logger.help(flags); } } else if (invalidArgs.length > 0) { const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command'; logger.warn(`You provided an invalid ${argType} '${invalidArgs[0]}'.`); - console.log(this.run().outputOptions.help); + logger.help(this.run().outputOptions.help); } else { - console.log(this.run().outputOptions.help); + logger.help(this.run().outputOptions.help); } - console.log('\n Made with ♥️ by the webpack team'); + logger.help('\n Made with ♥️ by the webpack team'); } outputVersion(externalPkg, commandsUsed, invalidArgs) { @@ -51,20 +51,20 @@ class HelpGroup { try { if ([externalPkg.alias, externalPkg.name].some((pkg) => commandsUsed.includes(pkg))) { const { name, version } = require(`@webpack-cli/${defaultCommands[externalPkg.name]}/package.json`); - console.log(`\n${name} ${version}`); + logger.help(`\n${name} ${version}`); } else { const { name, version } = require(`${externalPkg.name}/package.json`); - console.log(`\n${name} ${version}`); + logger.help(`\n${name} ${version}`); } } catch (e) { logger.error('Error: External package not found.'); - process.exit(2); + process.exitCode = 1; } } if (commandsUsed.length > 1) { logger.error('You provided multiple commands. Please use only one command at a time.\n'); - process.exit(2); + process.exit(1); } if (invalidArgs.length > 0) { @@ -76,8 +76,8 @@ class HelpGroup { const pkgJSON = require('../../package.json'); const webpack = require('webpack'); - console.log(`\nwebpack-cli ${pkgJSON.version}`); - console.log(`\nwebpack ${webpack.version}\n`); + logger.help(`\nwebpack-cli ${pkgJSON.version}`); + logger.help(`\nwebpack ${webpack.version}\n`); } run() { diff --git a/packages/webpack-cli/lib/utils/logger.js b/packages/webpack-cli/lib/utils/logger.js index c5f5c952ddb..d2e90e04a81 100644 --- a/packages/webpack-cli/lib/utils/logger.js +++ b/packages/webpack-cli/lib/utils/logger.js @@ -6,4 +6,5 @@ module.exports = { info: (val) => console.info(`[webpack-cli] ${cyan(val)}`), success: (val) => console.log(`[webpack-cli] ${green(val)}`), log: (val) => console.log(`[webpack-cli] ${val}`), + help: (val) => console.log(val), }; From be815947f322aa6be71d9ec8ba7281d613144cff Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Thu, 20 Aug 2020 08:21:54 +0530 Subject: [PATCH 09/12] refactor: rename logger.help() to logger.raw() --- packages/info/src/index.ts | 2 +- packages/migrate/src/index.ts | 4 ++-- packages/utils/src/modify-config-helper.ts | 8 +++---- packages/utils/src/scaffold.ts | 2 +- packages/webpack-cli/lib/bootstrap.js | 2 +- packages/webpack-cli/lib/groups/HelpGroup.js | 22 +++++++++---------- .../webpack-cli/lib/utils/cli-executer.js | 2 +- packages/webpack-cli/lib/utils/logger.js | 2 +- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/info/src/index.ts b/packages/info/src/index.ts index d97c3a06219..03939b7836f 100644 --- a/packages/info/src/index.ts +++ b/packages/info/src/index.ts @@ -61,6 +61,6 @@ export default async function info(...args): Promise { output = output.replace(/npmGlobalPackages/g, 'Global Packages'); const finalOutput = output; - console.log(finalOutput + '\n'); + logger.raw(finalOutput + '\n'); return finalOutput; } diff --git a/packages/migrate/src/index.ts b/packages/migrate/src/index.ts index 1b8196c8306..788512a3cd3 100644 --- a/packages/migrate/src/index.ts +++ b/packages/migrate/src/index.ts @@ -93,9 +93,9 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom diffOutput.forEach((diffLine: Change): void => { if (diffLine.added) { - console.log(green(`+ ${diffLine.value}`)); + logger.raw(green(`+ ${diffLine.value}`)); } else if (diffLine.removed) { - console.log(red(`- ${diffLine.value}`)); + logger.raw(red(`- ${diffLine.value}`)); } }); diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index 5723b786b4a..9d76b188189 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -85,7 +85,7 @@ export function modifyHelperUtil( } } } catch (err) { - console.log('\n'); + logger.raw('\n'); logger.error('Your package.json was incorrectly formatted.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; @@ -105,7 +105,7 @@ export function modifyHelperUtil( const confPath = path.resolve(process.cwd(), '.yo-rc.json'); configModule = require(confPath); } catch (err) { - console.log('\n'); + logger.raw('\n'); logger.error('Could not find a yeoman configuration file (.yo-rc.json).\n\n'); logger.error( "Please make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n", @@ -125,7 +125,7 @@ export function modifyHelperUtil( } catch (err) { logger.error(err); logger.error(err.stack); - console.log('\n'); + logger.raw('\n'); logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; @@ -142,7 +142,7 @@ export function modifyHelperUtil( const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build'; const successMessage = `You can now run ${green(runCommand)} to bundle your application!\n\n`; - console.log('\n'); + logger.raw('\n'); logger.log(successMessage); } diff --git a/packages/utils/src/scaffold.ts b/packages/utils/src/scaffold.ts index ae851781263..6cc5c0368cb 100644 --- a/packages/utils/src/scaffold.ts +++ b/packages/utils/src/scaffold.ts @@ -103,6 +103,6 @@ export function runTransform(transformConfig: TransformConfig, action: string, g if (initActionNotDefined && transformConfig.config.item) { successMessage = green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`); } - console.log('\n'); + logger.raw('\n'); logger.log(successMessage); } diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index 5979d8122c1..82d01511916 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -100,7 +100,7 @@ async function runCLI(cliArgs) { cliArgs = newArgKeys; args = argParser('', core, cliArgs); await cli.run(args.opts, core); - console.log('\n'); + logger.raw('\n'); logger.warn('Duplicate flags found, defaulting to last set value'); } else { logger.error(err); diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index c78612e8b54..c3da0e25f32 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -22,11 +22,11 @@ class HelpGroup { const description = options.description; const link = options.link; - logger.help(`${header('Usage')}: ${usage}`); - logger.help(`${header('Description')}: ${description}`); + logger.raw(`${header('Usage')}: ${usage}`); + logger.raw(`${header('Description')}: ${description}`); if (link) { - logger.help(`${header('Documentation')}: ${link}`); + logger.raw(`${header('Documentation')}: ${link}`); } if (options.flags) { @@ -34,16 +34,16 @@ class HelpGroup { header: 'Options', optionList: options.flags, }); - logger.help(flags); + logger.raw(flags); } } else if (invalidArgs.length > 0) { const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command'; logger.warn(`You provided an invalid ${argType} '${invalidArgs[0]}'.`); - logger.help(this.run().outputOptions.help); + logger.raw(this.run().outputOptions.help); } else { - logger.help(this.run().outputOptions.help); + logger.raw(this.run().outputOptions.help); } - logger.help('\n Made with ♥️ by the webpack team'); + logger.raw('\n Made with ♥️ by the webpack team'); } outputVersion(externalPkg, commandsUsed, invalidArgs) { @@ -51,10 +51,10 @@ class HelpGroup { try { if ([externalPkg.alias, externalPkg.name].some((pkg) => commandsUsed.includes(pkg))) { const { name, version } = require(`@webpack-cli/${defaultCommands[externalPkg.name]}/package.json`); - logger.help(`\n${name} ${version}`); + logger.raw(`\n${name} ${version}`); } else { const { name, version } = require(`${externalPkg.name}/package.json`); - logger.help(`\n${name} ${version}`); + logger.raw(`\n${name} ${version}`); } } catch (e) { logger.error('Error: External package not found.'); @@ -76,8 +76,8 @@ class HelpGroup { const pkgJSON = require('../../package.json'); const webpack = require('webpack'); - logger.help(`\nwebpack-cli ${pkgJSON.version}`); - logger.help(`\nwebpack ${webpack.version}\n`); + logger.raw(`\nwebpack-cli ${pkgJSON.version}`); + logger.raw(`\nwebpack ${webpack.version}\n`); } run() { diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index afec2d972a4..2e1bbf4ee5e 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -54,7 +54,7 @@ async function prompter() { async function run() { try { const args = await prompter(); - console.log('\n'); + logger.raw('\n'); logger.info('Executing CLI\n'); await runCLI(args); } catch (err) { diff --git a/packages/webpack-cli/lib/utils/logger.js b/packages/webpack-cli/lib/utils/logger.js index d2e90e04a81..b1d76269d56 100644 --- a/packages/webpack-cli/lib/utils/logger.js +++ b/packages/webpack-cli/lib/utils/logger.js @@ -6,5 +6,5 @@ module.exports = { info: (val) => console.info(`[webpack-cli] ${cyan(val)}`), success: (val) => console.log(`[webpack-cli] ${green(val)}`), log: (val) => console.log(`[webpack-cli] ${val}`), - help: (val) => console.log(val), + raw: (val) => console.log(val), }; From f8f6abcdde97ee5cc8bf6f47b333cc9bfa2fafc1 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Mon, 24 Aug 2020 17:27:38 +0530 Subject: [PATCH 10/12] refactor: use logger --- packages/info/src/index.ts | 2 +- packages/utils/src/modify-config-helper.ts | 8 ++++---- packages/utils/src/resolve-packages.ts | 14 +++++++------- packages/utils/src/scaffold.ts | 2 +- packages/webpack-cli/lib/bootstrap.js | 2 +- packages/webpack-cli/lib/groups/HelpGroup.js | 4 ++-- packages/webpack-cli/lib/utils/cli-executer.js | 2 +- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/info/src/index.ts b/packages/info/src/index.ts index 03939b7836f..a59c64ba23d 100644 --- a/packages/info/src/index.ts +++ b/packages/info/src/index.ts @@ -61,6 +61,6 @@ export default async function info(...args): Promise { output = output.replace(/npmGlobalPackages/g, 'Global Packages'); const finalOutput = output; - logger.raw(finalOutput + '\n'); + logger.raw(finalOutput); return finalOutput; } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index 9d76b188189..49b7768fb0c 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -85,7 +85,7 @@ export function modifyHelperUtil( } } } catch (err) { - logger.raw('\n'); + logger.raw(); logger.error('Your package.json was incorrectly formatted.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; @@ -105,7 +105,7 @@ export function modifyHelperUtil( const confPath = path.resolve(process.cwd(), '.yo-rc.json'); configModule = require(confPath); } catch (err) { - logger.raw('\n'); + logger.raw(); logger.error('Could not find a yeoman configuration file (.yo-rc.json).\n\n'); logger.error( "Please make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n", @@ -125,7 +125,7 @@ export function modifyHelperUtil( } catch (err) { logger.error(err); logger.error(err.stack); - logger.raw('\n'); + logger.raw(); logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; @@ -142,7 +142,7 @@ export function modifyHelperUtil( const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build'; const successMessage = `You can now run ${green(runCommand)} to bundle your application!\n\n`; - logger.raw('\n'); + logger.raw(); logger.log(successMessage); } diff --git a/packages/utils/src/resolve-packages.ts b/packages/utils/src/resolve-packages.ts index 38ec4436308..f919716daf9 100644 --- a/packages/utils/src/resolve-packages.ts +++ b/packages/utils/src/resolve-packages.ts @@ -58,8 +58,8 @@ export function resolvePackages(pkg: string[]): Function | void { require.resolve(absolutePath); packageLocations.push(absolutePath); } catch (err) { - logger.error(`Cannot find a generator at ${absolutePath}.\n`); - logger.error('Reason:\n'); + logger.error(`Cannot find a generator at ${absolutePath}.`); + logger.error('Reason:'); logger.error(bold(err)); process.exitCode = 1; } @@ -75,16 +75,16 @@ export function resolvePackages(pkg: string[]): Function | void { const globalPath: string = getPathToGlobalPackages(); packageLocations.push(path.resolve(globalPath, scaffold)); } catch (err) { - logger.error("Package wasn't validated correctly.."); - logger.error('Submit an issue for', pkg, 'if this persists\n'); - logger.error('Reason: \n'); + logger.error("Package wasn't validated correctly..."); + logger.error('Submit an issue for', pkg, 'if this persists'); + logger.error('Reason:'); logger.error(bold(err)); process.exitCode = 1; } }) .catch((err: string): void => { - logger.error("Package couldn't be installed, aborting..\n"); - logger.error('Reason: \n'); + logger.error("Package couldn't be installed, aborting..."); + logger.error('Reason:'); logger.error(bold(err)); process.exitCode = 1; }) diff --git a/packages/utils/src/scaffold.ts b/packages/utils/src/scaffold.ts index 6cc5c0368cb..90c7b98239e 100644 --- a/packages/utils/src/scaffold.ts +++ b/packages/utils/src/scaffold.ts @@ -103,6 +103,6 @@ export function runTransform(transformConfig: TransformConfig, action: string, g if (initActionNotDefined && transformConfig.config.item) { successMessage = green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`); } - logger.raw('\n'); + logger.raw(); logger.log(successMessage); } diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index 82d01511916..f199d848454 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -100,7 +100,7 @@ async function runCLI(cliArgs) { cliArgs = newArgKeys; args = argParser('', core, cliArgs); await cli.run(args.opts, core); - logger.raw('\n'); + logger.raw(); logger.warn('Duplicate flags found, defaulting to last set value'); } else { logger.error(err); diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index c3da0e25f32..7622310dd30 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -58,13 +58,13 @@ class HelpGroup { } } catch (e) { logger.error('Error: External package not found.'); - process.exitCode = 1; + process.exit(2); } } if (commandsUsed.length > 1) { logger.error('You provided multiple commands. Please use only one command at a time.\n'); - process.exit(1); + process.exit(2); } if (invalidArgs.length > 0) { diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index 2e1bbf4ee5e..55ad3f4148e 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -54,7 +54,7 @@ async function prompter() { async function run() { try { const args = await prompter(); - logger.raw('\n'); + logger.raw(); logger.info('Executing CLI\n'); await runCLI(args); } catch (err) { From a34667ff04f359fd6baa7283ef406028481b94b2 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Tue, 25 Aug 2020 22:17:28 +0530 Subject: [PATCH 11/12] refactor: remove extraneous newline character --- packages/generators/src/init-generator.ts | 5 +++-- packages/migrate/src/index.ts | 6 +++--- packages/utils/src/modify-config-helper.ts | 11 ++++++----- packages/utils/src/run-prettier.ts | 2 +- packages/webpack-cli/lib/groups/HelpGroup.js | 6 +++--- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/generators/src/init-generator.ts b/packages/generators/src/init-generator.ts index e90006a62a8..699da240980 100644 --- a/packages/generators/src/init-generator.ts +++ b/packages/generators/src/init-generator.ts @@ -78,9 +78,10 @@ export default class InitGenerator extends CustomGenerator { logger.log( `\n${logSymbols.info}${blue(' INFO ')} ` + 'For more information and a detailed description of each question, have a look at: ' + - `${bold(green('https://github.com/webpack/webpack-cli/blob/master/INIT.md'))}\n`, + `${bold(green('https://github.com/webpack/webpack-cli/blob/master/INIT.md'))}`, ); - logger.log(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n\n'); + logger.log(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info'); + logger.raw(); const { multiEntries } = await Confirm( self, diff --git a/packages/migrate/src/index.ts b/packages/migrate/src/index.ts index 788512a3cd3..546c06daa05 100644 --- a/packages/migrate/src/index.ts +++ b/packages/migrate/src/index.ts @@ -139,7 +139,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom // eslint-disable-next-line @typescript-eslint/no-explicit-any const webpackOptionsValidationErrors: any = validate(outputConfig); if (webpackOptionsValidationErrors.length) { - logger.error("\n✖ Your configuration validation wasn't successful \n"); + logger.error("\n✖ Your configuration validation wasn't successful"); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore logger.error(new WebpackOptionsValidationError(webpackOptionsValidationErrors)); @@ -154,7 +154,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom ); }) .catch((err: object): void => { - const errMsg = '\n ✖ ︎Migration aborted due to some errors: \n'; + const errMsg = '\n ✖ ︎Migration aborted due to some errors:'; logger.error(errMsg); logger.error(err); process.exitCode = 1; @@ -175,7 +175,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom export default function migrate(...args: string[]): void | Promise { const filePaths = args; if (!filePaths.length) { - const errMsg = '\n ✖ Please specify a path to your webpack config \n '; + const errMsg = '\n ✖ Please specify a path to your webpack config'; logger.error(errMsg); return; } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index 49b7768fb0c..ba9b7e82ec1 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -86,7 +86,7 @@ export function modifyHelperUtil( } } catch (err) { logger.raw(); - logger.error('Your package.json was incorrectly formatted.\n'); + logger.error('Your package.json was incorrectly formatted.'); Error.stackTraceLimit = 0; process.exitCode = 2; } @@ -106,9 +106,10 @@ export function modifyHelperUtil( configModule = require(confPath); } catch (err) { logger.raw(); - logger.error('Could not find a yeoman configuration file (.yo-rc.json).\n\n'); + logger.error('Could not find a yeoman configuration file (.yo-rc.json).'); + logger.raw(); logger.error( - "Please make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n", + "Please make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.", ); Error.stackTraceLimit = 0; process.exitCode = 2; @@ -126,7 +127,7 @@ export function modifyHelperUtil( logger.error(err); logger.error(err.stack); logger.raw(); - logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n'); + logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.'); Error.stackTraceLimit = 0; process.exitCode = 2; } @@ -141,7 +142,7 @@ export function modifyHelperUtil( if (finalConfig.usingDefaults && finalConfig.usingDefaults === true) { const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build'; - const successMessage = `You can now run ${green(runCommand)} to bundle your application!\n\n`; + const successMessage = `You can now run ${green(runCommand)} to bundle your application!\n`; logger.raw(); logger.log(successMessage); } diff --git a/packages/utils/src/run-prettier.ts b/packages/utils/src/run-prettier.ts index f68df7e4862..a2af09cfb44 100644 --- a/packages/utils/src/run-prettier.ts +++ b/packages/utils/src/run-prettier.ts @@ -22,7 +22,7 @@ export function runPrettier(outputPath: string, source: string): void { useTabs: true, }); } catch (err) { - logger.warn(`WARNING: Could not apply prettier to ${outputPath} due to validation error, but the file has been created\n`); + logger.warn(`WARNING: Could not apply prettier to ${outputPath} due to validation error, but the file has been created`); prettySource = source; } diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index 7622310dd30..d8a68a1777b 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -63,21 +63,21 @@ class HelpGroup { } if (commandsUsed.length > 1) { - logger.error('You provided multiple commands. Please use only one command at a time.\n'); + logger.error('You provided multiple commands. Please use only one command at a time.'); process.exit(2); } if (invalidArgs.length > 0) { const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command'; logger.error(red(`Error: Invalid ${argType} '${invalidArgs[0]}'.`)); - logger.info(cyan('Run webpack --help to see available commands and arguments.\n')); + logger.info(cyan('Run webpack --help to see available commands and arguments.')); process.exit(2); } const pkgJSON = require('../../package.json'); const webpack = require('webpack'); logger.raw(`\nwebpack-cli ${pkgJSON.version}`); - logger.raw(`\nwebpack ${webpack.version}\n`); + logger.raw(`\nwebpack ${webpack.version}`); } run() { From 814033c79a66b2c8a3237f2138cb2e8efda17b81 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 26 Aug 2020 18:00:04 +0530 Subject: [PATCH 12/12] refactor: cleanup --- packages/generators/src/init-generator.ts | 3 +-- packages/migrate/src/index.ts | 12 +++++------- packages/utils/src/modify-config-helper.ts | 18 ++++++------------ packages/utils/src/resolve-packages.ts | 12 ++++++------ packages/utils/src/run-prettier.ts | 2 +- packages/utils/src/scaffold.ts | 5 ++--- packages/webpack-cli/lib/bootstrap.js | 3 +-- packages/webpack-cli/lib/groups/BasicGroup.js | 2 +- packages/webpack-cli/lib/groups/HelpGroup.js | 6 +++--- packages/webpack-cli/lib/utils/cli-executer.js | 3 +-- test/migrate/config/migrate-config.test.js | 2 +- 11 files changed, 28 insertions(+), 40 deletions(-) diff --git a/packages/generators/src/init-generator.ts b/packages/generators/src/init-generator.ts index 699da240980..5eb9862c41e 100644 --- a/packages/generators/src/init-generator.ts +++ b/packages/generators/src/init-generator.ts @@ -80,8 +80,7 @@ export default class InitGenerator extends CustomGenerator { 'For more information and a detailed description of each question, have a look at: ' + `${bold(green('https://github.com/webpack/webpack-cli/blob/master/INIT.md'))}`, ); - logger.log(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info'); - logger.raw(); + logger.log(`${logSymbols.info}${blue(' INFO ')} ` + 'Alternatively, run "webpack(-cli) --help" for usage info\n'); const { multiEntries } = await Confirm( self, diff --git a/packages/migrate/src/index.ts b/packages/migrate/src/index.ts index 546c06daa05..ad89cb78a1c 100644 --- a/packages/migrate/src/index.ts +++ b/packages/migrate/src/index.ts @@ -93,9 +93,9 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom diffOutput.forEach((diffLine: Change): void => { if (diffLine.added) { - logger.raw(green(`+ ${diffLine.value}`)); + process.stdout.write(green(`+ ${diffLine.value}`)); } else if (diffLine.removed) { - logger.raw(red(`- ${diffLine.value}`)); + process.stdout.write(red(`- ${diffLine.value}`)); } }); @@ -139,7 +139,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom // eslint-disable-next-line @typescript-eslint/no-explicit-any const webpackOptionsValidationErrors: any = validate(outputConfig); if (webpackOptionsValidationErrors.length) { - logger.error("\n✖ Your configuration validation wasn't successful"); + logger.error("\n✖ Your configuration validation wasn't successful\n"); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore logger.error(new WebpackOptionsValidationError(webpackOptionsValidationErrors)); @@ -154,8 +154,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom ); }) .catch((err: object): void => { - const errMsg = '\n ✖ ︎Migration aborted due to some errors:'; - logger.error(errMsg); + logger.error('\n ✖ ︎Migration aborted due to some errors:\n'); logger.error(err); process.exitCode = 1; }); @@ -175,8 +174,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom export default function migrate(...args: string[]): void | Promise { const filePaths = args; if (!filePaths.length) { - const errMsg = '\n ✖ Please specify a path to your webpack config'; - logger.error(errMsg); + logger.error('\n ✖ Please specify a path to your webpack config\n'); return; } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index ba9b7e82ec1..2cd66031704 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -85,8 +85,7 @@ export function modifyHelperUtil( } } } catch (err) { - logger.raw(); - logger.error('Your package.json was incorrectly formatted.'); + logger.error('\nYour package.json was incorrectly formatted.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; } @@ -105,11 +104,9 @@ export function modifyHelperUtil( const confPath = path.resolve(process.cwd(), '.yo-rc.json'); configModule = require(confPath); } catch (err) { - logger.raw(); - logger.error('Could not find a yeoman configuration file (.yo-rc.json).'); - logger.raw(); + logger.error('\nCould not find a yeoman configuration file (.yo-rc.json).\n'); logger.error( - "Please make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.", + "\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n", ); Error.stackTraceLimit = 0; process.exitCode = 2; @@ -125,9 +122,8 @@ export function modifyHelperUtil( } } catch (err) { logger.error(err); - logger.error(err.stack); - logger.raw(); - logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.'); + logger.error(`${err.stack}\n`); + logger.error('Your yeoman configuration file (.yo-rc.json) was incorrectly formatted. Deleting it may fix the problem.\n'); Error.stackTraceLimit = 0; process.exitCode = 2; } @@ -142,9 +138,7 @@ export function modifyHelperUtil( if (finalConfig.usingDefaults && finalConfig.usingDefaults === true) { const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build'; - const successMessage = `You can now run ${green(runCommand)} to bundle your application!\n`; - logger.raw(); - logger.log(successMessage); + logger.log(`\nYou can now run ${green(runCommand)} to bundle your application!\n`); } // scaffold webpack config file from using .yo-rc.json diff --git a/packages/utils/src/resolve-packages.ts b/packages/utils/src/resolve-packages.ts index f919716daf9..773cc0f9d41 100644 --- a/packages/utils/src/resolve-packages.ts +++ b/packages/utils/src/resolve-packages.ts @@ -58,8 +58,8 @@ export function resolvePackages(pkg: string[]): Function | void { require.resolve(absolutePath); packageLocations.push(absolutePath); } catch (err) { - logger.error(`Cannot find a generator at ${absolutePath}.`); - logger.error('Reason:'); + logger.error(`Cannot find a generator at ${absolutePath}.\n`); + logger.error('Reason:\n'); logger.error(bold(err)); process.exitCode = 1; } @@ -76,15 +76,15 @@ export function resolvePackages(pkg: string[]): Function | void { packageLocations.push(path.resolve(globalPath, scaffold)); } catch (err) { logger.error("Package wasn't validated correctly..."); - logger.error('Submit an issue for', pkg, 'if this persists'); - logger.error('Reason:'); + logger.error(`Submit an issue for ${pkg} if this persists\n`); + logger.error('Reason:\n'); logger.error(bold(err)); process.exitCode = 1; } }) .catch((err: string): void => { - logger.error("Package couldn't be installed, aborting..."); - logger.error('Reason:'); + logger.error("Package couldn't be installed, aborting...\n"); + logger.error('Reason:\n'); logger.error(bold(err)); process.exitCode = 1; }) diff --git a/packages/utils/src/run-prettier.ts b/packages/utils/src/run-prettier.ts index a2af09cfb44..6d7afcfb6a5 100644 --- a/packages/utils/src/run-prettier.ts +++ b/packages/utils/src/run-prettier.ts @@ -22,7 +22,7 @@ export function runPrettier(outputPath: string, source: string): void { useTabs: true, }); } catch (err) { - logger.warn(`WARNING: Could not apply prettier to ${outputPath} due to validation error, but the file has been created`); + logger.warn(`\nWARNING: Could not apply prettier to ${outputPath} due to validation error, but the file has been created`); prettySource = source; } diff --git a/packages/utils/src/scaffold.ts b/packages/utils/src/scaffold.ts index 90c7b98239e..a9b2229ca41 100644 --- a/packages/utils/src/scaffold.ts +++ b/packages/utils/src/scaffold.ts @@ -89,7 +89,7 @@ export function runTransform(transformConfig: TransformConfig, action: string, g runPrettier(outputPath, source); }) .catch((err: Error): void => { - logger.error(err.message ? err.message : err); + logger.error(err); }); }, ); @@ -103,6 +103,5 @@ export function runTransform(transformConfig: TransformConfig, action: string, g if (initActionNotDefined && transformConfig.config.item) { successMessage = green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`); } - logger.raw(); - logger.log(successMessage); + logger.log(`\n${successMessage}`); } diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index f199d848454..5c15faa5ab9 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -100,8 +100,7 @@ async function runCLI(cliArgs) { cliArgs = newArgKeys; args = argParser('', core, cliArgs); await cli.run(args.opts, core); - logger.raw(); - logger.warn('Duplicate flags found, defaulting to last set value'); + logger.warn('\nDuplicate flags found, defaulting to last set value'); } else { logger.error(err); return; diff --git a/packages/webpack-cli/lib/groups/BasicGroup.js b/packages/webpack-cli/lib/groups/BasicGroup.js index 27361bbe289..de43289985e 100644 --- a/packages/webpack-cli/lib/groups/BasicGroup.js +++ b/packages/webpack-cli/lib/groups/BasicGroup.js @@ -39,7 +39,7 @@ class BasicGroup extends GroupHelper { if (arg === 'entry') { options[arg] = this.resolveFilePath(args[arg], 'index.js'); if (options[arg].length === 0) { - logger.error('Error: you provided an invalid entry point.'); + logger.error('\nError: you provided an invalid entry point.'); } } }); diff --git a/packages/webpack-cli/lib/groups/HelpGroup.js b/packages/webpack-cli/lib/groups/HelpGroup.js index d8a68a1777b..7622310dd30 100644 --- a/packages/webpack-cli/lib/groups/HelpGroup.js +++ b/packages/webpack-cli/lib/groups/HelpGroup.js @@ -63,21 +63,21 @@ class HelpGroup { } if (commandsUsed.length > 1) { - logger.error('You provided multiple commands. Please use only one command at a time.'); + logger.error('You provided multiple commands. Please use only one command at a time.\n'); process.exit(2); } if (invalidArgs.length > 0) { const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command'; logger.error(red(`Error: Invalid ${argType} '${invalidArgs[0]}'.`)); - logger.info(cyan('Run webpack --help to see available commands and arguments.')); + logger.info(cyan('Run webpack --help to see available commands and arguments.\n')); process.exit(2); } const pkgJSON = require('../../package.json'); const webpack = require('webpack'); logger.raw(`\nwebpack-cli ${pkgJSON.version}`); - logger.raw(`\nwebpack ${webpack.version}`); + logger.raw(`\nwebpack ${webpack.version}\n`); } run() { diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index 55ad3f4148e..6a8af1841c5 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -54,8 +54,7 @@ async function prompter() { async function run() { try { const args = await prompter(); - logger.raw(); - logger.info('Executing CLI\n'); + logger.info('\nExecuting CLI\n'); await runCLI(args); } catch (err) { logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible options.`); diff --git a/test/migrate/config/migrate-config.test.js b/test/migrate/config/migrate-config.test.js index e364bd5bb08..a22e1f7a705 100644 --- a/test/migrate/config/migrate-config.test.js +++ b/test/migrate/config/migrate-config.test.js @@ -1,9 +1,9 @@ 'use strict'; +const { red } = require('colorette'); const fs = require('fs'); const path = require('path'); const rimraf = require('rimraf'); -const { red } = require('colorette'); const { run, runAndGetWatchProc, runPromptWithAnswers } = require('../../utils/test-utils'); const ENTER = '\x0D';