Skip to content

Commit

Permalink
ast(init): fix init command
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed Feb 23, 2018
1 parent 7ab69a3 commit d36cd4f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

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

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

module.exports = function initializeInquirer(pkg) {
if (pkg.length === 0) {
return creator();
return modifyHelper("init", defaultGenerator);
}
return npmPackagesExists(pkg);
};
15 changes: 9 additions & 6 deletions lib/init/transformations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ module.exports = function runTransform(webpackProperties, action) {
.filter(e => e[1]);

const ast = j(
action ? webpackProperties.configFile : "module.exports = {}"
action && action !== "init"
? webpackProperties.configFile
: "module.exports = {}"
);
const transformAction = action || null;

Expand All @@ -111,15 +113,16 @@ module.exports = function runTransform(webpackProperties, action) {
})
.then(_ => {
let configurationName;
if (!config.configName) {
if (!config.configName && action !== "init") {
configurationName = "webpack.config.js";
} else {
configurationName = "webpack." + config.configName + ".js";
}

const outputPath = action
? webpackProperties.configPath
: path.join(process.cwd(), configurationName);
const outputPath =
action && action !== "init"
? webpackProperties.configPath
: path.join(process.cwd(), configurationName);
const source = ast.toSource({
quote: "single"
});
Expand All @@ -130,7 +133,7 @@ module.exports = function runTransform(webpackProperties, action) {
console.error(err.message ? err.message : err);
});
});
if (action) {
if (action && webpackProperties.config.item) {
process.stdout.write(
"\n" +
chalk.green(
Expand Down
8 changes: 3 additions & 5 deletions lib/utils/modify-config-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ const runTransform = require("../init/transformations/index");
*/

module.exports = function modifyHelperUtil(action, generator) {
const configPath = path.resolve(process.cwd(), "webpack.config.js");
let configPath = path.resolve(process.cwd(), "webpack.config.js");
const webpackConfigExists = fs.existsSync(configPath);
if (!webpackConfigExists) {
throw new Error(
"Couldn't find a webpack configuration in your project path"
);
configPath = null;
}
const env = yeoman.createEnv("webpack", null);
const generatorName = `webpack-${action}-generator`;
Expand All @@ -30,7 +28,7 @@ module.exports = function modifyHelperUtil(action, generator) {
env.run(generatorName).on("end", () => {
const config = Object.assign(
{
configFile: fs.readFileSync(configPath, "utf8"),
configFile: !configPath ? null : fs.readFileSync(configPath, "utf8"),
configPath: configPath
},
env.getArgument("configuration")
Expand Down

0 comments on commit d36cd4f

Please sign in to comment.