import { ASTItem } from '../ast'; import { template, templateDefintions } from './ts-template'; import { REGEXP_SANITIZER_OUTPUT } from '../regexps'; export class FileAST { public static newContentDelimiter: string = '/// content'; public content: string; public name: string; public parseDefintions: boolean; constructor(astItem: ASTItem, parseDefintions: boolean) { this.name = parseDefintions ? `${astItem.rawname}.d.ts`: `${astItem.rawname}.ts`; this.content = parseDefintions ? templateDefintions(astItem) : template(astItem); this.parseDefintions = parseDefintions; } private replaceContent(content: string) { let lines = this.content.split('\n'); let lineNumber = lines.map(line => line.trim()).indexOf(FileAST.newContentDelimiter); lines.splice(lineNumber, 0, content); this.content = lines.join('\n'); } public addContent(content: ASTItem) { if (this.parseDefintions) { this.replaceContent(`\texport const ${content.name}: string;`); } else { this.replaceContent(`\texport const ${content.name}: string = '${content.value}';`); } } public toString(): string { let lines: string[] = this.content.split('\n'); return lines .filter(line => line.trim() !== FileAST.newContentDelimiter) .filter(line => line.trim().length > 0) .join('\n') .replace(REGEXP_SANITIZER_OUTPUT, ''); } }