mirror of
https://github.com/renovatebot/renovate.git
synced 2024-12-22 13:38:32 +00:00
c93154bd24
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
32 lines
790 B
TypeScript
32 lines
790 B
TypeScript
import type { CodeFormat } from './code-format';
|
|
import type { IndentationType } from './indentation-type';
|
|
|
|
export class JSONWriter {
|
|
private readonly indentationType: IndentationType;
|
|
|
|
private readonly indentationSize: number;
|
|
|
|
constructor(codeFormat: CodeFormat = {}) {
|
|
this.indentationSize = codeFormat.indentationSize ?? 2;
|
|
this.indentationType = codeFormat.indentationType ?? 'space';
|
|
}
|
|
|
|
public write(json: unknown, newLineAtTheEnd = true): string {
|
|
let content = JSON.stringify(json, null, this.indentation);
|
|
|
|
if (newLineAtTheEnd) {
|
|
content = content.concat('\n');
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
private get indentation(): string | number {
|
|
if (this.indentationType === 'tab') {
|
|
return '\t';
|
|
}
|
|
|
|
return this.indentationSize;
|
|
}
|
|
}
|