Skip to content

Commit

Permalink
refactor: use logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Aug 17, 2020
1 parent d3e2936 commit e5ecf3c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"p-lazy": "3.0.0"
},
"peerDependencies": {
"webpack-cli": "4.x.x",
"webpack": "4.x.x || 5.x.x"
},
"devDependencies": {
Expand Down
25 changes: 13 additions & 12 deletions packages/migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`));
}
});

Expand All @@ -121,7 +122,7 @@ function runMigration(currentConfigPath: string, outputConfigPath: string): Prom
},
]);
} else {
console.error(red('✖ Migration aborted'));
logger.error('✖ Migration aborted');
}
},
)
Expand All @@ -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;
});
}
Expand All @@ -175,7 +176,7 @@ export default function migrate(...args: string[]): void | Promise<void> {
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;
}

Expand All @@ -194,7 +195,7 @@ export default function migrate(...args: string[]): void | Promise<void> {
])
.then((ans: { confirmPath: boolean }): void | Promise<void> => {
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]);
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/groups/BasicGroup.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -36,7 +36,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');
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/webpack-cli/lib/utils/logger.js
Original file line number Diff line number Diff line change
@@ -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),
};
2 changes: 1 addition & 1 deletion test/migrate/config/migrate-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit e5ecf3c

Please sign in to comment.