-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathcache-graphql.ts
More file actions
256 lines (221 loc) · 7.59 KB
/
cache-graphql.ts
File metadata and controls
256 lines (221 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { saveFile } from '../utils/file'
import { Args, Command, Flags } from '@oclif/core'
import chalk from 'chalk'
import { getBasePath, withBasePath } from '../utils/directory'
import { logger } from '../utils/logger'
import graphql from 'graphql'
import path from 'path'
import fsExtra from 'fs-extra'
import prettier from 'prettier'
const { Kind, OperationTypeNode, parse: parseGraphql } = graphql
const persistedDocumentsName = 'persisted-documents.json'
const configFileName = 'discovery.config.default.js'
export default class CacheGraphql extends Command {
static flags = {
queries: Flags.string({
name: 'queries',
description: 'The path to locate persisted-document file.',
}),
config: Flags.string({
name: 'config',
description: 'The path where the discovery.config is located',
}),
}
static args = {
store: Args.string({
name: 'store',
description:
'The path where the FastStore being built is or the persisted-document path. Defaults to cwd.',
}),
}
async run() {
const { args, flags } = await this.parse(CacheGraphql)
const rootPath = getBasePath()
const argPath =
(args?.store && path.resolve(rootPath, args.store)) || rootPath
const { tmpDir } = withBasePath(argPath)
const configPath =
this.getConfigFile(flags.config && path.resolve(argPath, flags.config)) ||
this.getConfigFile(tmpDir) ||
this.getConfigFile(argPath)
const persistedDocumentsPath =
this.getPersistedDocument(
(flags?.queries && path.resolve(argPath, flags?.queries)) || argPath
) || this.getPersistedDocument(tmpDir)
if (!configPath) {
return this.errorFileNotFound(
configFileName,
`\n ${tmpDir}\n ${argPath}`
)
}
if (!persistedDocumentsPath) {
return this.errorFileNotFound(
persistedDocumentsName,
flags?.queries ?? argPath
)
}
const saveConfigFile = saveFile(configPath)
if (fsExtra.pathExistsSync(tmpDir))
logger.info(`${chalk.blue('[Info]')} - .faststore Path at: ${tmpDir}`)
logger.info(`${chalk.blue('[Info]')} - Config file location: ${configPath}`)
logger.info(
`${chalk.blue('[Info]')} - Persisted documents at: ${persistedDocumentsPath}`
)
const { default: persistedDocuments } = await import(
persistedDocumentsPath,
{ with: { type: 'json' } }
)
const cachedQueries = getQueries(persistedDocuments)
const source = fsExtra.readFileSync(configPath, 'utf8')
const patched = updateCachedOperations(source, cachedQueries)
const prettierConfig = await prettier.resolveConfig(configPath)
const formatted = await prettier.format(patched, {
...prettierConfig,
filepath: configPath,
})
saveConfigFile(formatted)
logger.info(
`${chalk.green('[Success]')} - GraphQL queries cached with success: 🎉
Queries: ${cachedQueries.join(', ')}`
)
}
getPersistedDocument(rootPath?: string) {
return this.getFile(persistedDocumentsName, [
'@generated',
persistedDocumentsName,
])(rootPath)
}
getConfigFile(rootPath?: string) {
return this.getFile(configFileName, [configFileName])(rootPath)
}
getFile(fileName: string, pathFromRoot?: string | Array<string>) {
return (rootPath?: string) => {
switch (true) {
case !rootPath:
return
case rootPath?.endsWith(fileName) && fsExtra.existsSync(rootPath):
return rootPath
case fsExtra.existsSync(path.join(rootPath ?? '', fileName)):
return path.join(rootPath, fileName)
default:
if (!pathFromRoot) return
const filePath = path.join(
rootPath,
...(Array.isArray(pathFromRoot) ? pathFromRoot : [pathFromRoot])
)
if (fsExtra.existsSync(filePath)) {
return filePath
}
return
}
}
}
errorFileNotFound(fileName: string, rootDir: string) {
logger.error(
`${chalk.red('[Error]')} - Couldn't find ${fileName} at ${rootDir}`
)
process.exit(1)
}
}
const getQueries = (persistedDocuments: Record<string, string>) => {
const operationNames: Array<string> = []
for (const operation of Object.values(persistedDocuments)) {
let currentNames: Array<string> = []
const operationAST = parseGraphql(operation)
const hasMutationDefinition = operationAST.definitions.some(
(def) =>
def.kind === Kind.OPERATION_DEFINITION &&
def.operation === OperationTypeNode.MUTATION
)
if (hasMutationDefinition) continue
operationAST.definitions.forEach((definition) => {
if (
definition.kind === Kind.OPERATION_DEFINITION &&
definition.operation === OperationTypeNode.QUERY &&
definition.name?.kind === Kind.NAME &&
!!definition.name?.value
) {
currentNames.push(definition.name.value)
return
}
})
if (currentNames.length) {
operationNames.push(...currentNames)
currentNames = []
}
}
return operationNames
}
/**
* Updates only the `experimental.cachedOperations` property inside the
* `module.exports = { ... }` object literal of a discovery config file.
*
* Works as a targeted string edit (no AST): it locates the `experimental: { ... }`
* block by counting curly braces and either replaces an existing
* `cachedOperations: [...]` array or inserts a new one before the closing `}`.
* The rest of the file (comments, `process.env.*`, formatting, ...) is left
* untouched. A final pass through prettier normalizes the inserted snippet.
*/
function updateCachedOperations(
source: string,
cachedQueries: string[]
): string {
const block = findExperimentalBlock(source)
if (!block) {
throw new Error(
`Couldn't find \`experimental: { ... }\` block in ${configFileName}`
)
}
const arraySnippet = `[\n${cachedQueries.map((q) => ` '${q}',`).join('\n')}\n ]`
const propSnippet = `cachedOperations: ${arraySnippet},`
const inner = source.slice(block.innerStart, block.innerEnd)
const existing = findCachedOperationsRange(inner)
if (existing) {
const absStart = block.innerStart + existing.start
const absEnd = block.innerStart + existing.end
return source.slice(0, absStart) + propSnippet + source.slice(absEnd)
}
const insertAt = block.innerEnd
const head = source.slice(0, insertAt).replace(/\s*$/, '')
const tail = source.slice(insertAt)
return `${head}\n ${propSnippet}\n${tail}`
}
/** Find the `experimental: { ... }` block, returning the inner range. */
function findExperimentalBlock(source: string) {
const re = /(^|\n)[ \t]*experimental\s*:\s*\{/
const match = re.exec(source)
if (!match) return null
const innerStart = match.index + match[0].length
let depth = 1
for (let i = innerStart; i < source.length; i++) {
const ch = source[i]
if (ch === '{') depth++
else if (ch === '}') {
depth--
if (depth === 0) return { innerStart, innerEnd: i }
}
}
return null
}
/** Find the `cachedOperations: [...]` property range inside a string. */
function findCachedOperationsRange(inner: string) {
const re = /(^|\n)[ \t]*cachedOperations\s*:\s*\[/
const match = re.exec(inner)
if (!match) return null
const start = match.index + (match[1] === '\n' ? 1 : 0)
const arrayStart = match.index + match[0].length
let depth = 1
for (let i = arrayStart; i < inner.length; i++) {
const ch = inner[i]
if (ch === '[') depth++
else if (ch === ']') {
depth--
if (depth === 0) {
let end = i + 1
if (inner[end] === ',') end++
return { start, end }
}
}
}
return null
}