From 44fa20c5b4f791c44e2e3993f8c613d16dcc1bcd Mon Sep 17 00:00:00 2001 From: Pranshu Chittora Date: Wed, 31 Jul 2019 02:18:45 +0530 Subject: [PATCH] fix(init): fixes config resolution on generating new configuration --- packages/utils/modify-config-helper.ts | 171 ++++++++++++------------- 1 file changed, 85 insertions(+), 86 deletions(-) diff --git a/packages/utils/modify-config-helper.ts b/packages/utils/modify-config-helper.ts index 2b5baaeb99c..4a924ff26df 100644 --- a/packages/utils/modify-config-helper.ts +++ b/packages/utils/modify-config-helper.ts @@ -1,29 +1,29 @@ -import chalk from "chalk"; -import * as fs from "fs"; -import * as path from "path"; -import * as yeoman from "yeoman-environment"; -import * as Generator from "yeoman-generator"; +import chalk from 'chalk'; +import * as fs from 'fs'; +import * as path from 'path'; +import * as yeoman from 'yeoman-environment'; +import * as Generator from 'yeoman-generator'; -import { TransformConfig } from "./types/Config"; -import runTransform from "./scaffold"; +import { TransformConfig } from './types/Config'; +import runTransform from './scaffold'; export interface Config extends Object { - item?: { - name: string; - }; - topScope?: string[]; - configName?: string; - merge: string | string[]; - webpackOptions: object; + item?: { + name: string; + }; + topScope?: string[]; + configName?: string; + merge: string | string[]; + webpackOptions: object; } export interface TransformConfig extends Object { - configPath?: string; - configFile?: string; - config?: Config; + configPath?: string; + configFile?: string; + config?: Config; } -const DEFAULT_WEBPACK_CONFIG_FILENAME = "webpack.config.js"; +const DEFAULT_WEBPACK_CONFIG_FILENAME = 'webpack.config.js'; /** * @@ -37,77 +37,76 @@ const DEFAULT_WEBPACK_CONFIG_FILENAME = "webpack.config.js"; * @returns {Function} runTransform - Returns a transformation instance */ -export default function modifyHelperUtil( - action: string, - generator: typeof Generator, - configFile: string = DEFAULT_WEBPACK_CONFIG_FILENAME, - packages?: string[], - autoSetDefaults: boolean = false -): any { - let configPath: string | null = null; +export default function modifyHelperUtil(action: string, generator: typeof Generator, configFile: string = DEFAULT_WEBPACK_CONFIG_FILENAME, packages?: string[], autoSetDefaults: boolean = false): any { + let configPath: string | null = null; - const env = yeoman.createEnv("webpack", null); - const generatorName = "webpack-init-generator"; + const env = yeoman.createEnv('webpack', null); + const generatorName = 'webpack-init-generator'; - if (!generator) { - generator = class extends Generator { - public initializing(): void { - packages.forEach( - (pkgPath: string): Generator => { - return this.composeWith(require.resolve(pkgPath), {}); - } - ); - } - }; - } + if (!generator) { + generator = class extends Generator { + public initializing(): void { + packages.forEach( + (pkgPath: string): Generator => { + return this.composeWith(require.resolve(pkgPath), {}); + }, + ); + } + }; + } - env.registerStub(generator, generatorName); - env.run(generatorName, { - configFile, - autoSetDefaults - }) - .then((): void => { - let configModule: object; - try { - const confPath = path.resolve(process.cwd(), ".yo-rc.json"); - configModule = require(confPath); - // Change structure of the config to be transformed - const tmpConfig: object = {}; - Object.keys(configModule).forEach((prop: string): void => { - const configs = Object.keys(configModule[prop].configuration); - configs.forEach((conf: string): void => { - tmpConfig[conf] = configModule[prop].configuration[conf]; - }); - }); - configModule = tmpConfig; - } catch (err) { - console.error(chalk.red("\nCould not find a yeoman configuration file.\n")); - console.error( - chalk.red( - "\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n" - ) - ); - Error.stackTraceLimit = 0; - process.exitCode = -1; - } - const transformConfig: TransformConfig = Object.assign( - { - configFile: !configPath ? null : fs.readFileSync(configPath, "utf8"), - configPath - }, - configModule - ); - return runTransform(transformConfig, "init"); - }) - .catch((err): void => { - console.error( - chalk.red( - ` + env.registerStub(generator, generatorName); + env.run(generatorName, { + configFile, + autoSetDefaults, + }) + .then((): void => { + let configModule: object; + try { + const confPath = path.resolve(process.cwd(), '.yo-rc.json'); + configModule = require(confPath); + const packageName: string = require(path.resolve(process.cwd(), 'package.json')).name; + + // Change structure of the config to be transformed + const tmpConfig: object = {}; + Object.keys(configModule) + .filter((config: string): boolean => { + if (packageName) { + return config === packageName; + } + return config === '*'; + }) + .forEach((prop: string): void => { + const configs = Object.keys(configModule[prop].configuration); + configs.forEach((conf: string): void => { + tmpConfig[conf] = configModule[prop].configuration[conf]; + }); + }); + configModule = tmpConfig; + } catch (err) { + console.error(chalk.red('\nCould not find a yeoman configuration file.\n')); + console.error(chalk.red("\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n")); + Error.stackTraceLimit = 0; + process.exitCode = -1; + } + const transformConfig: TransformConfig = Object.assign( + { + configFile: !configPath ? null : fs.readFileSync(configPath, 'utf8'), + configPath, + }, + configModule, + ); + return runTransform(transformConfig, 'init'); + }) + .catch((err): void => { + console.error( + chalk.red( + ` Unexpected Error please file an issue here https://github.com/webpack/webpack-cli/issues/new?template=Bug_report.md - ` - ) - ); - console.error(err); - }); + `, + ), + ); + console.error(err); + }); }