Skip to content

Commit

Permalink
Merge branch 'fix-init'
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed Feb 24, 2018
2 parents a165269 + 4dff074 commit d62faea
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 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
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
"travis:integration": "yarn prepare && nyc jest && yarn reportCoverage",
"travis:lint": "yarn lint && yarn bundlesize",
"bundlesize": "bundlesize",
"commitmsg": "commitlint -e $GIT_PARAMS",
"changelog": "conventional-changelog --config ./build/changelog-generator/index.js --infile CHANGELOG.md --same-file"
},
"lint-staged": {
"{lib,bin}/**/!(__testfixtures__)/**.js": [
"eslint --fix",
"npm run commitmsg",
"git add"
]
},
Expand Down

0 comments on commit d62faea

Please sign in to comment.