From 1ea88dfc5c0203630c5b44bc24bb0d797199d1bf Mon Sep 17 00:00:00 2001 From: David de Boer Date: Wed, 12 Jun 2024 16:17:44 +0200 Subject: [PATCH] feat: Simplify CLI args by removing configDir (#82) --- CONTRIBUTING.md | 2 +- package.json | 4 ++-- src/cliArgs.ts | 41 +++++++---------------------------------- src/main.ts | 23 +++++------------------ src/utils/init.ts | 2 +- 5 files changed, 16 insertions(+), 56 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 301935e..d5b667b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,7 +11,7 @@ git clone https://github.com/netwerk-digitaal-erfgoed/ld-workbench.git cd ld-workbench npm i npm run compile -npm run ld-workbench -- --configDir static/example +npm run ld-workbench -- --config static/example ``` The configuration of this project is validated and defined by [JSON Schema](https://json-schema.org). The schema is located in `./static/ld-workbench-schema.json`. To create the types from this schema, run `npm run util:json-schema-to-typescript`. This will regenerate `./src/types/LDWorkbenchConfiguration.d.ts`, do not modify this file by hand. diff --git a/package.json b/package.json index 98949e6..abeabd7 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "prepare": "husky install", "dev": "tsc --watch --preserveWatchOutput", "ld-workbench": "node build/main", - "ld-workbench:example": "node build/main --configDir static", + "ld-workbench:example": "node build/main --config static", "util:json-schema-to-typescript": "npx json2ts -i ./static/ld-workbench.schema.json -o src/lib/LDWorkbenchConfiguration.d.ts", "semantic-release": "semantic-release", "lint": "gts lint", @@ -21,7 +21,7 @@ "compile": "tsc", "fix": "gts fix", "pretest": "npm run compile", - "posttest": "npm run lint" + "posttest": "npm run fix" }, "repository": { "type": "git", diff --git a/src/cliArgs.ts b/src/cliArgs.ts index 3685113..5d802a7 100644 --- a/src/cliArgs.ts +++ b/src/cliArgs.ts @@ -8,12 +8,9 @@ program .name('ld-workbench') .description('CLI tool to transform Linked Data using SPARQL') .option( - '-c, --config ', - 'Path to a configuration file for your pipeline.' - ) - .option( - '--configDir ', - 'Path to a folder containing your configuration files.' + '-c, --config ', + 'Path to the directory containing your pipeline configuration(s)', + 'pipelines/' ) .option( '-p, --pipeline ', @@ -23,16 +20,15 @@ program '-s, --stage ', 'Name of the stage of the pipeline you want to run' ) - .option('--init', 'Initializes a new LDWorkbench project') + .option('--init', 'Initialize a new LDWorkbench project') .option( '--silent', - 'Disable console output, including the progress indicator.' + 'Disable console output, including the progress indicator' ) .version(version()); program.parse(); export const cliArgs: { - config?: string; - configDir?: string; + config: string; pipeline?: string; stage?: string; silent?: boolean; @@ -40,7 +36,7 @@ export const cliArgs: { } = program.opts(); if (cliArgs.init !== undefined) { - if (Object.values(cliArgs).length !== 1) { + if (Object.values(cliArgs).length !== 2) { error( 'The --init flag can not be used in conjunction with other CLI arguments.' ); @@ -56,26 +52,3 @@ if (cliArgs.init !== undefined) { error(e as Error); } } - -if (cliArgs.config !== undefined && cliArgs.configDir !== undefined) { - error( - 'Do not use both the --config and the --configDir options.', - 1, - `${chalk.italic( - '--config' - )} should be used to process a single pipeline, ${chalk.italic( - '--configDir' - )} should be used to overwrite the default director where LD Workbench searches configs (${chalk.italic( - './pipelines/' - )}).` - ); -} -if (cliArgs.config !== undefined && cliArgs.pipeline !== undefined) { - error( - 'Do not use both the --config and the --pipeline options.', - 1, - `Both ${chalk.italic('--config')} and ${chalk.italic( - '--pipeline' - )} should be used to process a single pipeline.` - ); -} diff --git a/src/main.ts b/src/main.ts index a85b076..ac8cfa5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,26 +13,19 @@ console.info( ); async function main(): Promise { - let pipelines = new Map(); - pipelines = loadPipelines( - cliArgs.config ?? cliArgs.configDir ?? './pipelines/' - ); - - const names = Array.from(pipelines.keys()); - - // this will be the configuration we use: + const pipelines = loadPipelines(cliArgs.config ?? './pipelines/'); + const names = [...pipelines.keys()]; let configuration: LDWorkbenchConfiguration | undefined; if (cliArgs.pipeline !== undefined) { - const config = pipelines.get(cliArgs.pipeline); - if (config === undefined) { + configuration = pipelines.get(cliArgs.pipeline); + if (configuration === undefined) { error( - `No pipeline named "${cliArgs.pipeline}" was found.`, + `No pipeline named “${cliArgs.pipeline}” was found.`, 2, `Valid pipeline names are: ${names.map(name => `"${name}"`).join(', ')}` ); } - configuration = pipelines.get(cliArgs.pipeline); } else if (names.length === 1) { configuration = pipelines.get(names[0]); } @@ -54,12 +47,6 @@ async function main(): Promise { ); } configuration = pipelines.get(answers.pipeline); - } else if (names.length !== 1) { - error( - 'This should not happen: no pipeline was picked, but we have multiple.' - ); - } else { - configuration = pipelines.get(names[0]); } if (configuration === undefined) { diff --git a/src/utils/init.ts b/src/utils/init.ts index ec963a8..507edfb 100644 --- a/src/utils/init.ts +++ b/src/utils/init.ts @@ -9,7 +9,7 @@ export default function init(): void { throw new Error( 'The --init script found an existing directory "' + path.join('pipelines', 'configurations', 'example') + - '". Make sure this directory does not exists before running this script.' + '". Make sure this directory does not exist before running this script.' ); } fs.mkdirSync(path.join('pipelines', 'data'), {recursive: true});