Skip to content

Commit

Permalink
Added support for the outputFormat flag for the `oslo-converter-sta…
Browse files Browse the repository at this point in the history
…keholders` package and allow file to be exported as `json`
  • Loading branch information
KristofVDB1 committed Mar 13, 2024
1 parent a8726f8 commit ee023b2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 24 deletions.
11 changes: 9 additions & 2 deletions packages/oslo-converter-stakeholders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
> Transforms an OSLO stakeholders csv file to JSON-LD
## Install

```bash
npm install @oslo-flanders/stakeholders-converter
```

## Global install

To use the service from the command line anywhere, you can install it globally.

```bash
npm install -g @oslo-flanders/stakeholders-converter
```
Expand All @@ -20,9 +23,13 @@ The service is executed from the CLI and expects the following parameters:
| --------- | --------- | ----------- | --------------- |
| `--input` | The URL or local file path of an OSLO stakeholders csv file | :heavy_check_mark: ||
| `--output` | Name of the output file | No, default `stakeholders.jsonld` ||
| `--outputFormat` | Which output format to generate the stakeholders file for | No, default `application/ld+json` |`application/ld+json` or `application/json`|

## Usage

```bash
oslo-stakeholders-converter --input path/to/stakeholders.csv --output path/to/output.jsonld
oslo-stakeholders-converter --input path/to/stakeholders.csv
```
oslo-stakeholders-converter --input path/to/stakeholders.csv --output path/to/output.jsonld
oslo-stakeholders-converter --input path/to/stakeholders.csv --output path/to/output.jsonld --outputFormat application/ld+json
oslo-stakeholders-converter --input path/to/stakeholders.csv --output path/to/output.json --outputFormat application/json
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable eslint-comments/disable-enable-pair */

import { writeFile } from 'fs/promises';
import type { IService } from '@oslo-flanders/core';
import type { StakeholdersDocument } from '@oslo-converter-stakeholders/interfaces/StakeholdersDocument';
import { fetchFileOrUrl, Logger, ServiceIdentifier } from '@oslo-flanders/core';

import { parse } from 'csv-parse';
Expand All @@ -12,7 +11,6 @@ import {
import { ContributorType } from '@oslo-converter-stakeholders/enums/ContributorType';
import { context } from '@oslo-converter-stakeholders/utils/JsonLdContext';
import { ToJsonLdTransformer } from '@oslo-converter-stakeholders/utils/ToJsonLdTransformer';

@injectable()
export class StakeholdersConversionService implements IService {
public readonly logger: Logger;
Expand All @@ -30,26 +28,58 @@ export class StakeholdersConversionService implements IService {
// Nothing to init here
}

public async run(): Promise<void> {
const data = await fetchFileOrUrl(this.configuration.input);
// helper methods for creating the StakeholdersDocument in the different output formats
private createJsonLdDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument {
const doc: StakeholdersDocument = {};
doc['@context'] = context;
doc.contributors = contributors;
doc.authors = authors;
doc.editors = editors;
return doc;
}

private createJsonDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument {
const doc: StakeholdersDocument = {};
doc.contributors = contributors;
doc.authors = authors;
doc.editors = editors;
return doc;
}

private createDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument {
switch (this.configuration.outputFormat) {
case 'application/json':
return this.createJsonDocument(authors, contributors, editors);
case 'application/ld+json':
return this.createJsonLdDocument(authors, contributors, editors);
default:
return this.createJsonLdDocument(authors, contributors, editors);
}
}

private async parseData(data: Buffer): Promise<{ authors: object[], contributors: object[], editors: object[] }> {
const parser = parse({ delimiter: ';', columns: true });
const transformer = new ToJsonLdTransformer();

const contributors: any[] = [];
const authors: any[] = [];
const editors: any[] = [];
const contributors: object[] = [];
const authors: object[] = [];
const editors: object[] = [];

await new Promise<void>((resolve, reject) => {
parser.pipe(transformer)
.on('data', (object: any) => {
if (object.contributorType === ContributorType.Author) {
authors.push(object);
} else if (object.contributorType === ContributorType.Contributor) {
contributors.push(object);
} else if (object.contributorType === ContributorType.Editor) {
editors.push(object);
} else {
this.logger.error(`Unable to find the contributor type for "${object.firstName} ${object.lastName}."`);
switch (object.contributorType) {
case ContributorType.Author:
authors.push(object);
break;
case ContributorType.Contributor:
contributors.push(object);
break;
case ContributorType.Editor:
editors.push(object);
break;
default:
this.logger.error(`Unable to find the contributor type for "${object.firstName} ${object.lastName}."`);
}

delete object.contributorType;
Expand All @@ -61,12 +91,15 @@ export class StakeholdersConversionService implements IService {
parser.end();
});

const doc: any = {};
doc['@context'] = context;
doc.contributors = contributors;
doc.authors = authors;
doc.editors = editors;
return { authors, contributors, editors };
}

public async run(): Promise<void> {
const data = await fetchFileOrUrl(this.configuration.input);
const { authors, contributors, editors } = await this.parseData(data);

const doc: StakeholdersDocument = this.createDocument(authors, contributors, editors);

await writeFile(this.configuration.output, JSON.stringify(doc, null, 2));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class StakeholdersConversionServiceRunner extends
default: 'info',
choices: LOG_LEVELS,
})
.option('outputFormat', {
describe: 'Define the output format',
default: 'application/ld+json',
choices: ['application/ld+json', 'application/json'],
})
.demandOption(['input'],
'Please provide the necessary arguments to work with this tool.')
.help('h')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export class StakeholdersConversionServiceConfiguration implements IConfiguratio
*/
private _output: string | undefined;

/**
* Format of the output file
*/
private _outputFormat: string | undefined;

public async createFromCli(params: YargsParams): Promise<void> {
this._input = <string>params.input;
this._output = <string>params.output;
this._outputFormat = <string>params.outputFormat;
}

public get input(): string {
Expand All @@ -31,4 +37,11 @@ export class StakeholdersConversionServiceConfiguration implements IConfiguratio
}
return this._output;
}

public get outputFormat(): string {
if (!this._outputFormat) {
throw new Error(`Trying to access property "outputFormat" before it was set.`);
}
return this._outputFormat;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface StakeholdersDocument {
'@context'?: object;
contributors?: object[];
authors?: object[];
editors?: object[];
}

0 comments on commit ee023b2

Please sign in to comment.