From 612763b1fa24261c545877ad10c5acf7ba662e0f Mon Sep 17 00:00:00 2001 From: Pete Gonzalez <4673363+octogonz@users.noreply.github.com> Date: Sun, 17 Jan 2021 17:56:27 -0800 Subject: [PATCH] Implement TSDocConfigFile.saveFile() and saveToObject() --- tsdoc-config/src/TSDocConfigFile.ts | 61 ++++++++++++++++++- .../src/__tests__/TSDocConfigFile.test.ts | 21 +++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/tsdoc-config/src/TSDocConfigFile.ts b/tsdoc-config/src/TSDocConfigFile.ts index 845e94a..785b80a 100644 --- a/tsdoc-config/src/TSDocConfigFile.ts +++ b/tsdoc-config/src/TSDocConfigFile.ts @@ -36,7 +36,6 @@ interface ITagConfigJson { interface IConfigJson { $schema: string; - tsdocVersion: string; extends?: string[]; tagDefinitions?: ITagConfigJson[]; supportForTags?: { [tagName: string]: boolean }; @@ -372,6 +371,66 @@ export class TSDocConfigFile { return configFile; } + /** + * Writes the config file content to a JSON file with the specified file path. + */ + public saveFile(jsonFilePath: string): void { + const jsonObject: unknown = this.saveToObject(); + const jsonContent: string = JSON.stringify(jsonObject, undefined, 2); + fs.writeFileSync(jsonFilePath, jsonContent); + } + + /** + * Writes the object state into a JSON-serializable object. + */ + public saveToObject(): unknown { + const configJson: IConfigJson = { + $schema: TSDocConfigFile.CURRENT_SCHEMA_URL, + }; + + if (this.tagDefinitions.length > 0) { + configJson.tagDefinitions = []; + for (const tagDefinition of this.tagDefinitions) { + configJson.tagDefinitions.push(TSDocConfigFile._serializeTagDefinition(tagDefinition)); + } + } + + if (this.supportForTags.size > 0) { + configJson.supportForTags = {}; + this.supportForTags.forEach((supported, tagName) => { + configJson.supportForTags![tagName] = supported; + }); + } + + return configJson; + } + + private static _serializeTagDefinition(tagDefinition: TSDocTagDefinition): ITagConfigJson { + let syntaxKind: 'inline' | 'block' | 'modifier' | undefined; + switch (tagDefinition.syntaxKind) { + case TSDocTagSyntaxKind.InlineTag: + syntaxKind = 'inline'; + break; + case TSDocTagSyntaxKind.BlockTag: + syntaxKind = 'block'; + break; + case TSDocTagSyntaxKind.ModifierTag: + syntaxKind = 'modifier'; + break; + default: + throw new Error('Unimplemented TSDocTagSyntaxKind'); + } + + const tagConfigJson: ITagConfigJson = { + tagName: tagDefinition.tagName, + syntaxKind, + }; + if (tagDefinition.allowMultiple) { + tagConfigJson.allowMultiple = true; + } + return tagConfigJson; + } + /** * Returns a report of any errors that occurred while attempting to load this file or any files * referenced via the "extends" field. diff --git a/tsdoc-config/src/__tests__/TSDocConfigFile.test.ts b/tsdoc-config/src/__tests__/TSDocConfigFile.test.ts index 4454da7..ed49c4d 100644 --- a/tsdoc-config/src/__tests__/TSDocConfigFile.test.ts +++ b/tsdoc-config/src/__tests__/TSDocConfigFile.test.ts @@ -45,6 +45,7 @@ test('Load p1', () => { } `); }); + test('Load p2', () => { expect(testLoadingFolder('assets/p2')).toMatchInlineSnapshot(` Object { @@ -72,6 +73,7 @@ test('Load p2', () => { } `); }); + test('Load p3', () => { expect(testLoadingFolder('assets/p3')).toMatchInlineSnapshot(` Object { @@ -149,6 +151,7 @@ test('Load p3', () => { } `); }); + test('Load p4', () => { expect(testLoadingFolder('assets/p4')).toMatchInlineSnapshot(` Object { @@ -192,3 +195,21 @@ test('Load p4', () => { } `); }); + +test('Re-serialize p2', () => { + const configFile: TSDocConfigFile = TSDocConfigFile.loadForFolder(path.join(__dirname, 'assets/p3')); + expect(configFile.saveToObject()).toMatchInlineSnapshot(` + Object { + "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json", + "supportForTags": Object { + "@base2": true, + }, + "tagDefinitions": Array [ + Object { + "syntaxKind": "modifier", + "tagName": "@root", + }, + ], + } + `); +});