diff --git a/packages/init/src/index.ts b/packages/init/src/index.ts index 70a72177b9d..4812f07e68b 100644 --- a/packages/init/src/index.ts +++ b/packages/init/src/index.ts @@ -2,7 +2,7 @@ import { initGenerator } from '@webpack-cli/generators'; import { modifyHelperUtil, npmPackagesExists } from '@webpack-cli/utils'; const AUTO_PREFIX = '--auto'; - +const CONFIG_PREFIX = '--force'; /** * * First function to be called after running the init flag. This is a check, @@ -17,8 +17,9 @@ const AUTO_PREFIX = '--auto'; export default function initializeInquirer(...args: string[]): Function | void { const packages = args; const includesDefaultPrefix = packages.includes(AUTO_PREFIX); - if (packages.length === 0 || includesDefaultPrefix) { - return modifyHelperUtil('init', initGenerator, null, null, includesDefaultPrefix); + const generateConfig = packages.includes(CONFIG_PREFIX); + if (packages.length === 0 || includesDefaultPrefix || generateConfig) { + return modifyHelperUtil('init', initGenerator, null, null, includesDefaultPrefix, generateConfig); } return npmPackagesExists(packages); } diff --git a/packages/utils/src/modify-config-helper.ts b/packages/utils/src/modify-config-helper.ts index 1aa8f6e9f47..ab67a5bdaeb 100644 --- a/packages/utils/src/modify-config-helper.ts +++ b/packages/utils/src/modify-config-helper.ts @@ -51,6 +51,7 @@ export function modifyHelperUtil( configFile: string = DEFAULT_WEBPACK_CONFIG_FILENAME, packages?: string[], autoSetDefaults = false, + generateConfig = false, ): void { const configPath: string | null = null; @@ -145,8 +146,9 @@ export function modifyHelperUtil( const successMessage = `\nYou can now run ${chalk.green(runCommand)} to bundle your application!\n\n`; process.stdout.write(`\n${successMessage}`); } + // scaffold webpack config file from using .yo-rc.json - return runTransform(transformConfig, 'init'); + return runTransform(transformConfig, 'init', generateConfig); }) .catch((err): void => { console.error( diff --git a/packages/utils/src/scaffold.ts b/packages/utils/src/scaffold.ts index 97c0813815b..bc1e8455fda 100644 --- a/packages/utils/src/scaffold.ts +++ b/packages/utils/src/scaffold.ts @@ -35,10 +35,13 @@ function mapOptionsToTransform(config: Config): string[] { * and writes the file */ -export function runTransform(transformConfig: TransformConfig, action: string): void { +export function runTransform(transformConfig: TransformConfig, action: string, generateConfig: boolean): void { // webpackOptions.name sent to nameTransform if match const webpackConfig = Object.keys(transformConfig).filter((p: string): boolean => { - return p !== 'configFile' && p !== 'configPath' && p !== 'usingDefaults'; + if (p == 'usingDefaults') { + return generateConfig; + } + return p !== 'configFile' && p !== 'configPath'; }); const initActionNotDefined = action && action !== 'init' ? true : false; diff --git a/test/init/force/init-force.test.js b/test/init/force/init-force.test.js new file mode 100644 index 00000000000..a22fb47577a --- /dev/null +++ b/test/init/force/init-force.test.js @@ -0,0 +1,53 @@ +'use strict'; + +const fs = require('fs'); +const { join, resolve } = require('path'); +const rimraf = require('rimraf'); +const { runPromptWithAnswers } = require('../../utils/test-utils'); +const firstPrompt = 'Will your application have multiple bundles?'; + +const ENTER = '\x0D'; +const genPath = join(__dirname, 'test-assets'); + +jest.setTimeout(100000); + +describe('init force flag', () => { + beforeAll(() => { + rimraf.sync(genPath); + fs.mkdirSync(genPath); + }); + + afterAll(() => { + rimraf.sync(genPath); + }); + + it('should scaffold webpack config', async () => { + const { stdout } = await runPromptWithAnswers(genPath, ['init', '--force'], [`N${ENTER}`, ENTER, ENTER, ENTER, ENTER, ENTER]); + + expect(stdout).toBeTruthy(); + expect(stdout).toContain(firstPrompt); + + // Skip test in case installation fails + if (!fs.existsSync(resolve(genPath, './yarn.lock'))) { + return; + } + + // Test regressively files are scaffolded + const files = ['./sw.js', './package.json', './src/index.js', './webpack.config.js']; + + // eslint-disable-next-line prettier/prettier + files.forEach((file) => { + expect(fs.existsSync(resolve(genPath, file))).toBeTruthy(); + }); + + // Check package json is correctly configured + const pkgJsonTests = () => { + const pkgJson = require(join(genPath, './package.json')); + expect(pkgJson).toBeTruthy(); + expect(pkgJson['devDependencies']).toBeTruthy(); + expect(pkgJson['devDependencies']['webpack']).toBeTruthy(); + expect(pkgJson['scripts']['build'] == 'webpack').toBeTruthy(); + }; + expect(pkgJsonTests).not.toThrow(); + }); +});