| | import fs from 'fs' |
| | import os from 'os' |
| | import path from 'path' |
| |
|
| | import yaml from 'js-yaml' |
| |
|
| | interface DataStructure { |
| | [key: string]: string | DataStructure |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export class DataDirectory { |
| | root: string |
| |
|
| | constructor(data: DataStructure, root?: string) { |
| | this.root = root || this.createTempRoot('data-directory') |
| | this.create(data) |
| | } |
| |
|
| | createTempRoot(prefix: string): string { |
| | const fullPath = path.join(os.tmpdir(), prefix) |
| | return fs.mkdtempSync(fullPath) |
| | } |
| |
|
| | create( |
| | data: DataStructure, |
| | root: string | null = null, |
| | isVariables = false, |
| | isReusables = false, |
| | ): void { |
| | const here = root || this.root |
| |
|
| | for (const [key, value] of Object.entries(data)) { |
| | if (isReusables) { |
| | if (typeof value === 'string') { |
| | fs.writeFileSync(path.join(here, `${key}.md`), value, 'utf-8') |
| | } else { |
| | fs.mkdirSync(path.join(here, key)) |
| | |
| | this.create(value as DataStructure, path.join(here, key), false, true) |
| | } |
| | } else if (isVariables) { |
| | fs.writeFileSync(path.join(here, `${key}.yml`), yaml.dump(value), 'utf-8') |
| | } else { |
| | if (key === 'ui') { |
| | fs.writeFileSync(path.join(here, `${key}.yml`), yaml.dump(value), 'utf-8') |
| | } else { |
| | const there = path.join(here, key) |
| | fs.mkdirSync(there) |
| | |
| | if (key === 'reusables') { |
| | this.create(value as DataStructure, there, false, true) |
| | } else if (key === 'variables') { |
| | this.create(value as DataStructure, there, true, false) |
| | } else { |
| | this.create(value as DataStructure, there) |
| | } |
| | } |
| | } |
| | } |
| | } |
| |
|
| | destroy(): void { |
| | fs.rmSync(this.root, { recursive: true }) |
| | } |
| | } |
| |
|