@carlosbajo/roket-micro
Version:
framework para microservicios con google/pubsub
75 lines (72 loc) • 2.03 kB
JavaScript
const fs = require('fs');
function trimTemplateFile(template) {
return fs.readFileSync(`${__dirname}/${template}.hbs`, 'utf8').replace(/\s*$/, '');
}
module.exports = function plopFunction(plop) {
return {
description: 'kafka pub sub generator',
prompts: [
{
type: 'input',
name: 'listener',
message: 'which topic is it goin to listen for?',
validate: (value) => {
if (!(/^$|-/).test(value)) { return true; }
return 'invalid format';
}
},
{
type: 'input',
name: 'methodListen',
message: 'name of the method to listen for?',
validate: (value) => {
if (!(/^$|-/).test(value)) { return true; }
return 'invalid format';
}
},
{
type: 'input',
name: 'method',
message: 'name of the method to run when it listens?',
validate: (value) => {
if (!(/^$|-/).test(value)) { return true; }
return 'invalid format';
}
},
{
type: 'input',
name: 'partitions',
message: 'number of partitions?',
validate: (value) => {
if ((/^$|[0-9]/).test(value)) { return true; }
return 'Numeric input';
}
},
{
type: 'input',
name: 'consumer',
message: 'which consumer file will it use?',
validate: (value) => {
if (!(/^$|-/).test(value)) { return true; }
return 'invalid format';
}
},
],
actions: function res(answer) {
const set = [];
set.push({
type: 'modify',
path: `${plop.getPlopfilePath()}/api/consumer/${answer.consumer}.js`,
pattern: /(module.exports = {)/g,
template: trimTemplateFile('consumer.hbs'),
});
set.push({
type: 'modify',
path: 'api/config/pubsub.js',
pattern: /(module.exports = res;)/g,
template: trimTemplateFile('config.hbs'),
});
return set;
}
};
};