Created
January 13, 2016 10:09
-
-
Save idan/7a9448d8fee22189b808 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict' | |
module.exports = function (plop) { | |
plop.setGenerator('component', { | |
description: 'Generate a new component', | |
prompts: [ | |
{ | |
type: 'input', | |
name: 'componentName', | |
message: 'What is the name of the new component?', | |
validate: (value) => { | |
if (/.+/.test(value.trim())) { return true } | |
return 'Component name is required.' | |
} | |
} | |
], | |
actions: [ | |
{ | |
type: 'add', | |
path: 'src/client/components/{{componentName}}/index.jsx', | |
templateFile: 'plopTemplates/component.jsx.hbs', | |
abortOnFail: true, | |
}, | |
{ | |
type: 'add', | |
path: 'src/client/components/{{componentName}}/style.scss', | |
templateFile: 'plopTemplates/component.scss.hbs', | |
abortOnFail: true, | |
}, | |
function customAction (answers) { | |
// inserts a line into index.js while maintaining alphabetical sort. | |
const fs = require('fs') | |
const toAdd = plop.renderString("export { default as {{componentName}} } from './{{componentName}}'", answers) | |
let imports = fs.readFileSync('src/client/components/index.js', 'utf8') | |
.split('\n').slice(0, -1) // chop off the trailing blank line | |
imports.push(toAdd) | |
imports = imports.sort() | |
imports.push('') // add a trailing blank line | |
fs.writeFileSync('src/client/components/index.js', imports.join('\n'), 'utf8') | |
return 'add component to components/index.js' | |
} | |
], | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment