Skip to content

Commit

Permalink
feat(configuration): Allow purge output folder
Browse files Browse the repository at this point in the history
closes: unlight#7
  • Loading branch information
unlight committed Apr 28, 2021
1 parent 7e2d802 commit a360869
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<details>
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ generator nestgraphql {
reExport = None
emitSingle = false
emitCompiled = false
purgeOutput = false
}

model User {
Expand Down
7 changes: 3 additions & 4 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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';

Expand Down Expand Up @@ -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<string, Model>();
const modelNames: string[] = [];
Expand Down Expand Up @@ -108,8 +109,6 @@ export async function generate(
getModelName: createGetModelName(modelNames),
};

// console.dir(prismaClientDmmf.schema.outputObjectTypes, { depth: 4 });

if (connectCallback) {
await connectCallback(eventEmitter, eventArguments);
}
Expand Down
34 changes: 34 additions & 0 deletions src/handlers/purge-output.ts
Original file line number Diff line number Diff line change
@@ -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 {}
}
}
1 change: 1 addition & 0 deletions src/helpers/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function createConfig(data: Record<string, string | undefined>) {
emitCompiled: toBoolean(config.emitCompiled),
$warnings,
fields,
purgeOutput: toBoolean(config.purgeOutput),
};
}

Expand Down
File renamed without changes.

0 comments on commit a360869

Please sign in to comment.