Skip to content

Commit

Permalink
feat(core): temp workspace provider implementation.
Browse files Browse the repository at this point in the history
Signed-off-by: Braden Mars <bradenmars@bradenmars.me>
  • Loading branch information
BradenM committed Feb 12, 2023
1 parent 6268f33 commit be6c90c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/openapi-generator-clients/src/workspace.ts
Original file line number Diff line number Diff line change
@@ -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<Workspace>({
rootPath: '',
templatesPath: '',
configPath: '',
outputPath: '',
})
export interface WorkspaceRecord extends RecordOf<Workspace> {}

export const temporaryWorkspaceProvider: WorkspaceProvider<WorkspaceRecord> = {
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
},
}

0 comments on commit be6c90c

Please sign in to comment.