Skip to content

Commit

Permalink
Refactor lib/creator and integrate validation in migrate (#99)
Browse files Browse the repository at this point in the history
* 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
evenstensberg authored Apr 7, 2017
1 parent 6e93af5 commit 66280b9
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 80 deletions.
77 changes: 43 additions & 34 deletions lib/creator/index.js
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
};
12 changes: 12 additions & 0 deletions lib/creator/index.test.js
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');
});

});
37 changes: 0 additions & 37 deletions lib/creator/init-transform.js

This file was deleted.

25 changes: 25 additions & 0 deletions lib/creator/transformations/index.js
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.
Original file line number Diff line number Diff line change
@@ -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', () => {

Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions lib/initialize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const npmPackagesExists = require('./utils/npm-packages-exists');
const creator = require('./creator/index');
const creator = require('./creator/index').creator;

/*
* @function initializeInquirer
Expand All @@ -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 { <Function> } prompt|npmPackagesExists - returns either an inquirer prompt with
* the initial questions we provide, or validates the packages given
* @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 initializeInquirer(pkg) {
Expand Down
17 changes: 14 additions & 3 deletions lib/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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'));
}
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/utils/resolve-packages.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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');
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 66280b9

Please sign in to comment.