From 27104f9d740c7c18923e2b667f7c2615e554fe40 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Fri, 22 Oct 2021 14:47:56 +0530 Subject: [PATCH] Optional server port arg. for watcher generation (#284) --- packages/codegen/README.md | 3 ++- packages/codegen/src/config.ts | 4 +++- packages/codegen/src/generate-code.ts | 8 +++++++- packages/codegen/src/templates/config-template.handlebars | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/codegen/README.md b/packages/codegen/README.md index 7b9c3791..8b466b93 100644 --- a/packages/codegen/README.md +++ b/packages/codegen/README.md @@ -21,7 +21,7 @@ * Run the following command to generate a watcher from a contract file: ```bash - yarn codegen --input-file --contract-name --output-folder [output-folder] --mode [eth_call | storage | all] --flatten [true | false] --kind [lazy | active] + yarn codegen --input-file --contract-name --output-folder [output-folder] --mode [eth_call | storage | all] --flatten [true | false] --kind [lazy | active] --port [server-port] ``` * `input-file`(alias: `i`): Input contract file path or an URL (required). @@ -30,6 +30,7 @@ * `mode`(alias: `m`): Code generation mode (default: `all`). * `flatten`(alias: `f`): Flatten the input contract file (default: `true`). * `kind` (alias: `k`): Kind of watcher (default: `active`). + * `port` (alias: `p`): Server port (default: `3008`). **Note**: When passed an *URL* as `input-file`, it is assumed that it points to an already flattened contract file. diff --git a/packages/codegen/src/config.ts b/packages/codegen/src/config.ts index ba13392b..b5eb5543 100644 --- a/packages/codegen/src/config.ts +++ b/packages/codegen/src/config.ts @@ -12,14 +12,16 @@ const TEMPLATE_FILE = './templates/config-template.handlebars'; /** * Writes the config file generated from a template to a stream. * @param watcherKind Watcher kind to be passed to the template. + * @param port Port for the watcher server. * @param folderName Watcher folder name to be passed to the template. * @param outStream A writable output stream to write the config file to. */ -export function exportConfig (watcherKind: string, folderName: string, outStream: Writable): void { +export function exportConfig (watcherKind: string, port: number, folderName: string, outStream: Writable): void { const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString(); const template = Handlebars.compile(templateString); const config = template({ watcherKind, + port, folderName }); outStream.write(config); diff --git a/packages/codegen/src/generate-code.ts b/packages/codegen/src/generate-code.ts index 88c5b1af..5c7e7e82 100644 --- a/packages/codegen/src/generate-code.ts +++ b/packages/codegen/src/generate-code.ts @@ -62,6 +62,12 @@ const main = async (): Promise => { default: KIND_ACTIVE, choices: [KIND_ACTIVE, KIND_LAZY] }) + .option('port', { + alias: 'p', + describe: 'Server port.', + type: 'number', + default: 3008 + }) .option('flatten', { alias: 'f', describe: 'Flatten the input contract file.', @@ -157,7 +163,7 @@ function generateWatcher (data: string, visitor: Visitor, argv: any) { outStream = outputDir ? fs.createWriteStream(path.join(outputDir, 'environments/local.toml')) : process.stdout; - exportConfig(argv.kind, path.basename(outputDir), outStream); + exportConfig(argv.kind, argv.port, path.basename(outputDir), outStream); outStream = outputDir ? fs.createWriteStream(path.join(outputDir, 'src/artifacts/', `${inputFileName}.json`)) diff --git a/packages/codegen/src/templates/config-template.handlebars b/packages/codegen/src/templates/config-template.handlebars index 26efb854..44340922 100644 --- a/packages/codegen/src/templates/config-template.handlebars +++ b/packages/codegen/src/templates/config-template.handlebars @@ -1,6 +1,6 @@ [server] host = "127.0.0.1" - port = 3008 + port = {{port}} kind = "{{watcherKind}}" # Checkpointing state.