Implement TSDocConfigFile.saveFile() and saveToObject()

This commit is contained in:
Pete Gonzalez 2021-01-17 17:56:27 -08:00
Родитель 0f23c1b565
Коммит 612763b1fa
2 изменённых файлов: 81 добавлений и 1 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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",
},
],
}
`);
});