diff --git a/src/cli/program.ts b/src/cli/program.ts index fb62ebbaa..d22b38a29 100644 --- a/src/cli/program.ts +++ b/src/cli/program.ts @@ -1138,7 +1138,9 @@ utils utils .command('rules') .description('returns technology rules') - .addOption(new Option('--format [string]', 'output format').default('yaml').choices(['yaml', 'json', 'latex'])) + .addOption( + new Option('--format [string]', 'output format').default('yaml').choices(['yaml', 'json', 'latex', 'csv']) + ) .action( hae.exit(async options => { std.out(await Controller.utils.rules(options)) diff --git a/src/controller/utils/rules.ts b/src/controller/utils/rules.ts index 1acf68185..4f364e411 100644 --- a/src/controller/utils/rules.ts +++ b/src/controller/utils/rules.ts @@ -7,7 +7,11 @@ export type RuleOptions = { export default async function (options: RuleOptions) { options.format = options.format ?? 'yaml' + + const headers = ['component', 'artifact', 'hosting', 'technology', 'weight'] + return files.toFormat(Registry.rules, options.format, { - latex: {headers: ['component', 'artifact', 'hosting', 'technology', 'weight']}, + latex: {headers}, + csv: {headers}, }) } diff --git a/src/utils/files.ts b/src/utils/files.ts index 682b17417..bfe3c50a5 100644 --- a/src/utils/files.ts +++ b/src/utils/files.ts @@ -14,6 +14,7 @@ import * as yaml from 'js-yaml' import lnk from 'lnk' import _ from 'lodash' import os from 'os' +import papa from 'papaparse' import path from 'path' // TODO: fix import problem // @ts-ignore @@ -163,13 +164,14 @@ export async function loadXML(file: string) { return (await xml2js.parseStringPromise(loadFile(file) /*, options */)) as T } -export function toFormat(obj: any, format: string, options: {latex?: LatexOptions} = {}) { +export function toFormat(obj: any, format: string, options: {latex?: LatexOptions; csv?: CSVOptions} = {}) { if (format === 'yaml') return toYAML(obj) if (format === 'json') return toJSON(obj) if (format === 'ini') return toINI(obj) if (format === 'env') return toENV(obj) if (format === 'xml') return toXML(obj) if (format === 'latex') return toLatex(obj, options.latex) + if (format === 'csv') return toCSV(obj, options.csv) throw new Error(`Format "${format}" not supported`) } @@ -178,6 +180,15 @@ export type LatexOptions = { headers?: string[] } +export type CSVOptions = { + headers?: string[] +} + +export function toCSV(obj: any, options: CSVOptions = {}) { + assert.isArray(obj) + return papa.unparse(obj, {columns: options.headers}) +} + export function toLatex(obj: any, options: LatexOptions = {}) { assert.isArray(obj) // TODO: this is dirty