Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add create and new alias for init #2616

Merged
merged 5 commits into from Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/generators/src/index.ts
Expand Up @@ -12,7 +12,7 @@ class GeneratorsCommand {
await cli.makeCommand(
{
name: 'init [generation-path]',
alias: 'c',
alias: ['c', 'create', 'new'],
description: 'Initialize a new webpack project.',
usage: '[generation-path] [options]',
pkg: '@webpack-cli/generators',
Expand Down
7 changes: 5 additions & 2 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -24,7 +24,10 @@ class WebpackCLI {

async makeCommand(commandOptions, options, action) {
const alreadyLoaded = this.program.commands.find(
(command) => command.name() === commandOptions.name || command.aliases().includes(commandOptions.alias),
(command) =>
command.name() === commandOptions.name ||
command.aliases().includes(commandOptions.alias) ||
JSON.stringify(command.aliases()) === JSON.stringify(commandOptions.alias),
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
);

if (alreadyLoaded) {
Expand Down Expand Up @@ -654,7 +657,7 @@ class WebpackCLI {
},
{
name: 'init',
alias: 'c',
alias: ['c', 'create', 'new'],
pkg: '@webpack-cli/generators',
},
{
Expand Down
362 changes: 181 additions & 181 deletions test/help/__snapshots__/help.test.js.snap.webpack5

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/help/help.test.js
Expand Up @@ -74,7 +74,7 @@ describe('help', () => {
{
name: 'init',
alias: 'c',
usage: 'webpack init|c [generation-path] [options]',
usage: 'webpack init|c|create|new [generation-path] [options]',
},
{
name: 'info',
Expand Down
63 changes: 63 additions & 0 deletions test/init/__snapshots__/init.test.js.snap.webpack4
Expand Up @@ -365,6 +365,69 @@ module.exports = {
"
`;

exports[`init command should should work with 'c' alias 1`] = `
Object {
"description": "My webpack project",
"devDependencies": Object {
"html-webpack-plugin": "x.x.x",
"webpack": "x.x.x",
"webpack-cli": "x.x.x",
"webpack-dev-server": "x.x.x",
},
"name": "my-webpack-project",
"scripts": Object {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"serve": "webpack serve",
"watch": "webpack --watch",
},
"version": "1.0.0",
}
`;

exports[`init command should should work with 'create' alias 1`] = `
Object {
"description": "My webpack project",
"devDependencies": Object {
"html-webpack-plugin": "x.x.x",
"webpack": "x.x.x",
"webpack-cli": "x.x.x",
"webpack-dev-server": "x.x.x",
},
"name": "my-webpack-project",
"scripts": Object {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"serve": "webpack serve",
"watch": "webpack --watch",
},
"version": "1.0.0",
}
`;

exports[`init command should should work with 'new' alias 1`] = `
Object {
"description": "My webpack project",
"devDependencies": Object {
"html-webpack-plugin": "x.x.x",
"webpack": "x.x.x",
"webpack-cli": "x.x.x",
"webpack-dev-server": "x.x.x",
},
"name": "my-webpack-project",
"scripts": Object {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"serve": "webpack serve",
"watch": "webpack --watch",
},
"version": "1.0.0",
}
`;

exports[`init command should use less in project when selected 1`] = `
Object {
"description": "My webpack project",
Expand Down
63 changes: 63 additions & 0 deletions test/init/__snapshots__/init.test.js.snap.webpack5
Expand Up @@ -365,6 +365,69 @@ module.exports = {
"
`;

exports[`init command should should work with 'c' alias 1`] = `
Object {
"description": "My webpack project",
"devDependencies": Object {
"html-webpack-plugin": "x.x.x",
"webpack": "x.x.x",
"webpack-cli": "x.x.x",
"webpack-dev-server": "x.x.x",
},
"name": "my-webpack-project",
"scripts": Object {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"serve": "webpack serve",
"watch": "webpack --watch",
},
"version": "1.0.0",
}
`;

exports[`init command should should work with 'create' alias 1`] = `
Object {
"description": "My webpack project",
"devDependencies": Object {
"html-webpack-plugin": "x.x.x",
"webpack": "x.x.x",
"webpack-cli": "x.x.x",
"webpack-dev-server": "x.x.x",
},
"name": "my-webpack-project",
"scripts": Object {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"serve": "webpack serve",
"watch": "webpack --watch",
},
"version": "1.0.0",
}
`;

exports[`init command should should work with 'new' alias 1`] = `
Object {
"description": "My webpack project",
"devDependencies": Object {
"html-webpack-plugin": "x.x.x",
"webpack": "x.x.x",
"webpack-cli": "x.x.x",
"webpack-dev-server": "x.x.x",
},
"name": "my-webpack-project",
"scripts": Object {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"serve": "webpack serve",
"watch": "webpack --watch",
},
"version": "1.0.0",
}
`;

exports[`init command should use less in project when selected 1`] = `
Object {
"description": "My webpack project",
Expand Down
48 changes: 48 additions & 0 deletions test/init/init.test.js
Expand Up @@ -380,4 +380,52 @@ describe('init command', () => {
expect(exitCode).toBe(2);
expect(stderr).toContain('Failed to create directory');
});

it("should should work with 'new' alias", async () => {
const assetsPath = await uniqueDirectoryForTest(rootAssetsPath);
const { stdout, stderr } = await run(assetsPath, ['new', '--force']);
expect(stdout).toContain('Project has been initialised with webpack!');
expect(stderr).toContain('webpack.config.js');

// Test files
const files = ['package.json', 'src', 'src/index.js', 'webpack.config.js'];
files.forEach((file) => {
expect(existsSync(resolve(assetsPath, file))).toBeTruthy();
});

// Check if the generated package.json file content matches the snapshot
expect(readFromPkgJSON(assetsPath)).toMatchSnapshot();
});

it("should should work with 'create' alias", async () => {
const assetsPath = await uniqueDirectoryForTest(rootAssetsPath);
const { stdout, stderr } = await run(assetsPath, ['create', '--force']);
expect(stdout).toContain('Project has been initialised with webpack!');
expect(stderr).toContain('webpack.config.js');

// Test files
const files = ['package.json', 'src', 'src/index.js', 'webpack.config.js'];
files.forEach((file) => {
expect(existsSync(resolve(assetsPath, file))).toBeTruthy();
});

// Check if the generated package.json file content matches the snapshot
expect(readFromPkgJSON(assetsPath)).toMatchSnapshot();
});

it("should should work with 'c' alias", async () => {
const assetsPath = await uniqueDirectoryForTest(rootAssetsPath);
const { stdout, stderr } = await run(assetsPath, ['c', '--force']);
expect(stdout).toContain('Project has been initialised with webpack!');
expect(stderr).toContain('webpack.config.js');

// Test files
const files = ['package.json', 'src', 'src/index.js', 'webpack.config.js'];
files.forEach((file) => {
expect(existsSync(resolve(assetsPath, file))).toBeTruthy();
});

// Check if the generated package.json file content matches the snapshot
expect(readFromPkgJSON(assetsPath)).toMatchSnapshot();
});
});