import { Colour } from './colour'; import { IColourMap } from './icolour-map'; import * as fs from 'fs'; import * as path from 'path'; export class ColoursFile { private colours: IColourMap; private dirname: string; private name: string; private output: string = ''; private token: string = '\n\t/// content\n'; constructor(name: string, dirname: string, colours: IColourMap) { this.colours = colours; this.dirname = dirname; this.name = name; } private addContent(content: string) { this.output = this.output.replace(this.token, `\n\t${content}${this.token}`); } private clearToken() { this.output = this.output.replace(this.token, '\n'); } private templateDT(): string { return `declare module "${this.dirname.replace(`${process.cwd()}/d.ts`, 'ride-components')}dist/ts/colours/${this.name}" { ${this.token}}`; } private templateTS(): string { return `export namespace ${this.name} { ${this.token}}`; } public makeOutput(isDefinition?: boolean): void { let colours = this.colours[this.name]; this.output = isDefinition ? this.templateDT() : this.templateTS(); if (colours) { colours.forEach(colour => { this.addContent( isDefinition ? `export const ${colour.name}: string;` : `export const ${colour.name}: string = '${colour.value}';`); }); } } public write(): void { fs.writeFileSync(path.join(this.dirname, `./${this.name}.ts`), this.toString()); } public toString(): string { this.clearToken(); return this.output; } }