Skip to content

Commit

Permalink
refactor: revert logger
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk committed Nov 6, 2020
1 parent 6d8140d commit e3bf375
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 142 deletions.
12 changes: 6 additions & 6 deletions packages/webpack-cli/__tests__/info.test.js
Expand Up @@ -3,23 +3,23 @@ const path = require('path');

describe('Info', () => {
it('should run with cli', () => {
const { stderr } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info'], {
const { stdout } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info'], {
cwd: path.resolve(__dirname),
reject: false,
});
expect(stderr).toContain('System');
expect(stderr).toContain('Binaries');
expect(stderr).toContain('OS');
expect(stdout).toContain('System');
expect(stdout).toContain('Binaries');
expect(stdout).toContain('OS');
});

it('should work with flags', () => {
const { stderr } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info', '--output=json'], {
const { stdout } = spawnSync(path.resolve(__dirname, '../bin/cli.js'), ['info', '--output=json'], {
cwd: path.resolve(__dirname),
reject: false,
});

const testJSON = () => {
const output = JSON.parse(stderr);
const output = JSON.parse(stdout);
expect(output['System']).toBeTruthy();
expect(output['System']['OS']).toBeTruthy();
};
Expand Down
26 changes: 6 additions & 20 deletions packages/webpack-cli/lib/utils/logger.js
@@ -1,25 +1,11 @@
const util = require('util');
const { red, cyan, yellow, green } = require('colorette');

const write = (prefix = '[webpack-cli] ', color = (val) => val) => {
return (...args) => {
const str = prefix + color(util.format(...args));
process.stderr.write(str + '\n');
};
};

module.exports = {
error: write(undefined, red),
warn: write(undefined, yellow),
info: write(undefined, cyan),
log: write(),

/**
* @deprecated use info
*/
success: write(undefined, green),
/**
* @deprecated use info
*/
raw: write(''),
error: (val) => console.error(`[webpack-cli] ${red(util.format(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}`),
raw: (val) => console.log(val),
};
2 changes: 1 addition & 1 deletion test/core-flags/bail-flag.test.js
Expand Up @@ -2,7 +2,7 @@

const { run } = require('../utils/test-utils');

describe.skip('--bail flag', () => {
describe('--bail flag', () => {
it('should set bail to true', () => {
const { stdout, exitCode } = run(__dirname, ['--bail']);

Expand Down
4 changes: 2 additions & 2 deletions test/core-flags/cache-flags.test.js
Expand Up @@ -122,8 +122,8 @@ describe('cache related flags from core', () => {
it('should assign cache build dependencies with default config', () => {
// TODO: Fix on windows
if (isWindows) return;
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem']);
expect(stderr).toBeFalsy();
const { stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem']);

expect(stdout).toContain('buildDependencies');
expect(stdout).toContain(`'${path.join(__dirname, './webpack.config.js')}'`);
expect(stdout).toContain("type: 'filesystem'");
Expand Down
23 changes: 11 additions & 12 deletions test/help/help-commands.test.js
Expand Up @@ -5,41 +5,40 @@ const helpHeader = 'The build tool for modern web applications';

describe('commands help', () => {
it('shows help for subcommands', () => {
const { stderr, exitCode } = run(__dirname, ['serve', 'help'], false);
const { stdout, exitCode } = run(__dirname, ['serve', 'help'], false);

expect(exitCode).toBe(0);

expect(stderr).toContain('webpack s | serve');
expect(stdout).toContain('webpack s | serve');
});

it('shows help information with subcommands as an arg', () => {
const { stderr, exitCode } = run(__dirname, ['help', 'serve'], false);
const { stdout, exitCode } = run(__dirname, ['help', 'serve'], false);

expect(exitCode).toBe(0);
expect(stderr).toContain('webpack s | serve');
expect(stdout).toContain('webpack s | serve');
});

it('shows warning for invalid command with --help flag', () => {
const { stderr, exitCode } = run(__dirname, ['--help', 'myCommand'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['--help', 'myCommand'], false);

expect(exitCode).toBe(0);
expect(stderr).toContain(`You provided an invalid command 'myCommand'`);
expect(stderr).toContain(helpHeader);
expect(stdout).toContain(helpHeader);
});

it('shows warning for invalid command with help command', () => {
const { stderr, exitCode } = run(__dirname, ['help', 'myCommand'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['help', 'myCommand'], false);

expect(exitCode).toBe(0);
expect(stderr).toContain(`You provided an invalid command 'myCommand'`);
expect(stderr).toContain(helpHeader);
expect(stdout).toContain(helpHeader);
});

it('gives precedence to earlier command in case of multiple commands', () => {
const { stderr, exitCode } = run(__dirname, ['--help', 'init', 'info'], false);
const { stdout, exitCode } = run(__dirname, ['--help', 'init', 'info'], false);

expect(exitCode).toBe(0);
expect(stderr).not.toContain(helpHeader);
expect(stderr).toContain('webpack c | init [scaffold]');
expect(stdout).not.toContain(helpHeader);
expect(stdout).toContain('webpack c | init [scaffold]');
});
});
22 changes: 11 additions & 11 deletions test/help/help-flags.test.js
Expand Up @@ -5,43 +5,43 @@ const helpHeader = 'The build tool for modern web applications';

describe('commands help', () => {
it('log warning for invalid flag with --help flag', () => {
const { stderr, exitCode } = run(__dirname, ['--help', '--my-flag'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['--help', '--my-flag'], false);

expect(exitCode).toBe(0);
expect(stderr).toContain(`You provided an invalid option '--my-flag'`);
expect(stderr).toContain(helpHeader);
expect(stdout).toContain(helpHeader);
});

it('log warning for invalid flag with help command', () => {
const { stderr, exitCode } = run(__dirname, ['help', '--my-flag'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['help', '--my-flag'], false);

expect(exitCode).toBe(0);
expect(stderr).toContain(`You provided an invalid option '--my-flag'`);
expect(stderr).toContain(helpHeader);
expect(stdout).toContain(helpHeader);
});

it('shows flag help with valid flag', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['--help', '--merge'], false);

expect(exitCode).toBe(0);
expect(stdout).not.toContain(helpHeader);
expect(stderr).toContain('webpack -m, --merge');
expect(stderr).not.toContain(helpHeader);
expect(stdout).toContain('webpack -m, --merge');
});

it('should show help for --mode', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['--mode', '--help'], false);

expect(exitCode).toBe(0);
expect(stdout).not.toContain(helpHeader);
expect(stderr).toContain('webpack --mode <development | production | none>');
expect(stderr).toContain('Defines the mode to pass to webpack');
expect(stderr).not.toContain(helpHeader);
expect(stdout).toContain('webpack --mode <development | production | none>');
expect(stdout).toContain('Defines the mode to pass to webpack');
});

it('gives precedence to earlier flag in case of multiple flags', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['--help', '--entry', '--merge'], false);

expect(exitCode).toBe(0);
expect(stdout).not.toContain(helpHeader);
expect(stderr).toContain('webpack --entry <path to entry file>');
expect(stderr).not.toContain(helpHeader);
expect(stdout).toContain('webpack --entry <path to entry file>');
});
});
12 changes: 6 additions & 6 deletions test/help/help-multi-args.test.js
Expand Up @@ -7,21 +7,21 @@ const helpHeader = 'The build tool for modern web applications';
describe('help cmd with multiple arguments', () => {
commands.forEach((cmd) => {
it(`shows cmd help with ${cmd.name}`, () => {
const { stderr, exitCode } = run(__dirname, ['--help', `${cmd.name}`], false);
const { stdout, stderr, exitCode } = run(__dirname, ['--help', `${cmd.name}`], false);

expect(exitCode).toBe(0);
expect(stderr).not.toContain(helpHeader);
expect(stderr).toContain(`${cmd.name}`);
expect(stderr).toContain(`${cmd.usage}`);
expect(stderr).toContain(`${cmd.description}`);
expect(stdout).toContain(`${cmd.name}`);
expect(stdout).toContain(`${cmd.usage}`);
expect(stdout).toContain(`${cmd.description}`);
});
});

it('should output help for --version by taking precedence', () => {
const { stderr, exitCode } = run(__dirname, ['--help', '--version'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['--help', '--version'], false);

expect(exitCode).toBe(0);
expect(stderr).not.toContain(helpHeader);
expect(stderr).toContain('webpack -v, --version');
expect(stdout).toContain('webpack -v, --version');
});
});
14 changes: 7 additions & 7 deletions test/help/help-single-arg.test.js
Expand Up @@ -6,30 +6,30 @@ const helpHeader = 'The build tool for modern web applications';

describe('single help flag', () => {
it('respects --no-color flag', () => {
const { stderr, exitCode } = run(__dirname, ['--help', '--no-color'], false);
const { stdout, stderr, exitCode } = run(__dirname, ['--help', '--no-color'], false);
const usage = 'webpack [...options] | <command>';
const example = 'webpack help --flag | <command>';
options.enabled = true;

expect(exitCode).toBe(0);
expect(stderr).not.toContain(yellow(usage));
expect(stderr).not.toContain(yellow(example));
expect(stderr).toContain(usage);
expect(stderr).toContain(example);
expect(stdout).toContain(usage);
expect(stdout).toContain(example);
});

it('outputs help info with command syntax', () => {
const { stderr, exitCode } = run(__dirname, ['help'], false);
const { stdout, exitCode } = run(__dirname, ['help'], false);

expect(exitCode).toBe(0);
expect(stderr).toContain(helpHeader);
expect(stdout).toContain(helpHeader);
});

it('outputs help info with dashed syntax', () => {
const { stderr, exitCode } = run(__dirname, ['--help'], false);
const { stdout, exitCode } = run(__dirname, ['--help'], false);

expect(exitCode).toBe(0);
expect(stderr).toContain(helpHeader);
expect(stdout).toContain(helpHeader);
});

it('creates a readable snapshot', () => {
Expand Down
16 changes: 8 additions & 8 deletions test/info/info-help.test.js
Expand Up @@ -11,26 +11,26 @@ const descriptionText = 'Outputs information about your system and dependencies'

describe('should print help for info command', () => {
it('shows usage information on supplying help flag', () => {
const { stderr, exitCode } = runInfo(['--help'], __dirname);
const { stdout, exitCode } = runInfo(['--help'], __dirname);

expect(exitCode).toBe(0);
expect(stderr).toContain(usageText);
expect(stderr).toContain(descriptionText);
expect(stdout).toContain(usageText);
expect(stdout).toContain(descriptionText);
});

it('should respect the --no-color flag', () => {
const { stderr, exitCode } = runInfo(['--help', '--no-color'], __dirname);
const { stdout, exitCode } = runInfo(['--help', '--no-color'], __dirname);
options.enabled = true;

expect(exitCode).toBe(0);
expect(stderr).not.toContain(yellow(usageText));
expect(stderr).toContain(descriptionText);
expect(stdout).not.toContain(yellow(usageText));
expect(stdout).toContain(descriptionText);
});

it('should output all cli flags', () => {
const { stderr, exitCode } = runInfo(['--help'], __dirname);
const { stdout, exitCode } = runInfo(['--help'], __dirname);

infoFlags.forEach((flag) => expect(stderr).toContain(`--${flag.name}`));
infoFlags.forEach((flag) => expect(stdout).toContain(`--${flag.name}`));

expect(exitCode).toBe(0);
});
Expand Down
20 changes: 10 additions & 10 deletions test/info/info-output.test.js
Expand Up @@ -5,19 +5,19 @@ const { runInfo } = require('../utils/test-utils');

describe('basic info usage', () => {
it('gets info without flags', () => {
const { stderr } = runInfo([], __dirname);
expect(stderr).toContain('System:');
expect(stderr).toContain('Node');
expect(stderr).toContain('npm');
expect(stderr).toContain('Yarn');
const { stdout } = runInfo([], __dirname);
expect(stdout).toContain('System:');
expect(stdout).toContain('Node');
expect(stdout).toContain('npm');
expect(stdout).toContain('Yarn');
});

it('gets info as json', () => {
const { stderr } = runInfo(['--output="json"'], __dirname);
expect(stderr).toContain('"System":');
const { stdout } = runInfo(['--output="json"'], __dirname);
expect(stdout).toContain('"System":');

const parse = () => {
const output = JSON.parse(stderr);
const output = JSON.parse(stdout);
expect(output['System']).toBeTruthy();
expect(output['Binaries']).toBeTruthy();
expect(output['System']['OS']).toBeTruthy();
Expand All @@ -28,8 +28,8 @@ describe('basic info usage', () => {
});

it('gets info as markdown', () => {
const { stderr } = runInfo(['--output="markdown"'], __dirname);
expect(stderr).toContain('## System:');
const { stdout } = runInfo(['--output="markdown"'], __dirname);
expect(stdout).toContain('## System:');
});

it('shows a warning if an invalid value is supplied', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/init/coreFlags/init-flags.test.js
Expand Up @@ -6,10 +6,10 @@ const firstPrompt = 'Will your application have multiple bundles?';

describe('init with core flags', () => {
it('should output help with --help flag', () => {
const { stdout, stderr } = run(__dirname, ['init', '--help'], false);
const { stdout } = run(__dirname, ['init', '--help'], false);

expect(stdout).not.toContain(firstPrompt);
expect(stderr).toContain('Initialize a new webpack configuration');
expect(stdout).toContain('Initialize a new webpack configuration');
});
it('should throw error with invalid scaffolder package', () => {
const { stderr } = run(__dirname, ['init', 'webpack-rocks'], false);
Expand Down
4 changes: 2 additions & 2 deletions test/serve/basic/serve-basic.test.js
Expand Up @@ -28,10 +28,10 @@ describe('basic serve usage', () => {
}

it('should respect the --no-color flag', async () => {
const { stdout, stderr } = await runServe(['--help', '--no-color'], __dirname);
const { stdout } = await runServe(['--help', '--no-color'], __dirname);
options.enabled = true;
expect(stdout).not.toContain(yellow(usageText));
expect(stderr).toContain(descriptionText);
expect(stdout).toContain(descriptionText);
});

it('should not invoke info subcommand', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unknown/unknown.test.js
Expand Up @@ -8,9 +8,9 @@ describe('unknown behaviour', () => {
expect(exitCode).toBe(2);
});
it('suggests the closest match to an unknown flag', () => {
const { stderr, exitCode } = run(__dirname, ['--entyr', './a.js']);
const { stdout, stderr, exitCode } = run(__dirname, ['--entyr', './a.js']);
expect(stderr).toContain('Unknown argument: --entyr');
expect(stderr).toContain('Did you mean --entry?');
expect(stdout).toContain('Did you mean --entry?');
expect(exitCode).toBe(2);
});
});

0 comments on commit e3bf375

Please sign in to comment.