Skip to content

Commit

Permalink
cli(pkgs): re-add entries
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed May 17, 2018
1 parent b02070d commit b2c2bbd
Show file tree
Hide file tree
Showing 19 changed files with 9,167 additions and 25,314 deletions.
7,770 changes: 3,885 additions & 3,885 deletions package-lock.json

Large diffs are not rendered by default.

17,459 changes: 0 additions & 17,459 deletions packages/add/package-lock.json

This file was deleted.

632 changes: 319 additions & 313 deletions packages/generate-loader/package-lock.json

Large diffs are not rendered by default.

632 changes: 319 additions & 313 deletions packages/generate-plugin/package-lock.json

Large diffs are not rendered by default.

2,636 changes: 1,312 additions & 1,324 deletions packages/generators/package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions packages/info/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 14 additions & 83 deletions packages/init/index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,25 @@
"use strict";

const path = require("path");
const j = require("jscodeshift");
const chalk = require("chalk");
const pEachSeries = require("p-each-series");

const runPrettier = require("@webpack-cli/utils/run-prettier");
const astTransform = require("@webpack-cli/utils/recursive-parser");
const propTypes = require("@webpack-cli/utils/prop-types");

/**
*
* Maps back transforms that needs to be run using the configuration
* provided.
*
* @param {Object} transformObject - An Object with all transformations
* @param {Object} config - Configuration to transform
* @returns {Object} - An Object with the transformations to be run
*/

function mapOptionsToTransform(config) {
return Object.keys(config.webpackOptions).filter(k => propTypes.has(k));
}
const npmPackagesExists = require("@webpack-cli/utils/npm-packages-exists");
const defaultGenerator = require("@webpack-cli/utils/generators/init-generator");
const modifyHelper = require("@webpack-cli/utils/modify-config-helper");

/**
*
* Runs the transformations from an object we get from yeoman
* First function to be called after running the init flag. This is a check,
* if we are running the init command with no arguments or if we got dependencies
*
* @param {Object} webpackProperties - Configuration to transform
* @param {String} action - Action to be done on the given ast
* @returns {Promise} - A promise that writes each transform, runs prettier
* and writes the file
* @param {Array} args - array of arguments such as
* packages included when running the init command
* @returns {Function} creator/npmPackagesExists - returns an installation of the package,
* followed up with a yeoman instance of that if there's packages. If not, it creates a defaultGenerator
*/

module.exports = function runTransform(webpackProperties, action) {
// webpackOptions.name sent to nameTransform if match
const webpackConfig = Object.keys(webpackProperties).filter(p => {
return p !== "configFile" && p !== "configPath";
});
const initActionNotDefined = action && action !== "init" ? true : false;

webpackConfig.forEach(scaffoldPiece => {
const config = webpackProperties[scaffoldPiece];
const transformations = mapOptionsToTransform(config);
const ast = j(
initActionNotDefined
? webpackProperties.configFile
: "module.exports = {}"
);
const transformAction = action || null;

return pEachSeries(transformations, f => {
return astTransform(j, ast, config.webpackOptions[f], transformAction, f);
})
.then(_ => {
let configurationName;
if (!config.configName) {
configurationName = "webpack.config.js";
} else {
configurationName = "webpack." + config.configName + ".js";
}

const outputPath = initActionNotDefined
? webpackProperties.configPath
: path.join(process.cwd(), configurationName);
const source = ast.toSource({
quote: "single"
});
module.exports = function initializeInquirer(...args) {
const packages = args.slice(3);

runPrettier(outputPath, source);
})
.catch(err => {
console.error(err.message ? err.message : err);
});
});
if (initActionNotDefined && webpackProperties.config.item) {
process.stdout.write(
"\n" +
chalk.green(
`Congratulations! ${
webpackProperties.config.item
} has been ${action}ed!\n`
)
);
} else {
process.stdout.write(
"\n" +
chalk.green(
"Congratulations! Your new webpack configuration file has been created!\n"
)
);
if (packages.length === 0) {
return modifyHelper("init", defaultGenerator);
}
return npmPackagesExists(packages);
};
94 changes: 94 additions & 0 deletions packages/init/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"use strict";

const path = require("path");
const j = require("jscodeshift");
const chalk = require("chalk");
const pEachSeries = require("p-each-series");

const runPrettier = require("@webpack-cli/utils/run-prettier");
const astTransform = require("@webpack-cli/utils/recursive-parser");
const propTypes = require("@webpack-cli/utils/prop-types");

/**
*
* Maps back transforms that needs to be run using the configuration
* provided.
*
* @param {Object} transformObject - An Object with all transformations
* @param {Object} config - Configuration to transform
* @returns {Object} - An Object with the transformations to be run
*/

function mapOptionsToTransform(config) {
return Object.keys(config.webpackOptions).filter(k => propTypes.has(k));
}

/**
*
* Runs the transformations from an object we get from yeoman
*
* @param {Object} webpackProperties - Configuration to transform
* @param {String} action - Action to be done on the given ast
* @returns {Promise} - A promise that writes each transform, runs prettier
* and writes the file
*/

module.exports = function runTransform(webpackProperties, action) {
// webpackOptions.name sent to nameTransform if match
const webpackConfig = Object.keys(webpackProperties).filter(p => {
return p !== "configFile" && p !== "configPath";
});
const initActionNotDefined = action && action !== "init" ? true : false;

webpackConfig.forEach(scaffoldPiece => {
const config = webpackProperties[scaffoldPiece];
const transformations = mapOptionsToTransform(config);
const ast = j(
initActionNotDefined
? webpackProperties.configFile
: "module.exports = {}"
);
const transformAction = action || null;

return pEachSeries(transformations, f => {
return astTransform(j, ast, config.webpackOptions[f], transformAction, f);
})
.then(_ => {
let configurationName;
if (!config.configName) {
configurationName = "webpack.config.js";
} else {
configurationName = "webpack." + config.configName + ".js";
}

const outputPath = initActionNotDefined
? webpackProperties.configPath
: path.join(process.cwd(), configurationName);
const source = ast.toSource({
quote: "single"
});

runPrettier(outputPath, source);
})
.catch(err => {
console.error(err.message ? err.message : err);
});
});
if (initActionNotDefined && webpackProperties.config.item) {
process.stdout.write(
"\n" +
chalk.green(
`Congratulations! ${
webpackProperties.config.item
} has been ${action}ed!\n`
)
);
} else {
process.stdout.write(
"\n" +
chalk.green(
"Congratulations! Your new webpack configuration file has been created!\n"
)
);
}
};
Loading

0 comments on commit b2c2bbd

Please sign in to comment.