Created
October 29, 2022 14:13
-
-
Save hosszukalman/b62b7eecbf5275e91749c00aaaa9f8a5 to your computer and use it in GitHub Desktop.
[POC] Creating a dynamic generator with dynamic options using node's yargs package
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
{ | |
"requiredArgs": [ | |
"where", | |
"command" | |
], | |
"optionalArgs": [ | |
"mutation-name" | |
], | |
"templates": [ | |
{ | |
"templatePath": "modules/core/public/generators/command/command.liquid", | |
"destinationPath": "%(where)/%(command).liquid" | |
}, | |
{ | |
"templatePath": "modules/core/public/generators/command/build.liquid", | |
"destinationPath": "%(where)/%(command)/build.liquid" | |
}, | |
{ | |
"templatePath": "modules/core/public/generators/command/check.liquid", | |
"destinationPath": "%(where)/%(command)/check.liquid" | |
} | |
] | |
} |
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 fs = require('fs'); | |
const { exit } = require('process'); | |
const yargs = require('yargs/yargs') | |
const { hideBin } = require('yargs/helpers') | |
String.prototype.mapReplace = function(map) { | |
var regex = []; | |
for(var key in map) | |
regex.push(key.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")); | |
return this.replace(new RegExp(regex.join('|'),"g"),function(word){ | |
return map[word]; | |
}); | |
}; | |
yargs(hideBin(process.argv)) | |
.command('generate <jsonPath>', 'Generate files based on the given json argument', (yargs) => { | |
return yargs | |
.positional('jsonPath', { | |
describe: 'path of the generator file' | |
}) | |
}, (argv) => { | |
let generatorRawData = fs.readFileSync(argv.jsonPath); | |
const generatorData = JSON.parse(generatorRawData); | |
// magicStrings will contains %(varName): varValue pairs. | |
let magicStrings = {}; | |
generatorData.requiredArgs.forEach(argumentName => { | |
// Check that all required options are set. | |
if (typeof argv[argumentName] === 'undefined') { | |
console.error(`Missing required option. Please set --${argumentName} VALUE in the command line`); | |
exit(1); | |
} | |
magicStrings[`%(${argumentName})`]= argv[argumentName]; | |
}); | |
generatorData.optionalArgs.forEach(argumentName => { | |
magicStrings[`%(${argumentName})`]= argv[argumentName]; | |
}); | |
generatorData.templates.forEach(templateObj => { | |
console.log(`here we can generate file form ${templateObj.templatePath} to ${templateObj.destinationPath.mapReplace(magicStrings)} using yeoman`) | |
}); | |
}) | |
.parse() |
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
$ node generator.js generate gen.json --command delete --where modules/user/public/views/partials/lib/commands/user | |
here we can generate file form modules/core/public/generators/command/command.liquid to modules/user/public/views/partials/lib/commands/user/delete.liquid using yeoman | |
here we can generate file form modules/core/public/generators/command/build.liquid to modules/user/public/views/partials/lib/commands/user/delete/build.liquid using yeoman | |
here we can generate file form modules/core/public/generators/command/check.liquid to modules/user/public/views/partials/lib/commands/user/delete/check.liquid using yeoman |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment