diff --git a/README.md b/README.md index 2790eb71..c4272415 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,12 @@ with temporal dead zone when generating merged file. Type: `boolean` Default: `false` +#### `purgeOutput` + +Delete all files in `output` folder +Type: `boolean` +Default: `false` + #### `types_*` (deprecated)
diff --git a/prisma/schema.prisma b/prisma/schema.prisma index da811e85..aa267121 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,6 +16,7 @@ generator nestgraphql { reExport = None emitSingle = false emitCompiled = false + purgeOutput = false } model User { diff --git a/src/generate.ts b/src/generate.ts index c2fb1525..f19191ae 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -1,7 +1,6 @@ import { GeneratorOptions } from '@prisma/generator-helper'; import { ok } from 'assert'; import AwaitEventEmitter from 'await-event-emitter'; -import { existsSync } from 'fs'; import { mapKeys } from 'lodash'; import { Project, QuoteKind } from 'ts-morph'; @@ -15,12 +14,13 @@ import { modelData } from './handlers/model-data'; import { modelOutputType } from './handlers/model-output-type'; import { noAtomicOperations } from './handlers/no-atomic-operations'; import { outputType } from './handlers/output-type'; +import { purgeOutput } from './handlers/purge-output'; import { ReExport, reExport } from './handlers/re-export'; import { registerEnum } from './handlers/register-enum'; import { typeNames } from './handlers/type-names'; import { warning } from './handlers/warning'; import { createConfig } from './helpers/create-config'; -import { factoryGetSourceFile } from './helpers/factory-get-souce-file'; +import { factoryGetSourceFile } from './helpers/factory-get-source-file'; import { createGetModelName } from './helpers/get-model-name'; import { DMMF, EventArguments, Field, FieldSettings, Model, OutputType } from './types'; @@ -76,6 +76,7 @@ export async function generate( config.noAtomicOperations && noAtomicOperations(eventEmitter); config.reExport !== ReExport.None && reExport(eventEmitter); config.emitSingle && emitSingle(eventEmitter); + config.purgeOutput && purgeOutput(eventEmitter); const models = new Map(); const modelNames: string[] = []; @@ -108,8 +109,6 @@ export async function generate( getModelName: createGetModelName(modelNames), }; - // console.dir(prismaClientDmmf.schema.outputObjectTypes, { depth: 4 }); - if (connectCallback) { await connectCallback(eventEmitter, eventArguments); } diff --git a/src/handlers/purge-output.ts b/src/handlers/purge-output.ts new file mode 100644 index 00000000..bac1669a --- /dev/null +++ b/src/handlers/purge-output.ts @@ -0,0 +1,34 @@ +import AwaitEventEmitter from 'await-event-emitter'; +import { promises as fs } from 'fs'; + +import { EventArguments } from '../types'; + +export function purgeOutput(emitter: AwaitEventEmitter) { + emitter.on('Begin', begin); + emitter.on('End', end); +} + +function begin({ project, output }: EventArguments) { + const sourceFiles = project.getDirectory(output)?.getDescendantSourceFiles(); + + if (sourceFiles) { + for (const sourceFile of sourceFiles) { + sourceFile.delete(); + } + } +} + +async function end({ project, output }: EventArguments) { + const directories = project + .getDirectory(output) + ?.getDescendantDirectories() + .filter(directory => directory.getSourceFiles().length === 0) + .map(directory => directory.getPath()); + + for (const directory of directories || []) { + try { + await fs.rmdir(directory); + // eslint-disable-next-line no-empty + } catch {} + } +} diff --git a/src/helpers/create-config.ts b/src/helpers/create-config.ts index 3ee7f75d..0c0ab39f 100644 --- a/src/helpers/create-config.ts +++ b/src/helpers/create-config.ts @@ -82,6 +82,7 @@ export function createConfig(data: Record) { emitCompiled: toBoolean(config.emitCompiled), $warnings, fields, + purgeOutput: toBoolean(config.purgeOutput), }; } diff --git a/src/helpers/factory-get-souce-file.ts b/src/helpers/factory-get-source-file.ts similarity index 100% rename from src/helpers/factory-get-souce-file.ts rename to src/helpers/factory-get-source-file.ts