Skip to content

Commit

Permalink
Merge 3020af9 into e7dd288
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyparis committed Dec 26, 2019
2 parents e7dd288 + 3020af9 commit 0b20eec
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 100 deletions.
20 changes: 15 additions & 5 deletions internals/generators/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Component Generator
*/

const properCase = require('change-case').pascalCase;
const componentExists = require('../utils/componentExists');

module.exports = {
Expand All @@ -13,6 +14,9 @@ module.exports = {
message: 'What should it be called?',
default: 'Button',
validate: value => {
if( (/\/\//).test(value) ) return "A parent directory cannot have two adjacent `/`"
if( (/^\/|.+\/$/).test(value) ) return "A parent directory cannot start or end with a `/`"

if (/.+/.test(value)) {
return componentExists(value)
? 'A component or container with this name already exists'
Expand Down Expand Up @@ -43,16 +47,21 @@ module.exports = {
],
actions: data => {
// Generate index.js and index.test.js
let filePath = data.name.split('/').map(properCase)
const fullPath = filePath.join('/')
const shortName = filePath.pop()
filePath = filePath.join('/')

const actions = [
{
type: 'add',
path: '../../app/components/{{properCase name}}/index.js',
path: `../../app/components/${fullPath}/index.js`,
templateFile: './component/index.js.hbs',
abortOnFail: true,
},
{
type: 'add',
path: '../../app/components/{{properCase name}}/tests/index.test.js',
path: `../../app/components/${fullPath}/tests/index.test.js`,
templateFile: './component/test.js.hbs',
abortOnFail: true,
},
Expand All @@ -62,7 +71,7 @@ module.exports = {
if (data.wantMessages) {
actions.push({
type: 'add',
path: '../../app/components/{{properCase name}}/messages.js',
path: `../../app/components/${fullPath}/messages.js`,
templateFile: './component/messages.js.hbs',
abortOnFail: true,
});
Expand All @@ -72,15 +81,16 @@ module.exports = {
if (data.wantLoadable) {
actions.push({
type: 'add',
path: '../../app/components/{{properCase name}}/Loadable.js',
path: `../../app/components/${fullPath}/Loadable.js`,
templateFile: './component/loadable.js.hbs',
abortOnFail: true,
});
}

actions.push({
type: 'prettify',
path: '/components/',
path: `/components${filePath ? `/${filePath}/` : '/'}`,
name: shortName
});

return actions;
Expand Down
41 changes: 26 additions & 15 deletions internals/generators/container/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Container Generator
*/


const properCase = require('change-case').pascalCase;
const componentExists = require('../utils/componentExists');

module.exports = {
Expand All @@ -13,6 +14,9 @@ module.exports = {
message: 'What should it be called?',
default: 'Form',
validate: value => {
if( (/\/\//).test(value) ) return "A parent directory cannot have two adjacent `/`"
if( (/^\/|.+\/$/).test(value) ) return "A parent directory cannot start or end with a `/`"

if (/.+/.test(value)) {
return componentExists(value)
? 'A component or container with this name already exists'
Expand Down Expand Up @@ -62,16 +66,21 @@ module.exports = {
],
actions: data => {
// Generate index.js and index.test.js
let filePath = data.name.split('/').map(properCase)
const fullPath = filePath.join('/')
const shortName = filePath.pop()
filePath = filePath.join('/')

const actions = [
{
type: 'add',
path: '../../app/containers/{{properCase name}}/index.js',
path: `../../app/containers/${fullPath}/index.js`,
templateFile: './container/index.js.hbs',
abortOnFail: true,
},
{
type: 'add',
path: '../../app/containers/{{properCase name}}/tests/index.test.js',
path: `../../app/containers/${fullPath}/tests/index.test.js`,
templateFile: './container/test.js.hbs',
abortOnFail: true,
},
Expand All @@ -81,7 +90,7 @@ module.exports = {
if (data.wantMessages) {
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/messages.js',
path: `../../app/containers/${fullPath}/messages.js`,
templateFile: './container/messages.js.hbs',
abortOnFail: true,
});
Expand All @@ -93,50 +102,50 @@ module.exports = {
// Actions
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/actions.js',
path: `../../app/containers/${fullPath}/actions.js`,
templateFile: './container/actions.js.hbs',
abortOnFail: true,
});
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/tests/actions.test.js',
path: `../../app/containers/${fullPath}/tests/actions.test.js`,
templateFile: './container/actions.test.js.hbs',
abortOnFail: true,
});

// Constants
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/constants.js',
path: `../../app/containers/${fullPath}/constants.js`,
templateFile: './container/constants.js.hbs',
abortOnFail: true,
});

// Selectors
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/selectors.js',
path: `../../app/containers/${fullPath}/selectors.js`,
templateFile: './container/selectors.js.hbs',
abortOnFail: true,
});
actions.push({
type: 'add',
path:
'../../app/containers/{{properCase name}}/tests/selectors.test.js',
`../../app/containers/${fullPath}/tests/selectors.test.js`,
templateFile: './container/selectors.test.js.hbs',
abortOnFail: true,
});

// Reducer
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/reducer.js',
path: `../../app/containers/${fullPath}/reducer.js`,
templateFile: './container/reducer.js.hbs',
abortOnFail: true,
});
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/tests/reducer.test.js',
path: `../../app/containers/${fullPath}/tests/reducer.test.js`,
templateFile: './container/reducer.test.js.hbs',
abortOnFail: true,
});
Expand All @@ -146,13 +155,13 @@ module.exports = {
if (data.wantSaga) {
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/saga.js',
path: `../../app/containers/${fullPath}/saga.js`,
templateFile: './container/saga.js.hbs',
abortOnFail: true,
});
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/tests/saga.test.js',
path: `../../app/containers/${fullPath}/tests/saga.test.js`,
templateFile: './container/saga.test.js.hbs',
abortOnFail: true,
});
Expand All @@ -161,17 +170,19 @@ module.exports = {
if (data.wantLoadable) {
actions.push({
type: 'add',
path: '../../app/containers/{{properCase name}}/Loadable.js',
path: `../../app/containers/${fullPath}/Loadable.js`,
templateFile: './component/loadable.js.hbs',
abortOnFail: true,
});
}

actions.push({
type: 'prettify',
path: '/containers/',
path: `/containers${filePath ? `/${filePath}/` : '/'}`,
name: shortName
});


return actions;
},
};
2 changes: 1 addition & 1 deletion internals/generators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = plop => {
__dirname,
'/../../app/',
config.path,
plop.getHelper('properCase')(answers.name),
plop.getHelper('properCase')(config.name || answers.name),
'**',
'**.js',
)}`;
Expand Down
23 changes: 14 additions & 9 deletions internals/generators/utils/componentExists.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
* Check whether the given component exist in either the components or containers directory
*/

const fs = require('fs');
const path = require('path');
const pageComponents = fs.readdirSync(
path.join(__dirname, '../../../app/components'),
);
const pageContainers = fs.readdirSync(
path.join(__dirname, '../../../app/containers'),
);
const components = pageComponents.concat(pageContainers);

const walkNestedComponents = require('./walkNestedComponents')

const pageComponents = walkNestedComponents(path.join(__dirname,'../../../app/components'))
const pageContainers = walkNestedComponents(path.join(__dirname,'../../../app/containers'))

const components = pageComponents.concat(pageContainers)

function componentExists(comp) {
return components.indexOf(comp) >= 0;
const containerPath = path.join(__dirname, `../../../app/containers/${comp}`)
const componentPath = path.join(__dirname, `../../../app/components/${comp}`)

return (
components.indexOf(containerPath) >= 0 ||
components.indexOf(componentPath) >= 0
);
}

module.exports = componentExists;
31 changes: 31 additions & 0 deletions internals/generators/utils/walkNestedComponents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* walkNestedComponents
*
* List all components listed within a directory
*/

const fs = require('fs');

function walkNestedComponents(dir) {
let results = [];
const list = fs.readdirSync(dir);
list.forEach((file) => {
// Only include the directory as a component if `index.js` is defined
if(file === 'index.js')
{
results.push(dir)
}
else
{
const filePath = `${dir}/${file}`;
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walkNestedComponents(filePath));
}
}
});
return results;
}

module.exports = walkNestedComponents
Loading

0 comments on commit 0b20eec

Please sign in to comment.