Created
October 29, 2022 14:07
-
-
Save hosszukalman/b122c119b5f65e98f7fab9551794d7e2 to your computer and use it in GitHub Desktop.
[POC] Creating a dynamic generator with dynamic options using node's commander 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 { Command } = require('commander'); | |
const program = new Command(); | |
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]; | |
}); | |
}; | |
program | |
.name('generator') | |
.description('CLI to generate files based on the given json argument') | |
.version('0.0.0'); | |
program.command('generate') | |
.description('Generate files based on the given json argument') | |
.argument('<jsonPath>', 'path of the generator file') | |
.action((jsonPath) => { | |
let generatorRawData = fs.readFileSync(jsonPath); | |
const generatorData = JSON.parse(generatorRawData); | |
// Here is the tricky part, we need to collect the additional arguments | |
// and make key-value pairs from them. | |
let additionalOptions = {}; | |
for (i=2; i<program.args.length; i++) { | |
const argument = Object.fromEntries([program.args[i].split("=")]) | |
additionalOptions = { | |
...additionalOptions, | |
...argument | |
}; | |
} | |
// magicStrings will contains %(varName): varValue pairs. | |
let magicStrings = {}; | |
generatorData.requiredArgs.forEach(argumentName => { | |
// Check that all required options are set. | |
if (typeof additionalOptions[argumentName] === 'undefined') { | |
console.error(`Missing required argument. Please set <${argumentName}>=VALUE after --`); | |
exit(1); | |
} | |
magicStrings[`%(${argumentName})`]= additionalOptions[argumentName]; | |
}); | |
generatorData.optionalArgs.forEach(argumentName => { | |
magicStrings[`%(${argumentName})`]= additionalOptions[argumentName]; | |
}); | |
generatorData.templates.forEach(templateObj => { | |
console.log(`here we can generate file form ${templateObj.templatePath} to ${templateObj.destinationPath.mapReplace(magicStrings)} using yeoman`) | |
}); | |
}); | |
program.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