Skip to content

Commit

Permalink
Merge branch 'feat/serve-integration' of https://github.com/webpack/w…
Browse files Browse the repository at this point in the history
…ebpack-cli into feat/serve-integration
  • Loading branch information
rishabh3112 committed Aug 16, 2020
2 parents 7431ec6 + 39a0601 commit 55867d5
Show file tree
Hide file tree
Showing 11 changed files with 639 additions and 271 deletions.
48 changes: 24 additions & 24 deletions package.json
Expand Up @@ -93,40 +93,40 @@
"webpack": "4.x.x || 5.x.x"
},
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/preset-env": "^7.8.7",
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-lerna-scopes": "^8.3.4",
"@commitlint/config-lerna-scopes": "^9.1.1",
"@types/jest": "^25.1.4",
"@types/node": "13.9.8",
"@typescript-eslint/eslint-plugin": "^2.24.0",
"@typescript-eslint/parser": "^2.26.0",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"colorette": "^1.2.1",
"commitlint": "^8.3.5",
"commitlint-config-cz": "^0.13.0",
"commitlint": "^9.1.2",
"commitlint-config-cz": "^0.13.1",
"concat-stream": "^2.0.0",
"cz-customizable": "^6.2.0",
"del-cli": "^3.0.0",
"cz-customizable": "^6.3.0",
"del-cli": "^3.0.1",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.1.2",
"execa": "^4.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"execa": "^4.0.3",
"get-port": "^5.1.1",
"git-cz": "^4.3.1",
"husky": "^4.2.3",
"git-cz": "^4.7.0",
"husky": "^4.2.5",
"jest": "^25.2.3",
"jest-junit": "^10.0.0",
"jest-junit": "^11.1.0",
"jest-serializer-ansi": "^1.0.3",
"jest-watch-typeahead": "^0.4.2",
"lerna": "^3.20.2",
"lint-staged": "^10.0.8",
"prettier": "^2.0.2",
"jest-watch-typeahead": "^0.5.0",
"lerna": "^3.22.1",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"readable-stream": "^3.6.0",
"rimraf": "^3.0.2",
"ts-jest": "^25.3.0",
"typescript": "^3.9.5",
"webpack": "^4.43.0",
"yeoman-test": "^2.3.0"
"ts-jest": "^25.5.1",
"typescript": "^3.9.7",
"webpack": "^4.44.1",
"yeoman-test": "^2.7.0"
}
}
9 changes: 4 additions & 5 deletions packages/info/src/index.ts
@@ -1,5 +1,5 @@
import { red } from 'colorette';
import envinfo from 'envinfo';
import logger from 'webpack-cli/lib/utils/logger';
import options from './options';
import WebpackCLI from 'webpack-cli';

Expand Down Expand Up @@ -36,10 +36,9 @@ export default async function info(...args): Promise<string[]> {
const infoArgs = parsedArgs.opts;
const envinfoConfig = {};

if (parsedArgs.unknownArgs.some((arg) => ['help', 'version', 'color'].includes(arg))) return;

if (parsedArgs.unknownArgs.length > 0) {
process.stderr.write(`Unknown argument: ${red(parsedArgs.unknownArgs)}\n`);
logger.error(`Unknown argument: ${parsedArgs.unknownArgs}`);
process.exit(2);
}

if (infoArgs.output) {
Expand All @@ -53,7 +52,7 @@ export default async function info(...args): Promise<string[]> {
envinfoConfig['json'] = true;
break;
default:
process.stderr.write(`${red(infoArgs.output)} is not a valid value for output\n`);
logger.error(`${infoArgs.output} is not a valid value for output\n`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/serve/src/index.ts
Expand Up @@ -33,7 +33,7 @@ export default function serve(...args: string[]): void {
parsedWebpackArgs.unknownArgs
.filter((e) => e)
.forEach((unknown) => {
logger.error('Unknown argument:', unknown);
logger.error(`Unknown argument: ${unknown}`);
});
process.exit(2);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/bootstrap.js
Expand Up @@ -53,7 +53,7 @@ async function runCLI(cli, commandIsUsed) {
}
} else if (parsedArgs.unknownArgs.length > 0) {
parsedArgs.unknownArgs.forEach((unknown) => {
logger.warn('Unknown argument:', unknown);
logger.warn(`Unknown argument: ${unknown}`);
});
cliExecuter();
return;
Expand Down
39 changes: 20 additions & 19 deletions packages/webpack-cli/lib/groups/HelpGroup.js
@@ -1,6 +1,7 @@
const { red, yellow, cyan, bold, underline } = require('colorette');
const { core, commands } = require('../utils/cli-flags');
const { defaultCommands } = require('../utils/commands');
const logger = require('../utils/logger');
const commandLineUsage = require('command-line-usage');

class HelpGroup {
Expand All @@ -21,62 +22,62 @@ class HelpGroup {
const description = options.description;
const link = options.link;

process.stdout.write(`${header('Usage')}: ${usage}\n`);
process.stdout.write(`${header('Description')}: ${description}\n`);
logger.help(`${header('Usage')}: ${usage}`);
logger.help(`${header('Description')}: ${description}`);

if (link) {
process.stdout.write(`${header('Documentation')}: ${link}\n`);
logger.help(`${header('Documentation')}: ${link}`);
}

if (options.flags) {
const flags = commandLineUsage({
header: 'Options',
optionList: options.flags,
});
process.stdout.write(flags);
logger.help(flags);
}
} else if (invalidArgs.length > 0) {
const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command';
console.warn(yellow(`\nYou provided an invalid ${argType} '${invalidArgs[0]}'.`));
process.stdout.write(this.run().outputOptions.help);
logger.warn(`You provided an invalid ${argType} '${invalidArgs[0]}'.`);
logger.help(this.run().outputOptions.help);
} else {
process.stdout.write(this.run().outputOptions.help);
logger.help(this.run().outputOptions.help);
}
process.stdout.write('\n Made with ♥️ by the webpack team \n');
logger.help('\n Made with ♥️ by the webpack team');
}

outputVersion(externalPkg, commandsUsed, invalidArgs) {
if (externalPkg && commandsUsed.length === 1 && invalidArgs.length === 0) {
try {
if ([externalPkg.alias, externalPkg.name].some((pkg) => commandsUsed.includes(pkg))) {
const { name, version } = require(`@webpack-cli/${defaultCommands[externalPkg.name]}/package.json`);
process.stdout.write(`\n${name} ${version}`);
logger.help(`\n${name} ${version}`);
} else {
const { name, version } = require(`${externalPkg.name}/package.json`);
process.stdout.write(`\n${name} ${version}`);
logger.help(`\n${name} ${version}`);
}
} catch (e) {
console.error(red('\nError: External package not found.'));
process.exitCode = -1;
logger.error('Error: External package not found.');
process.exitCode = 1;
}
}

if (commandsUsed.length > 1) {
console.error(red('\nYou provided multiple commands. Please use only one command at a time.\n'));
process.exit();
logger.error('You provided multiple commands. Please use only one command at a time.\n');
process.exit(1);
}

if (invalidArgs.length > 0) {
const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command';
console.error(red(`\nError: Invalid ${argType} '${invalidArgs[0]}'.`));
console.info(cyan('Run webpack --help to see available commands and arguments.\n'));
process.exit(-2);
logger.error(red(`Error: Invalid ${argType} '${invalidArgs[0]}'.`));
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');
process.stdout.write(`\nwebpack-cli ${pkgJSON.version}`);
process.stdout.write(`\nwebpack ${webpack.version}\n`);
logger.help(`\nwebpack-cli ${pkgJSON.version}`);
logger.help(`\nwebpack ${webpack.version}\n`);
}

run() {
Expand Down
10 changes: 8 additions & 2 deletions packages/webpack-cli/lib/utils/logger.js
@@ -1,3 +1,9 @@
const { getLogger } = require('webpack/lib/logging/runtime');
const { red, cyan, yellow } = require('colorette');

module.exports = getLogger('webpack-cli');
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)}`),
log: (val) => console.log(`[webpack-cli] ${val}`),
help: (val) => console.log(val),
};
5 changes: 4 additions & 1 deletion test/hot/hot-flag.test.js
Expand Up @@ -2,6 +2,7 @@
const { run } = require('../utils/test-utils');
const { stat } = require('fs');
const { resolve } = require('path');
const { yellow } = require('colorette');

describe('--hot flag', () => {
it('should be successful when --hot is passed', (done) => {
Expand All @@ -19,7 +20,9 @@ describe('--hot flag', () => {
it('should warn when --hot and --no-hot both are passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--no-hot', '--hot']);
expect(stderr).toContain(
'[webpack-cli] You provided both --hot and --no-hot. We will use only the last of these flags that you provided in your CLI arguments',
`[webpack-cli] ${yellow(
'You provided both --hot and --no-hot. We will use only the last of these flags that you provided in your CLI arguments',
)}`,
);
expect(stdout).toContain('HotModuleReplacementPlugin');

Expand Down
6 changes: 3 additions & 3 deletions test/info/info-help.test.js
Expand Up @@ -11,22 +11,22 @@ 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 { stdout, stderr } = runInfo(['help'], __dirname);
const { stdout, stderr } = runInfo(['--help'], __dirname);
expect(stdout).toContain(usageText);
expect(stdout).toContain(descriptionText);
expect(stderr).toHaveLength(0);
});

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

it('should output all cli flags', () => {
const { stdout, stderr } = runInfo(['help'], __dirname);
const { stdout, stderr } = runInfo(['--help'], __dirname);
infoFlags.forEach((flag) => expect(stdout).toContain(`--${flag.name}`));
expect(stderr).toHaveLength(0);
});
Expand Down
2 changes: 1 addition & 1 deletion test/info/info-unknown.test.js
Expand Up @@ -4,6 +4,6 @@ const { runInfo } = require('../utils/test-utils');
describe('should handle unknown args', () => {
it('shows an appropriate warning on supplying unknown args', () => {
const { stderr } = runInfo(['--unknown'], __dirname);
expect(stderr).toContain(`Unknown argument: ${red('--unknown')}`);
expect(stderr).toContain(`[webpack-cli] ${red('Unknown argument: --unknown')}`);
});
});
6 changes: 5 additions & 1 deletion test/no-hot/no-hot.test.js
Expand Up @@ -2,6 +2,8 @@
const { run } = require('../utils/test-utils');
const { stat, readFile } = require('fs');
const { resolve } = require('path');
const { yellow } = require('colorette');

describe('no-hot flag', () => {
it('should be successful when --no-hot is passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--no-hot']);
Expand All @@ -25,7 +27,9 @@ describe('no-hot flag', () => {
it('should warn when --hot and --no-hot both are passed', (done) => {
const { stderr, stdout } = run(__dirname, ['--hot', '--no-hot']);
expect(stderr).toContain(
'[webpack-cli] You provided both --hot and --no-hot. We will use only the last of these flags that you provided in your CLI arguments',
`[webpack-cli] ${yellow(
'You provided both --hot and --no-hot. We will use only the last of these flags that you provided in your CLI arguments',
)}`,
);
expect(stdout).toBeTruthy();

Expand Down

0 comments on commit 55867d5

Please sign in to comment.