Skip to content

Commit

Permalink
feat: add flag to force config
Browse files Browse the repository at this point in the history
Co-authored-by: James George <[email protected]>
  • Loading branch information
rishabh3112 and jamesgeorge007 authored Jun 2, 2020
1 parent 616187b commit f61e7e0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/init/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
4 changes: 3 additions & 1 deletion packages/utils/src/modify-config-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/src/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
53 changes: 53 additions & 0 deletions test/init/force/init-force.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit f61e7e0

Please sign in to comment.