From be6c90cc65fef3782c64b40ce8ba2cd4b1852331 Mon Sep 17 00:00:00 2001 From: Braden Mars Date: Sun, 12 Feb 2023 03:31:47 -0600 Subject: [PATCH] feat(core): temp workspace provider implementation. Signed-off-by: Braden Mars --- .../src/workspace.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 packages/openapi-generator-clients/src/workspace.ts diff --git a/packages/openapi-generator-clients/src/workspace.ts b/packages/openapi-generator-clients/src/workspace.ts new file mode 100644 index 00000000..9c7fa20c --- /dev/null +++ b/packages/openapi-generator-clients/src/workspace.ts @@ -0,0 +1,43 @@ +import type { + GeneratorConfig, + Workspace, + WorkspaceProvider, +} from '@openapi-generator-clients/types' +import {Record as ImRecord, type RecordOf} from 'immutable' +import {getTempDir} from '@openapi-generator-clients/utils' +import path from 'pathe' +import {partial} from 'rambdax/immutable.js' +import fse from 'fs-extra' + +export const WorkspaceRecord = ImRecord({ + rootPath: '', + templatesPath: '', + configPath: '', + outputPath: '', +}) +export interface WorkspaceRecord extends RecordOf {} + +export const temporaryWorkspaceProvider: WorkspaceProvider = { + workspace: WorkspaceRecord(), + async create(outputDir: string, overwrite = false) { + const root = getTempDir() + const resolve = partial(path.resolve, [root]) + this.workspace = WorkspaceRecord({ + rootPath: root, + templatesPath: resolve('./templates'), + configPath: resolve('./config.json'), + outputPath: overwrite ? outputDir : resolve(outputDir), + }) + await fse.ensureDir(this.workspace.templatesPath) + return this + }, + async addTemplatePath(path: string) { + const templatesDir = this.workspace.templatesPath + await fse.copy(path, templatesDir, {overwrite: true, errorOnExist: false}) + return this + }, + async setConfig(config: GeneratorConfig) { + await fse.writeJson(this.workspace.configPath, config, {spaces: 2}) + return this + }, +}