forked from redwoodjs/redwood
/
graphiql.js
56 lines (53 loc) · 1.48 KB
/
graphiql.js
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
import { Listr } from 'listr2'
import {
existsAnyExtensionSync,
deleteFile,
readFile,
writeFile,
getGraphqlPath,
} from '../../../lib'
import c from '../../../lib/colors'
import { getOutputPath } from '../../setup/graphiql/graphiql'
const removeGraphiqlFromGraphqlHandler = () => {
const graphqlPath = getGraphqlPath()
let content = readFile(graphqlPath).toString()
const [_, hasHeaderImport] =
content.match(/(import .* from 'src\/lib\/generateGraphiQLHeader.*')/s) ||
[]
if (hasHeaderImport) {
// remove header import statement
content = content.replace(
`\n\nimport generateGraphiQLHeader from 'src/lib/generateGraphiQLHeader'`,
''
)
// remove object from handler
content = content.replace(`generateGraphiQLHeader,\n`, '')
}
writeFile(graphqlPath, content, {
overwriteExisting: true,
})
}
export const command = 'graphiql'
export const description = 'Destroy graphiql header'
export const handler = () => {
const path = getOutputPath()
const tasks = new Listr(
[
{
title: 'Destroying graphiql files...',
skip: () => !existsAnyExtensionSync(path) && `File doesn't exist`,
task: () => deleteFile(path),
},
{
title: 'Removing graphiql import from createGraphQLHandler',
task: removeGraphiqlFromGraphqlHandler,
},
],
{ rendererOptions: { collapse: false }, exitOnError: true }
)
try {
tasks.run()
} catch (e) {
console.log(c.error(e.message))
}
}