-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor lib/creator and integrate validation in migrate (#99)
* feat: refactor Lays the groundwork for a first release. Integrated Validation to migrate, refactored creator for the init feature to not be circular and made a cleaner structure to the project inside lib/creator. * feat: Use custom generatorNames * feat: Simply logic for yeoman Also commented out unused modules for now. * fix: add tests * fix: use real implementation for test
- Loading branch information
1 parent
6e93af5
commit 66280b9
Showing
14 changed files
with
100 additions
and
80 deletions.
There are no files selected for viewing
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,42 +1,51 @@ | ||
const validateSchema = require('./utils/validateSchema.js'); | ||
const webpackOptionsSchema = require('./utils/webpackOptionsSchema.json'); | ||
const WebpackOptionsValidationError = require('./utils/WebpackOptionsValidationError'); | ||
const initTransform = require('./init-transform'); | ||
const chalk = require('chalk'); | ||
const yeoman = require('yeoman-environment'); | ||
const path = require('path'); | ||
const defaultGenerator = require('./yeoman/webpack-generator'); | ||
const WebpackAdapter = require('./yeoman/webpack-adapter'); | ||
const runTransform = require('./transformations/index'); | ||
|
||
/* | ||
* @function creator | ||
* | ||
* Main function to build up a webpack configuration. | ||
* Either throws an error if it doesn't match the webpack schema, | ||
* or validates the filepaths of the options given. | ||
* If a package is supplied, it finds the path of the package and runs inquirer | ||
* Runs yeoman and runs the transformations based on the object | ||
* built up from an author/user | ||
* | ||
* @param { Array } pkgPaths - An Array of packages to run | ||
* @param { <object|null> } opts - An object containing webpackOptions or nothing | ||
* @returns { <Function|Error> } initTransform - Initializes the scaffold in yeoman | ||
* @param { String } options - An path to the given generator | ||
* @returns { Function } runTransform - Run transformations based on yeoman prompt | ||
*/ | ||
|
||
module.exports = function creator(pkgPaths, opts) { | ||
// null, config -> without package | ||
// addon, null -> with package | ||
// we're dealing with init, we need to change this later, as it may have been emptied by yeoman | ||
if(!pkgPaths && !opts) { | ||
initTransform(); | ||
} | ||
else if(pkgPaths) { | ||
// this example app actually needs a refactor in order for it to work | ||
initTransform(pkgPaths); | ||
} | ||
else if(!pkgPaths && opts) { | ||
console.log(opts); | ||
// scaffold is done | ||
/* | ||
const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, initialWebpackConfig); | ||
if (webpackOptionsValidationErrors.length) { | ||
throw new WebpackOptionsValidationError(webpackOptionsValidationErrors); | ||
} else { | ||
process.stdout.write('\n' + chalk.green('Congratulations! Your new webpack config file is created!') + '\n'); | ||
} | ||
*/ | ||
function creator(options) { | ||
const env = yeoman.createEnv(null, null, new WebpackAdapter()); | ||
const generatorName = options ? replaceGeneratorName(path.basename(options)) : 'webpack-default-generator'; | ||
if(options) { | ||
const generatorName = replaceGeneratorName(path.basename(options)); | ||
env.register(require.resolve(options), generatorName); | ||
} else { | ||
env.registerStub(defaultGenerator, 'webpack-default-generator'); | ||
} | ||
|
||
env.run(generatorName) | ||
.on('end', () => { | ||
return runTransform(env.getArgument('configuration')); | ||
}); | ||
} | ||
|
||
/* | ||
* @function replaceGeneratorName | ||
* | ||
* Replaces the webpack-addons pattern with the end of the addons name merged | ||
* with 'generator' | ||
* | ||
* @param { String } name - name of the generator | ||
* @returns { String } name - replaced pattern of the name | ||
*/ | ||
|
||
function replaceGeneratorName(name) { | ||
return name.replace( | ||
/(webpack-addons)?([^:]+)(:.*)?/g, 'generator$2'); | ||
} | ||
|
||
module.exports = { | ||
creator, | ||
replaceGeneratorName | ||
}; |
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,12 @@ | ||
'use strict'; | ||
|
||
const replaceGeneratorName = require('./index').replaceGeneratorName; | ||
|
||
describe('replaceGeneratorName', () => { | ||
|
||
it('should replace a pattern of an addon', () => { | ||
const generatorName = replaceGeneratorName('webpack-addons-thefox'); | ||
expect(generatorName).toEqual('generator-thefox'); | ||
}); | ||
|
||
}); |
This file was deleted.
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//const chalk = require('chalk'); | ||
//const validateSchema = require('../../utils/validateSchema.js'); | ||
//const webpackOptionsSchema = require('../../utils/webpackOptionsSchema.json'); | ||
//const WebpackOptionsValidationError = require('../../utils/WebpackOptionsValidationError'); | ||
/* | ||
* @function runTransform | ||
* | ||
* Runs the transformations from an object we get from yeoman | ||
* | ||
* @param { Object } transformObject - Options to transform | ||
* @returns { <Void> } TODO | ||
*/ | ||
|
||
module.exports = function runTransform(transformObject) { | ||
|
||
// scaffold is done | ||
/* | ||
const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, initialWebpackConfig); | ||
if (webpackOptionsValidationErrors.length) { | ||
throw new WebpackOptionsValidationError(webpackOptionsValidationErrors); | ||
} else { | ||
process.stdout.write('\n' + chalk.green('Congratulations! Your new webpack config file is created!') + '\n'); | ||
} | ||
*/ | ||
}; |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
lib/creator/validate-options.spec.js → lib/creator/utils/validate-options.spec.js
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
File renamed without changes.
File renamed without changes.
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
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.