Skip to content

Commit 44fa20c

Browse files
fix(init): fixes config resolution on generating new configuration
1 parent 0005910 commit 44fa20c

1 file changed

Lines changed: 85 additions & 86 deletions

File tree

Lines changed: 85 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import chalk from "chalk";
2-
import * as fs from "fs";
3-
import * as path from "path";
4-
import * as yeoman from "yeoman-environment";
5-
import * as Generator from "yeoman-generator";
1+
import chalk from 'chalk';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
import * as yeoman from 'yeoman-environment';
5+
import * as Generator from 'yeoman-generator';
66

7-
import { TransformConfig } from "./types/Config";
8-
import runTransform from "./scaffold";
7+
import { TransformConfig } from './types/Config';
8+
import runTransform from './scaffold';
99

1010
export interface Config extends Object {
11-
item?: {
12-
name: string;
13-
};
14-
topScope?: string[];
15-
configName?: string;
16-
merge: string | string[];
17-
webpackOptions: object;
11+
item?: {
12+
name: string;
13+
};
14+
topScope?: string[];
15+
configName?: string;
16+
merge: string | string[];
17+
webpackOptions: object;
1818
}
1919

2020
export interface TransformConfig extends Object {
21-
configPath?: string;
22-
configFile?: string;
23-
config?: Config;
21+
configPath?: string;
22+
configFile?: string;
23+
config?: Config;
2424
}
2525

26-
const DEFAULT_WEBPACK_CONFIG_FILENAME = "webpack.config.js";
26+
const DEFAULT_WEBPACK_CONFIG_FILENAME = 'webpack.config.js';
2727

2828
/**
2929
*
@@ -37,77 +37,76 @@ const DEFAULT_WEBPACK_CONFIG_FILENAME = "webpack.config.js";
3737
* @returns {Function} runTransform - Returns a transformation instance
3838
*/
3939

40-
export default function modifyHelperUtil(
41-
action: string,
42-
generator: typeof Generator,
43-
configFile: string = DEFAULT_WEBPACK_CONFIG_FILENAME,
44-
packages?: string[],
45-
autoSetDefaults: boolean = false
46-
): any {
47-
let configPath: string | null = null;
40+
export default function modifyHelperUtil(action: string, generator: typeof Generator, configFile: string = DEFAULT_WEBPACK_CONFIG_FILENAME, packages?: string[], autoSetDefaults: boolean = false): any {
41+
let configPath: string | null = null;
4842

49-
const env = yeoman.createEnv("webpack", null);
50-
const generatorName = "webpack-init-generator";
43+
const env = yeoman.createEnv('webpack', null);
44+
const generatorName = 'webpack-init-generator';
5145

52-
if (!generator) {
53-
generator = class extends Generator {
54-
public initializing(): void {
55-
packages.forEach(
56-
(pkgPath: string): Generator => {
57-
return this.composeWith(require.resolve(pkgPath), {});
58-
}
59-
);
60-
}
61-
};
62-
}
46+
if (!generator) {
47+
generator = class extends Generator {
48+
public initializing(): void {
49+
packages.forEach(
50+
(pkgPath: string): Generator => {
51+
return this.composeWith(require.resolve(pkgPath), {});
52+
},
53+
);
54+
}
55+
};
56+
}
6357

64-
env.registerStub(generator, generatorName);
65-
env.run(generatorName, {
66-
configFile,
67-
autoSetDefaults
68-
})
69-
.then((): void => {
70-
let configModule: object;
71-
try {
72-
const confPath = path.resolve(process.cwd(), ".yo-rc.json");
73-
configModule = require(confPath);
74-
// Change structure of the config to be transformed
75-
const tmpConfig: object = {};
76-
Object.keys(configModule).forEach((prop: string): void => {
77-
const configs = Object.keys(configModule[prop].configuration);
78-
configs.forEach((conf: string): void => {
79-
tmpConfig[conf] = configModule[prop].configuration[conf];
80-
});
81-
});
82-
configModule = tmpConfig;
83-
} catch (err) {
84-
console.error(chalk.red("\nCould not find a yeoman configuration file.\n"));
85-
console.error(
86-
chalk.red(
87-
"\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n"
88-
)
89-
);
90-
Error.stackTraceLimit = 0;
91-
process.exitCode = -1;
92-
}
93-
const transformConfig: TransformConfig = Object.assign(
94-
{
95-
configFile: !configPath ? null : fs.readFileSync(configPath, "utf8"),
96-
configPath
97-
},
98-
configModule
99-
);
100-
return runTransform(transformConfig, "init");
101-
})
102-
.catch((err): void => {
103-
console.error(
104-
chalk.red(
105-
`
58+
env.registerStub(generator, generatorName);
59+
env.run(generatorName, {
60+
configFile,
61+
autoSetDefaults,
62+
})
63+
.then((): void => {
64+
let configModule: object;
65+
try {
66+
const confPath = path.resolve(process.cwd(), '.yo-rc.json');
67+
configModule = require(confPath);
68+
const packageName: string = require(path.resolve(process.cwd(), 'package.json')).name;
69+
70+
// Change structure of the config to be transformed
71+
const tmpConfig: object = {};
72+
Object.keys(configModule)
73+
.filter((config: string): boolean => {
74+
if (packageName) {
75+
return config === packageName;
76+
}
77+
return config === '*';
78+
})
79+
.forEach((prop: string): void => {
80+
const configs = Object.keys(configModule[prop].configuration);
81+
configs.forEach((conf: string): void => {
82+
tmpConfig[conf] = configModule[prop].configuration[conf];
83+
});
84+
});
85+
configModule = tmpConfig;
86+
} catch (err) {
87+
console.error(chalk.red('\nCould not find a yeoman configuration file.\n'));
88+
console.error(chalk.red("\nPlease make sure to use 'this.config.set('configuration', this.configuration);' at the end of the generator.\n"));
89+
Error.stackTraceLimit = 0;
90+
process.exitCode = -1;
91+
}
92+
const transformConfig: TransformConfig = Object.assign(
93+
{
94+
configFile: !configPath ? null : fs.readFileSync(configPath, 'utf8'),
95+
configPath,
96+
},
97+
configModule,
98+
);
99+
return runTransform(transformConfig, 'init');
100+
})
101+
.catch((err): void => {
102+
console.error(
103+
chalk.red(
104+
`
106105
Unexpected Error
107106
please file an issue here https://github.com/webpack/webpack-cli/issues/new?template=Bug_report.md
108-
`
109-
)
110-
);
111-
console.error(err);
112-
});
107+
`,
108+
),
109+
);
110+
console.error(err);
111+
});
113112
}

0 commit comments

Comments
 (0)