Skip to content

Commit

Permalink
feat: Skip write stage for files with no changes
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Aug 15, 2020
1 parent 721a064 commit ecc2fb8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 41 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"get-relative-path": "^1.0.2",
"pupa": "^2.0.1",
"to-kebab": "^1.0.7",
"ts-morph": "^7.2.0"
"ts-morph": "^7.3.0",
"typescript-equals": "^1.0.0"
},
"peerDependencies": {
"@prisma/client": "2"
Expand All @@ -64,14 +65,14 @@
"@prisma/client": "^2.4.1",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/mocha": "^8.0.1",
"@types/mocha": "^8.0.2",
"@types/node": "^14.0.27",
"@typescript-eslint/eslint-plugin": "^3.9.0",
"@typescript-eslint/parser": "^3.9.0",
"c8": "^7.3.0",
"eslint": "^7.6.0",
"eslint": "^7.7.0",
"eslint-import-resolver-node": "^0.3.4",
"eslint-plugin-etc": "0.0.1-beta.38",
"eslint-plugin-etc": "0.0.1-beta.40",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-only-warn": "^1.0.2",
"eslint-plugin-prettier": "^3.1.4",
Expand All @@ -80,7 +81,7 @@
"eslint-plugin-simple-import-sort": "^5.0.3",
"eslint-plugin-sonarjs": "^0.5.0",
"eslint-plugin-sort-class-members": "^1.8.0",
"eslint-plugin-total-functions": "^1.40.0",
"eslint-plugin-total-functions": "^1.42.1",
"eslint-plugin-unicorn": "^21.0.0",
"eslint-plugin-wix-editor": "^3.2.0",
"git-branch-is": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ generator nestgraphql {
model User {
id String @id @default(cuid())
email String @unique
/// User's name
name String @unique
password String
bio String?
Expand Down
2 changes: 1 addition & 1 deletion src/@generated/user/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class User {

@Field(() => String, {
nullable: false,
description: undefined,
description: "User's name",
})
name!: string;

Expand Down
4 changes: 2 additions & 2 deletions src/generate-enum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ describe('generate enum', () => {
registerEnumType(Role, { name: 'Role' })
`,
});
assert.equal(
assert.strictEqual(
sourceText.match(/registerEnumType/g)?.length,
2,
'Only import and call once should exists',
);
assert.equal(sourceText.match(/enum Role/g)?.length, 1);
assert.strictEqual(sourceText.match(/enum Role/g)?.length, 1);
});
});
10 changes: 1 addition & 9 deletions src/generate-model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { DMMF as PrismaDMMF } from '@prisma/client/runtime/dmmf-types';
import assert from 'assert';
import {
ClassDeclaration,
Node,
ObjectLiteralExpression,
PropertyAssignment,
SourceFile,
StructureKind,
} from 'ts-morph';
import { SourceFile } from 'ts-morph';

import { generateClass } from './generate-class';
import { generateProperty } from './generate-property';
Expand Down
4 changes: 2 additions & 2 deletions src/generate-property.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DMMF as PrismaDMMF } from '@prisma/client/runtime/dmmf-types';
import assert from 'assert';
import { ClassDeclaration, Node, ObjectLiteralExpression, SourceFile } from 'ts-morph';

Expand All @@ -23,6 +22,7 @@ type GeneratePropertyArgs = {
isId?: boolean;
default?: any;
isReadOnly?: boolean;
documentation?: string;
};
};

Expand Down Expand Up @@ -113,6 +113,6 @@ export function generateProperty(args: GeneratePropertyArgs) {
setObjectProperty({
expression: optionsExpression,
name: 'description',
value: (field as any).documentation,
value: field.documentation,
});
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { generatorHandler } from '@prisma/generator-helper';

import { generate } from './generate';
import { remove } from './remove';
import { update } from './update';
// import { remove } from './remove';

generatorHandler({
async onGenerate(options) {
const project = await generate(options);
// await remove(project, options);
await remove(project, options);
await update(project, options);
},
onManifest() {
Expand Down
25 changes: 5 additions & 20 deletions src/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,23 @@ import { GeneratorOptions } from '@prisma/generator-helper';
import assert from 'assert';
import { existsSync, promises as fs } from 'fs';
import { resolve } from 'path';
import { Project, SourceFile } from 'ts-morph';
import { Project } from 'ts-morph';
import { equals } from 'typescript-equals';

/**
* Remove unchanged files
*/
export async function remove(project: Project, options: GeneratorOptions) {
assert(options.generator.output);
let testSourceFile: SourceFile | undefined;
for (const sourceFile of project.getSourceFiles()) {
const filePath = sourceFile.getFilePath();
const outfile = resolve(options.generator.output, `.${filePath}`);
if (existsSync(outfile)) {
const testText = await fs.readFile(outfile, { encoding: 'utf8' });
testSourceFile = project.createSourceFile('0.ts', testText, {
overwrite: true,
});
testSourceFile.formatText({ ensureNewLineAtEndOfFile: true });
sourceFile.formatText({ ensureNewLineAtEndOfFile: true });
if (testSourceFile.getText() === sourceFile.getText()) {
const outfileText = await fs.readFile(outfile, { encoding: 'utf8' });
const sourceFileText = sourceFile.getText();
if (equals(outfileText, sourceFileText)) {
sourceFile.deleteImmediatelySync();
project.removeSourceFile(sourceFile);
project.removeSourceFile(testSourceFile);
console.log('length', project.getSourceFiles().length);
} else {
if (outfile.includes('tag.model.ts')) {
console.log('testSourceFile.getText()', testSourceFile.getText());
console.log('sourceFile.getText()', sourceFile.getText());
}
}
}
}
if (testSourceFile) {
testSourceFile.deleteImmediatelySync();
}
}

0 comments on commit ecc2fb8

Please sign in to comment.