diff --git a/lib/creator/index.js b/lib/creator/index.js index 7f8daf8cb71..9778b08fbe5 100644 --- a/lib/creator/index.js +++ b/lib/creator/index.js @@ -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 { } opts - An object containing webpackOptions or nothing -* @returns { } 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 }; diff --git a/lib/creator/index.test.js b/lib/creator/index.test.js new file mode 100644 index 00000000000..cbe6ad75cf4 --- /dev/null +++ b/lib/creator/index.test.js @@ -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'); + }); + +}); diff --git a/lib/creator/init-transform.js b/lib/creator/init-transform.js deleted file mode 100644 index d557cc2997e..00000000000 --- a/lib/creator/init-transform.js +++ /dev/null @@ -1,37 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const yeoman = require('yeoman-environment'); -const Generator = require('yeoman-generator'); -const initGenerator = require('./generators/index'); -const WebpackAdapter = require('./generators/adapter'); - -/* -* @function initTransform -* -* Runs yeoman and in the future lets us grab the answers from the generators -* -* @param { Array } options - An Array of paths to match generators for -* @returns { } -*/ - -module.exports = function initTransform(options) { - const creator = require('./index'); - const env = yeoman.createEnv(null, null, new WebpackAdapter()); - - if(options) { - env.register(require.resolve(options), 'npm:app'); - - env.run('npm:app') - .on('end', () => { - return creator(null, env.getArgument('configuration')); - }); - - } else { - env.registerStub(initGenerator, 'npm:app'); - - env.run('npm:app') - .on('end', () => { - return creator(null, env.getArgument('configuration')); - }); - } -}; diff --git a/lib/creator/transformations/index.js b/lib/creator/transformations/index.js new file mode 100644 index 00000000000..308d2644156 --- /dev/null +++ b/lib/creator/transformations/index.js @@ -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 { } 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'); + } + */ +}; diff --git a/lib/creator/validate-options.js b/lib/creator/utils/validate-options.js similarity index 100% rename from lib/creator/validate-options.js rename to lib/creator/utils/validate-options.js diff --git a/lib/creator/validate-options.spec.js b/lib/creator/utils/validate-options.spec.js similarity index 79% rename from lib/creator/validate-options.spec.js rename to lib/creator/utils/validate-options.spec.js index 5ffa642da2a..fc2c0ef0f4f 100644 --- a/lib/creator/validate-options.spec.js +++ b/lib/creator/utils/validate-options.spec.js @@ -1,6 +1,6 @@ 'use strict'; -const validateOptions = require('../../__mocks__/creator/validate-options.mock').validateOptions; +const validateOptions = require('../../../__mocks__/creator/validate-options.mock').validateOptions; describe('validate-options', () => { diff --git a/lib/creator/generators/adapter.js b/lib/creator/yeoman/webpack-adapter.js similarity index 100% rename from lib/creator/generators/adapter.js rename to lib/creator/yeoman/webpack-adapter.js diff --git a/lib/creator/generators/index.js b/lib/creator/yeoman/webpack-generator.js similarity index 100% rename from lib/creator/generators/index.js rename to lib/creator/yeoman/webpack-generator.js diff --git a/lib/initialize.js b/lib/initialize.js index 81a107ff32b..0988943e89d 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -1,5 +1,5 @@ const npmPackagesExists = require('./utils/npm-packages-exists'); -const creator = require('./creator/index'); +const creator = require('./creator/index').creator; /* * @function initializeInquirer @@ -8,8 +8,8 @@ const creator = require('./creator/index'); * if we are running the init command with no arguments or if we got dependencies * * @param { Object } pkg - packages included when running the init command -* @returns { } prompt|npmPackagesExists - returns either an inquirer prompt with -* the initial questions we provide, or validates the packages given +* @returns { } 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 initializeInquirer(pkg) { diff --git a/lib/migrate.js b/lib/migrate.js index 797afe1258c..6a3db45a3cb 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -4,6 +4,9 @@ const diff = require('diff'); const inquirer = require('inquirer'); const PLazy = require('p-lazy'); const Listr = require('listr'); +const validateSchema = require('./utils/validateSchema.js'); +const webpackOptionsSchema = require('./utils/webpackOptionsSchema.json'); +const WebpackOptionsValidationError = require('./utils/WebpackOptionsValidationError'); module.exports = function transformFile(currentConfigPath, outputConfigPath, options) { const recastOptions = Object.assign({ @@ -65,9 +68,17 @@ module.exports = function transformFile(currentConfigPath, outputConfigPath, opt ]) .then(answers => { if (answers['confirmMigration']) { - // TODO validate the config - fs.writeFileSync(outputConfigPath, result, 'utf8'); - console.log(chalk.green(`✔︎ New webpack v2 config file is at ${outputConfigPath}`)); + fs.writeFile(outputConfigPath, result, 'utf8', (err) => { + const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, require(outputConfigPath)); + if (err) { + throw err; + } + else if (webpackOptionsValidationErrors.length) { + throw new WebpackOptionsValidationError(webpackOptionsValidationErrors); + } else { + console.log(chalk.green(`\n ✔︎ New webpack v2 config file is at ${outputConfigPath}`)); + } + }); } else { console.log(chalk.red('✖ Migration aborted')); } diff --git a/lib/creator/utils/WebpackOptionsValidationError.js b/lib/utils/WebpackOptionsValidationError.js similarity index 100% rename from lib/creator/utils/WebpackOptionsValidationError.js rename to lib/utils/WebpackOptionsValidationError.js diff --git a/lib/utils/resolve-packages.js b/lib/utils/resolve-packages.js index 8f8f5587272..a892cc494d0 100644 --- a/lib/utils/resolve-packages.js +++ b/lib/utils/resolve-packages.js @@ -1,5 +1,5 @@ const spawn = require('cross-spawn'); -const creator = require('../creator/index'); +const creator = require('../creator/index').creator; const path = require('path'); const chalk = require('chalk'); @@ -49,7 +49,7 @@ module.exports = function resolvePackages(pkg) { return processPromise(spawnChild(pkg)).then( () => { try { let loc = path.join('..', '..', 'node_modules', pkg); - creator(loc, null); + return creator(loc); } catch(err) { console.log('Package wasn\'t validated correctly..'); console.log('Submit an issue for', pkg, 'if this persists'); diff --git a/lib/creator/utils/validateSchema.js b/lib/utils/validateSchema.js similarity index 100% rename from lib/creator/utils/validateSchema.js rename to lib/utils/validateSchema.js diff --git a/lib/creator/utils/webpackOptionsSchema.json b/lib/utils/webpackOptionsSchema.json similarity index 100% rename from lib/creator/utils/webpackOptionsSchema.json rename to lib/utils/webpackOptionsSchema.json