import { fromPairs, toPairs } from 'ramda'; import { File } from './utils'; type Dependency = [string, string]; export class SourceContainer { protected fileToTargetSource: Record = {}; protected fileContent: Record; public importDependencies: Record = {}; protected filesToMove: Record = {}; public constructor(sourceFiles: File[]) { this.fileContent = fromPairs(sourceFiles.map(({ fileName, content }) => [fileName, content])); } public getTargetSource(fileName) { return this.fileToTargetSource[fileName]; } public addTargetSource(fileName, target) { this.fileToTargetSource[fileName] = target; } public add(fileName, content) { this.fileContent[fileName] = content; } public addImportDependencies(importDependencies: Record = {}) { // if some template returns a dependency with a specified version // it should have a priority over the same dependency with the `latest` version const specificDependencies = fromPairs( Object.keys(importDependencies) .map((name) => { const version: string = this.importDependencies[name] && this.importDependencies[name] !== 'latest' ? this.importDependencies[name] : importDependencies[name]; if (importDependencies[name]) { return [name, version]; } return false; }) .filter(Boolean) ); // todo: version validation this.importDependencies = { ...this.importDependencies, ...importDependencies, ...specificDependencies, }; } public addFileToMove(from, to) { this.filesToMove[from] = to; } public outputSources() { return toPairs(this.fileContent).map(([fileName, content]) => ({ fileName, content, })); } }