import {join} from "path"; import {existsSync, readdirSync, readFileSync, statSync} from "fs"; import * as marked from "marked"; import {checkFolder} from "./shared/check_folder"; import {requireNoCache} from "./shared/require_no_cache"; import {views} from "./views"; import {Config, getConfig} from "./config"; export class Data { private config: Config = getConfig(); public getPopsVersion(): any { const packageJson: any = require(join(__dirname, '..', 'package.json')); return { version: packageJson.version }; } public getDocs(): any[] { const src: string = this.config.src; const folder: string = join(src, 'docs'); const docs: any[] = []; checkFolder(folder); if (existsSync(folder) && statSync(folder).isDirectory()) { const items: string[] = readdirSync(folder); items.map((item) => { const fileContents: string = readFileSync(join(folder, item), 'utf8'); const doc: Object = {}; doc['name'] = item; doc['template'] = fileContents; doc['compiledView'] = marked(fileContents); docs.push(doc); }); } return docs; } public getItems(type: string): any[] { const src: string = this.config.src; const folder: string = join(src, type); const itemsOfType: any[] = []; checkFolder(folder); if (existsSync(folder) && statSync(folder).isDirectory()) { const items: string[] = readdirSync(folder); items.map((item) => { try { const itemObject: Object = requireNoCache(join(folder, item)); if (views[this.config.ext.templates]) { const viewEngine: any = new views[this.config.ext.templates](); itemObject['compiledView'] = viewEngine.compile( itemObject['template'], itemObject['context'] ); } else { itemObject['compiledView'] = ''; } itemObject['doc'] = marked(itemObject['doc'] || ''); itemsOfType.push(itemObject); } catch (error) { throw error; } }); } return itemsOfType; } public getAllItems(): Object { const pops: any = this.getPopsVersion(); return { pops, 'config': this.config, 'patterns': this.getItems('patterns'), 'components': this.getItems('components'), 'pages': this.getItems('pages'), } } }