Skip to content

Commit

Permalink
style: Fixed all styles accoring to the webpack's style
Browse files Browse the repository at this point in the history
  • Loading branch information
okonet committed Apr 6, 2017
1 parent 3bfaa86 commit 82d7586
Show file tree
Hide file tree
Showing 24 changed files with 272 additions and 253 deletions.
4 changes: 2 additions & 2 deletions __mocks__/creator/validate-options.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ function getPath(part) {
}

function validateOptions(opts) {
return Object.keys(opts).forEach( (location) => {
return Object.keys(opts).forEach((location) => {
let part = getPath(opts[location]);
try {
fs.readFileSync(part);
} catch (err) {
} catch(err) {
throw new Error('Did not find the file');
}
});
Expand Down
11 changes: 6 additions & 5 deletions __mocks__/inquirer/resolve.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
const path = require('path');

function mockPromise(value) {
return (value || {}).then ? value : {
then: function (callback) {
return(value || {}).then ? value : {
then: function(callback) {
return mockPromise(callback(value));
}
};
}

function spawnChild(pkg) {
return pkg;
}

function getLoc(option) {
let packageModule = [];
option.filter( (pkg) => {
mockPromise(spawnChild(pkg)).then( () => {
option.filter((pkg) => {
mockPromise(spawnChild(pkg)).then(() => {
try {
let loc = path.join('..', '..', 'node_modules', pkg);
packageModule.push(loc);
} catch(err) {
throw new Error('Package wasn\'t validated correctly..' +
'Submit an issue for', pkg, 'if this persists');
'Submit an issue for', pkg, 'if this persists');
}
});
return packageModule;
Expand Down
5 changes: 2 additions & 3 deletions lib/creator/generators/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const Generator = require('yeoman-generator');
const Input = require('webpack-addons').Input;


module.exports = class WebpackGenerator extends Generator {
constructor(args, opts) {
super(args, opts);
Expand All @@ -13,8 +12,8 @@ module.exports = class WebpackGenerator extends Generator {
}
Inquirer() {
this.myConfig.inquirer = [
Input('entry','What is the name of the entry point in your application?'),
Input('output','What is the name of the output directory in your application?')
Input('entry', 'What is the name of the entry point in your application?'),
Input('output', 'What is the name of the output directory in your application?')
];
}
Config() {
Expand Down
28 changes: 13 additions & 15 deletions lib/creator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@ const WebpackOptionsValidationError = require('./utils/WebpackOptionsValidationE
const initTransform = require('./init-transform');
const chalk = require('chalk');
/*
* @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
*
* @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
*/
* @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
*
* @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
*/

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) {
} else if(pkgPaths) {
// this example app actually needs a refactor in order for it to work
initTransform(pkgPaths);
}
else if(!pkgPaths && opts) {
} else if(!pkgPaths && opts) {
// scaffold is done
/*
const webpackOptionsValidationErrors = validateSchema(webpackOptionsSchema, initialWebpackConfig);
Expand Down
18 changes: 9 additions & 9 deletions lib/creator/init-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const Generator = require('yeoman-generator');
const initGenerator = require('./generators/index');

/*
* @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 { <Void> }
*/
* @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 { <Void> }
*/

module.exports = function initTransform(options) {
const env = yeoman.createEnv();
Expand All @@ -22,13 +22,13 @@ module.exports = function initTransform(options) {
let name = path.basename(options);
console.log('Done!');
//eslint-disable-next-line
} catch (e) {}
} catch(e) {}
} else {
env.registerStub(initGenerator, 'npm:app');
env.run('npm:app');
try {
console.log('Done!');
// eslint-disable-next-line
} catch (e) {}
} catch(e) {}
}
};
34 changes: 17 additions & 17 deletions lib/creator/validate-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ const fs = require('fs');
const path = require('path');

/*
* @function getPath
*
* Finds the current filepath of a given string
*
* @param { String } part - The name of the file to be checked.
* @returns { String } - returns an string with the filepath
*/
* @function getPath
*
* Finds the current filepath of a given string
*
* @param { String } part - The name of the file to be checked.
* @returns { String } - returns an string with the filepath
*/

function getPath(part) {
return path.join(process.cwd(), part);
}

/*
* @function validateOptions
*
* Validates the options passed from an inquirer instance to make
* sure the path supplied exists
*
* @param { String } part - The name of the file to be checked.
* @returns { <Error|noop> } part - checks if the path exists or throws an error
*/
* @function validateOptions
*
* Validates the options passed from an inquirer instance to make
* sure the path supplied exists
*
* @param { String } part - The name of the file to be checked.
* @returns { <Error|noop> } part - checks if the path exists or throws an error
*/

module.exports = function validateOptions(opts) {
return Object.keys(opts).forEach( (location) => {
return Object.keys(opts).forEach((location) => {
let part = getPath(opts[location]);
try {
fs.readFileSync(part);
} catch (err) {
} catch(err) {
console.error('Found no file at:', part);
process.exit(1);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/creator/validate-options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ describe('validate-options', () => {

it('should throw on fake paths', () => {
expect(() => {
validateOptions({entry: 'noop', output: 'noopsi'});
validateOptions({
entry: 'noop',
output: 'noopsi'
});
}).toThrowError('Did not find the file');
});

it('should find the real files', () => {
expect(() => {
validateOptions({entry: 'package.json'});
validateOptions({
entry: 'package.json'
});
}).not.toThrowError('Did not find the file');
});

Expand Down
3 changes: 1 addition & 2 deletions lib/transformations/bannerPlugin/bannerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function(j, ast) {
const args = path.value.arguments;
// If the first argument is a literal replace it with object notation
// See https://webpack.js.org/guides/migrating/#bannerplugin-breaking-change
if (args && args.length > 1 && args[0].type === j.Literal.name) {
if(args && args.length > 1 && args[0].type === j.Literal.name) {
// and remove the first argument
path.value.arguments = [path.value.arguments[1]];
utils.createOrUpdatePluginByName(j, path.parent, 'webpack.BannerPlugin', {
Expand All @@ -15,5 +15,4 @@ module.exports = function(j, ast) {
}
});


};
14 changes: 8 additions & 6 deletions lib/transformations/defineTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const path = require('path');
* alongside the transform and __tests__ directory.
*/
function runSingleTansform(dirName, transformName, testFilePrefix) {
if (!testFilePrefix) {
if(!testFilePrefix) {
testFilePrefix = transformName;
}

Expand All @@ -38,21 +38,23 @@ function runSingleTansform(dirName, transformName, testFilePrefix) {
// Jest resets the module registry after each test, so we need to always get
// a fresh copy of jscodeshift on every test run.
let jscodeshift = require('jscodeshift/dist/core');
if (module.parser) {
if(module.parser) {
jscodeshift = jscodeshift.withParser(module.parser);
}
const ast = jscodeshift(source);
return transform(jscodeshift, ast, source).toSource({ quote: 'single' });
return transform(jscodeshift, ast, source).toSource({
quote: 'single'
});
}

/**
* Handles some boilerplate around defining a simple jest/Jasmine test for a
* jscodeshift transform.
*/
function defineTest(dirName, transformName, testFilePrefix) {
const testName = testFilePrefix
? `transforms correctly using "${testFilePrefix}" data`
: 'transforms correctly';
const testName = testFilePrefix ?
`transforms correctly using "${testFilePrefix}" data` :
'transforms correctly';
describe(transformName, () => {
it(testName, () => {
const output = runSingleTansform(dirName, transformName, testFilePrefix);
Expand Down
8 changes: 3 additions & 5 deletions lib/transformations/extractTextPlugin/extractTextPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module.exports = function(j, ast) {
// return p;
// } else
const literalArgs = args.filter(p => utils.isType(p, 'Literal'));
if (literalArgs && literalArgs.length > 1) {
if(literalArgs && literalArgs.length > 1) {
const newArgs = j.objectExpression(literalArgs.map((p, index) =>
utils.createProperty(j, index === 0 ? 'fallback': 'use', p.value)
utils.createProperty(j, index === 0 ? 'fallback' : 'use', p.value)
));
p.value.arguments = [newArgs];
}
Expand All @@ -26,7 +26,5 @@ module.exports = function(j, ast) {

return ast.find(j.CallExpression)
.filter(p => findInvocation(j, p, name))
.forEach(changeArguments);
.forEach(changeArguments);
};


2 changes: 1 addition & 1 deletion lib/transformations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function transformSingleAST(ast, source, transformFunction) {
setTimeout(() => {
try {
resolve(transformFunction(jscodeshift, ast, source));
} catch (err) {
} catch(err) {
reject(err);
}
}, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ module.exports = function(j, ast) {
// If there is debug: true, set debug: true in the plugin
// TODO: remove global debug setting
// TODO: I can't figure out how to find the topmost `debug: true`. help!
if (ast.find(j.Identifier, { name: 'debug' }).size()) {
if(ast.find(j.Identifier, {
name: 'debug'
}).size()) {
loaderOptions.debug = true;
}

// If there is UglifyJsPlugin, set minimize: true
if (findPluginsByName(j, ast, ['webpack.optimize.UglifyJsPlugin']).size()) {
if(findPluginsByName(j, ast, ['webpack.optimize.UglifyJsPlugin']).size()) {
loaderOptions.minimize = true;
}

Expand All @@ -23,6 +25,6 @@ module.exports = function(j, ast) {
.filter(path => safeTraverse(path, ['parent', 'value', 'key', 'name']) === 'plugins')
.forEach(path => {
!isEmpty(loaderOptions) &&
createOrUpdatePluginByName(j, path, 'webpack.optimize.LoaderOptionsPlugin', loaderOptions);
createOrUpdatePluginByName(j, path, 'webpack.optimize.LoaderOptionsPlugin', loaderOptions);
});
};
Loading

0 comments on commit 82d7586

Please sign in to comment.