-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): temp workspace provider implementation.
Signed-off-by: Braden Mars <bradenmars@bradenmars.me>
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} |