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

fix(init): fixes config resolution on generating new configuration #977

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 85 additions & 86 deletions packages/utils/modify-config-helper.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
*
Expand All @@ -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);
});
}