-
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b02070d
commit b2c2bbd
Showing
19 changed files
with
9,167 additions
and
25,314 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
); | ||
} | ||
}; |
Oops, something went wrong.