Last active
July 24, 2017 12:26
-
-
Save martinherweg/d832ba4ae912635546039f62644fdcaf to your computer and use it in GitHub Desktop.
Create a Module in the Modules Folder
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
/* | |
|-------------------------------------------------------------------------- | |
| <%= moduleName.replace('-', '_') %> | |
|-------------------------------------------------------------------------- | |
*/ | |
export default function() { | |
console.log('Initialize: <%= moduleName.replace('-', '_') %>'); | |
} |
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
/* | |
|-------------------------------------------------------------------------- | |
| <%= moduleName.replace('-', '_') %> | |
|-------------------------------------------------------------------------- | |
*/ | |
// Modul | |
.m-<%= moduleName.replace('-', '_') %> { | |
} |
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
{# <%= moduleName %> #} | |
{% set defaults = { | |
classname: 'js-<%= moduleName.replace('-', '_') %>' | |
} %} | |
{% set options = options is defined ? defaults|merge(options) : defaults %} | |
<div class="m-<%= moduleName.replace('-', '_') %> {{ options.classname }}"> | |
<%= moduleName %> | |
</div> |
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
/** | |
* Create a new Module | |
* | |
* @package generator-mh-boilerplate | |
* @author Martin Herweg <[email protected]> | |
* | |
* Add a "module" part to your scripts section in package.json pointing to the module script | |
* Run `yarn module --js --html --css` remove flags you don't need | |
* | |
*/ | |
const memFs = require('mem-fs'); | |
const argv = require('yargs').argv; | |
const editor = require('mem-fs-editor'); | |
const inquirer = require('inquirer'); | |
const path = require('path'); | |
const pkg = require('../package.json'); | |
function createModule({moduleName, components = {} }) { | |
var store = memFs.create(); | |
var fs = editor.create(store); | |
const srcPath = path.resolve(pkg.srcPaths.views); | |
const dist = { | |
path: moduleName.includes('/') ? moduleName.split('/')[0] + '/' : '', | |
name: moduleName.includes('/') ? '_' + moduleName.split('/')[1] : `${moduleName}/_${moduleName}`, | |
template: moduleName.includes('/') ? moduleName.split('/')[1] : `${moduleName}`, | |
}; | |
const fileName = dist.path + dist.name; | |
try { | |
if (components.js) { | |
fs.copyTpl(path.resolve(__dirname, './module/_script.js'), `${srcPath}/modules/${fileName}.scripts.js`, { | |
moduleName: dist.template, | |
}); | |
console.log(`${srcPath}/modules/${fileName}.scripts.js`); | |
} | |
if (components.css) { | |
fs.copyTpl(path.resolve(__dirname, './module/_style.scss'), `${srcPath}/modules/${fileName}.styles.scss`, { | |
moduleName: dist.template, | |
}); | |
console.log(`${srcPath}/modules/${fileName}.styles.scss`); | |
} | |
if (components.template) { | |
fs.copyTpl(path.resolve(__dirname, './module/_template.html'), `${srcPath}/modules/${fileName}-template.html`, { | |
moduleName: dist.template, | |
}); | |
console.log(`${srcPath}/modules/${fileName}-template.html`); | |
} | |
console.log('Everything created'); | |
fs.commit(function(done) { | |
console.log('done'); | |
}); | |
} catch(e) { console.error(e); } | |
} | |
if(argv._.length) { | |
return createModule({ | |
moduleName: argv._[0], | |
components: { | |
js: argv.js || argv.javascript, | |
css: argv.css || argv.scss || argv.style, | |
template: argv.html || argv.template | |
} | |
}); | |
} | |
inquirer | |
.prompt([ | |
{ | |
type: 'input', | |
name: 'moduleName', | |
message: 'Please input module name', | |
default: 'module', | |
}, | |
{ | |
type: 'checkbox', | |
name: 'components', | |
message: 'Module Components', | |
choices: [ | |
{ | |
name: 'Style', | |
value: 'css' | |
}, | |
{ | |
name: 'JavaScript', | |
value: 'js', | |
}, | |
{ | |
name: 'Template', | |
value: 'template' | |
} | |
] | |
} | |
]) | |
.then(async (answers) => { | |
createModule({ | |
moduleName: answers.moduleName, | |
components: { | |
js: answers.components.includes('js') ? true : false, | |
css: answers.components.includes('css') ? true : false, | |
template: answers.components.includes('template') ? true : false | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment