From a3c83d63651f93cde725592f2aa6f2c96e2a5839 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 12 Apr 2021 15:59:32 +0530 Subject: [PATCH] feat: add `create` and `new` alias for `init` --- packages/generators/src/index.ts | 2 +- packages/webpack-cli/lib/webpack-cli.js | 2 +- .../__snapshots__/init.test.js.snap.webpack4 | 63 +++++++++++++++++++ .../__snapshots__/init.test.js.snap.webpack5 | 63 +++++++++++++++++++ test/init/init.test.js | 48 ++++++++++++++ 5 files changed, 176 insertions(+), 2 deletions(-) diff --git a/packages/generators/src/index.ts b/packages/generators/src/index.ts index 4d862c0cebe..f7ab03a5c35 100644 --- a/packages/generators/src/index.ts +++ b/packages/generators/src/index.ts @@ -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', diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index d080c9b6595..cd79d1a7713 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -654,7 +654,7 @@ class WebpackCLI { }, { name: 'init', - alias: 'c', + alias: ['c', 'create', 'new'], pkg: '@webpack-cli/generators', }, { diff --git a/test/init/__snapshots__/init.test.js.snap.webpack4 b/test/init/__snapshots__/init.test.js.snap.webpack4 index 6cc10a447af..3d69b880770 100644 --- a/test/init/__snapshots__/init.test.js.snap.webpack4 +++ b/test/init/__snapshots__/init.test.js.snap.webpack4 @@ -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", diff --git a/test/init/__snapshots__/init.test.js.snap.webpack5 b/test/init/__snapshots__/init.test.js.snap.webpack5 index 6cc10a447af..3d69b880770 100644 --- a/test/init/__snapshots__/init.test.js.snap.webpack5 +++ b/test/init/__snapshots__/init.test.js.snap.webpack5 @@ -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", diff --git a/test/init/init.test.js b/test/init/init.test.js index 706f34fe766..f829f3bf896 100644 --- a/test/init/init.test.js +++ b/test/init/init.test.js @@ -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(); + }); });