Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 0b926a4

Browse files
reuveniyJason Kuhrt
authored andcommitted
feat: do not generate file if same content (#287)
closes #286
1 parent e4f765a commit 0b926a4

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

packages/graphqlgen/src/output.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export function outputDefinitionFilesNotFound(
1515
console.log(`❌ Some path to model definitions defined in your graphqlgen.yml were not found
1616
1717
${chalk.bold(
18-
'Step 1',
19-
)}: Make sure each of these model definitions point to an existing file
18+
'Step 1',
19+
)}: Make sure each of these model definitions point to an existing file
2020
2121
models:
2222
override:
@@ -42,8 +42,8 @@ export function outputWrongSyntaxFiles(
4242
console.log(`❌ Some model definitions defined in your graphqlgen.yml have syntax errors
4343
4444
${chalk.bold(
45-
'Step 1',
46-
)}: Make sure each of these model definitions follow the correct syntax
45+
'Step 1',
46+
)}: Make sure each of these model definitions follow the correct syntax
4747
4848
${chalk.cyan(
4949
`(Correct syntax: ${chalk.bold('<typeName>')}: ${chalk.bold(
@@ -74,8 +74,8 @@ export function outputInterfaceDefinitionsNotFound(
7474
console.log(`❌ Some model definitions defined in your graphqlgen.yml were not found
7575
7676
${chalk.bold(
77-
'Step 1',
78-
)}: Make sure each of these model definitions are defined in the following files
77+
'Step 1',
78+
)}: Make sure each of these model definitions are defined in the following files
7979
8080
models:
8181
override:
@@ -99,12 +99,12 @@ export function outputMissingModels(
9999
console.log(`❌ Some types from your application schema have model definitions that are not defined yet
100100
101101
${chalk.bold(
102-
'Step 1',
103-
)}: Copy/paste the model definitions below to your application
102+
'Step 1',
103+
)}: Copy/paste the model definitions below to your application
104104
105105
${missingModels
106-
.map(type => renderModelFromType(type, language, defaultName))
107-
.join(os.EOL)}
106+
.map(type => renderModelFromType(type, language, defaultName))
107+
.join(os.EOL)}
108108
109109
110110
${chalk.bold('Step 2')}: Reference the model definitions in your ${chalk.bold(
@@ -151,9 +151,9 @@ function renderTypescriptModelFromType(
151151
return `\
152152
export interface ${chalk.bold(name)} {
153153
${type.fields
154-
.filter(field => field.type.isScalar)
155-
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
156-
.join(os.EOL)}
154+
.filter(field => field.type.isScalar)
155+
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
156+
.join(os.EOL)}
157157
}`
158158
}
159159

@@ -165,7 +165,7 @@ function renderFlowModelFromType(
165165
return `\
166166
export interface ${chalk.bold(name)} {
167167
${type.fields
168-
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
169-
.join(',' + os.EOL)}
168+
.map(field => ` ${field.name}: ${graphQLToTypecriptFlowType(field.type)}`)
169+
.join(',' + os.EOL)}
170170
}`
171171
}

packages/graphqlgen/src/project-output.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as Path from 'path'
22
import * as FS from 'fs'
33
import * as mkdirp from 'mkdirp'
4-
import * as rimraf from 'rimraf'
54
import { GraphQLGenDefinition } from 'graphqlgen-json-schema'
65
import chalk from 'chalk'
76
import { CodeFileLike } from './types'
@@ -11,6 +10,18 @@ import {
1110
} from './path-helpers'
1211
import { replaceAll } from './utils'
1312

13+
function writeChangesOnly(filename: string, content: string) {
14+
if (
15+
!FS.existsSync(filename) ||
16+
FS.readFileSync(filename, { encoding: 'utf-8' }) !== content
17+
) {
18+
console.log(chalk.green(`Overriding ${filename} with new model`))
19+
FS.writeFileSync(filename, content)
20+
} else {
21+
console.log(chalk.gray(`File ${filename} is the same`))
22+
}
23+
}
24+
1425
/**
1526
* Bootstrap a graphqlgen config for the user to finish configuring.
1627
*/
@@ -65,7 +76,7 @@ const writeTypes = (types: string, config: GraphQLGenDefinition): void => {
6576
mkdirp.sync(Path.dirname(config.output))
6677

6778
try {
68-
FS.writeFileSync(config.output, types, { encoding: 'utf-8' })
79+
writeChangesOnly(config.output, types)
6980
} catch (e) {
7081
console.error(
7182
chalk.red(`Failed to write the file at ${config.output}, error: ${e}`),
@@ -93,13 +104,27 @@ const writeResolversScaffolding = (
93104

94105
const outputResolversDir = config['resolver-scaffolding'].output
95106

96-
rimraf.sync(outputResolversDir)
107+
const toBeCreatedFiles = resolvers.map(f =>
108+
Path.join(outputResolversDir, f.path),
109+
)
110+
111+
if (FS.existsSync(outputResolversDir)) {
112+
FS.readdirSync(outputResolversDir)
113+
.map(f => Path.join(outputResolversDir, f))
114+
.filter(f => !toBeCreatedFiles.includes(f))
115+
.forEach(f => {
116+
FS.unlinkSync(f)
117+
console.log(
118+
chalk.yellow(`Deleting file ${f} - model scaffold no long available`),
119+
)
120+
})
121+
}
97122

98123
resolvers.forEach(f => {
99124
const writePath = Path.join(outputResolversDir, f.path)
100125
mkdirp.sync(Path.dirname(writePath))
101126
try {
102-
FS.writeFileSync(
127+
writeChangesOnly(
103128
writePath,
104129
replaceAll(
105130
f.code,

0 commit comments

Comments
 (0)