Skip to content

Commit d36cd4f

Browse files
committed
ast(init): fix init command
1 parent 7ab69a3 commit d36cd4f

3 files changed

Lines changed: 15 additions & 13 deletions

File tree

lib/commands/init.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

33
const npmPackagesExists = require("../utils/npm-packages-exists");
4-
const creator = require("../init/index").creator;
4+
const defaultGenerator = require("../generators/init-generator");
5+
const modifyHelper = require("../utils/modify-config-helper");
56

67
/**
78
*
@@ -15,7 +16,7 @@ const creator = require("../init/index").creator;
1516

1617
module.exports = function initializeInquirer(pkg) {
1718
if (pkg.length === 0) {
18-
return creator();
19+
return modifyHelper("init", defaultGenerator);
1920
}
2021
return npmPackagesExists(pkg);
2122
};

lib/init/transformations/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ module.exports = function runTransform(webpackProperties, action) {
9898
.filter(e => e[1]);
9999

100100
const ast = j(
101-
action ? webpackProperties.configFile : "module.exports = {}"
101+
action && action !== "init"
102+
? webpackProperties.configFile
103+
: "module.exports = {}"
102104
);
103105
const transformAction = action || null;
104106

@@ -111,15 +113,16 @@ module.exports = function runTransform(webpackProperties, action) {
111113
})
112114
.then(_ => {
113115
let configurationName;
114-
if (!config.configName) {
116+
if (!config.configName && action !== "init") {
115117
configurationName = "webpack.config.js";
116118
} else {
117119
configurationName = "webpack." + config.configName + ".js";
118120
}
119121

120-
const outputPath = action
121-
? webpackProperties.configPath
122-
: path.join(process.cwd(), configurationName);
122+
const outputPath =
123+
action && action !== "init"
124+
? webpackProperties.configPath
125+
: path.join(process.cwd(), configurationName);
123126
const source = ast.toSource({
124127
quote: "single"
125128
});
@@ -130,7 +133,7 @@ module.exports = function runTransform(webpackProperties, action) {
130133
console.error(err.message ? err.message : err);
131134
});
132135
});
133-
if (action) {
136+
if (action && webpackProperties.config.item) {
134137
process.stdout.write(
135138
"\n" +
136139
chalk.green(

lib/utils/modify-config-helper.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ const runTransform = require("../init/transformations/index");
1616
*/
1717

1818
module.exports = function modifyHelperUtil(action, generator) {
19-
const configPath = path.resolve(process.cwd(), "webpack.config.js");
19+
let configPath = path.resolve(process.cwd(), "webpack.config.js");
2020
const webpackConfigExists = fs.existsSync(configPath);
2121
if (!webpackConfigExists) {
22-
throw new Error(
23-
"Couldn't find a webpack configuration in your project path"
24-
);
22+
configPath = null;
2523
}
2624
const env = yeoman.createEnv("webpack", null);
2725
const generatorName = `webpack-${action}-generator`;
@@ -30,7 +28,7 @@ module.exports = function modifyHelperUtil(action, generator) {
3028
env.run(generatorName).on("end", () => {
3129
const config = Object.assign(
3230
{
33-
configFile: fs.readFileSync(configPath, "utf8"),
31+
configFile: !configPath ? null : fs.readFileSync(configPath, "utf8"),
3432
configPath: configPath
3533
},
3634
env.getArgument("configuration")

0 commit comments

Comments
 (0)