Created
February 17, 2017 13:04
-
-
Save felisio/30a6aab33a6df9740e3f26252429721d to your computer and use it in GitHub Desktop.
Gulp Generate
This file contains 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
const gulp = require('gulp'); | |
const template = require('gulp-template'); | |
const rename = require('gulp-rename'); | |
const path = require('path'); | |
const yargs = require('yargs'); | |
const appRoot = require('app-root-path'); | |
const loadsh = require('lodash'); | |
require('./contains'); | |
/* | |
* Gerador de Codigos | |
* task para gerar query Graphql: gulp graph --name 'nome-no-singular-minusculo' (opcional)[--model 'id \n name'] | |
* task para gerar Component: gulp component --name 'nome-no-singular-minusculo' --parent Nome-da-pasta-aonde-sera-inserido | |
* task para gerar Component e GraphQl: gulp crud --name 'nome-no-singular-minusculo' --parent Nome-da-pasta-aonde-sera-inserido | |
* Exemplo de uso: gulp crud --name 'modality' --parent Incomes | |
*/ | |
const paths = { | |
graphTemplates: path.join(__dirname, 'graphql/**/*.**'), | |
componentTemplate: path.join(__dirname, 'crud/**/*.**') | |
}; | |
const resolveToComponents = (glob) => { | |
glob = glob || ''; | |
return path.join(appRoot.toString(), 'src/', glob); | |
}; | |
const toCamelCase = val => val.charAt(0).toUpperCase() + val.slice(1); | |
const toLowerCase = val => val.charAt(0).toLowerCase() + val.slice(1); | |
const toPlural = (val) => { | |
const lastTwo = val.substr(val.length-2); | |
const lastOne = val.substr(val.length-1); | |
const arrayTwo = ['ay', 'ey', 'iy', 'oy', 'uy']; | |
const arrayOne = ['o', 's', 'x', 'z']; | |
if(arrayTwo.contains(lastTwo)) return `${val}s`; | |
if(['sh', 'ch'].contains(lastTwo)) return `${val}es`; | |
if(arrayOne.contains(lastOne)) return `${val}es`; | |
if(lastOne === 'y') return `${val.slice(0, -1)}ies`; | |
if(val === 'have') return 'has'; | |
return `${val}s`; | |
}; | |
gulp.task('graph', () => { | |
const name = yargs.argv.name; | |
const model = yargs.argv.model || 'id \n name'; | |
const destPath = path.join(appRoot.toString(), 'src/graphql'); | |
console.log('Gerando querys para o modelo: ',name); | |
return gulp.src(paths.graphTemplates) | |
.pipe(template({ | |
name: name, | |
pluralName: toPlural(name), | |
model: model, | |
upCaseName: toCamelCase(name), | |
pluralNameUpCase: toCamelCase(toPlural(name)) | |
})) | |
.pipe(rename((path) => { | |
path.basename = path.basename.replace('temp', name); | |
})) | |
.pipe(gulp.dest(destPath)); | |
}); | |
gulp.task('component', () => { | |
const name = yargs.argv.name; | |
const parentPath = yargs.argv.parent || ''; | |
const destPath = path.join(resolveToComponents('components'), parentPath, toCamelCase(name)); | |
console.log('Gerando Component para o modelo: ',name); | |
return gulp.src(paths.componentTemplate) | |
.pipe(template({ | |
name: name, | |
pluralName: toPlural(name), | |
upCaseName: toCamelCase(name), | |
parentName: toLowerCase(parentPath), | |
kebabName: loadsh.kebabCase(name) | |
})) | |
.pipe(rename((path) => { | |
path.basename = path.basename.replace('temp.list', toCamelCase(name)+'List'); | |
})) | |
.pipe(rename((path) => { | |
path.basename = path.basename.replace('temp.form', toCamelCase(name)+'Form'); | |
})) | |
.pipe(rename((path) => { | |
path.basename = path.basename.replace('router', 'router'); | |
})) | |
.pipe(gulp.dest(toCamelCase(destPath))); | |
}); | |
gulp.task('crud', ['graph', 'component']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment