forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
118 lines (107 loc) · 4.17 KB
/
gulpfile.js
File metadata and controls
118 lines (107 loc) · 4.17 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
// @ts-check
const { generateNamespace } = require('@gql2ts/from-schema')
const { DEFAULT_OPTIONS, DEFAULT_TYPE_MAP } = require('@gql2ts/language-typescript')
const { buildSchema, graphql, introspectionQuery } = require('graphql')
const gulp = require('gulp')
const { compile: compileJSONSchema } = require('json-schema-to-typescript')
const mkdirp = require('mkdirp-promise')
const { readFile, writeFile } = require('mz/fs')
const path = require('path')
const { format, resolveConfig } = require('prettier')
const GRAPHQL_SCHEMA_PATH = path.join(__dirname, '../cmd/frontend/graphqlbackend/schema.graphql')
/**
* Generates the TypeScript types for the GraphQL schema
*
* @returns {Promise<void>}
*/
async function graphQLTypes() {
const schemaStr = await readFile(GRAPHQL_SCHEMA_PATH, 'utf8')
const schema = buildSchema(schemaStr)
const result = /** @type {{ data: import('graphql').IntrospectionQuery }} */ (await graphql(
schema,
introspectionQuery
))
const formatOptions = await resolveConfig(__dirname, { config: __dirname + '/../prettier.config.js' })
const typings =
'export type ID = string\n' +
'export type GitObjectID = string\n' +
'export type DateTime = string\n' +
'export type JSONCString = string\n' +
'\n' +
generateNamespace(
'',
result,
{
typeMap: {
...DEFAULT_TYPE_MAP,
ID: 'ID',
GitObjectID: 'GitObjectID',
DateTime: 'DateTime',
JSONCString: 'JSONCString',
},
},
{
generateNamespace: (name, interfaces) => interfaces,
interfaceBuilder: (name, body) => 'export ' + DEFAULT_OPTIONS.interfaceBuilder(name, body),
enumTypeBuilder: (name, values) =>
'export ' + DEFAULT_OPTIONS.enumTypeBuilder(name, values).replace(/^const enum/, 'enum'),
typeBuilder: (name, body) => 'export ' + DEFAULT_OPTIONS.typeBuilder(name, body),
wrapList: type => `${type}[]`,
postProcessor: code => format(code, { ...formatOptions, parser: 'typescript' }),
}
)
await writeFile(__dirname + '/src/graphql/schema.ts', typings)
}
async function watchGraphQLTypes() {
await new Promise((resolve, reject) => {
gulp.watch(GRAPHQL_SCHEMA_PATH, graphQLTypes).on('error', reject)
})
}
/**
* Allow json-schema-ref-parser to resolve the v7 draft of JSON Schema
* using a local copy of the spec, enabling developers to run/develop Sourcegraph offline
*/
const draftV7resolver = {
order: 1,
read: () => readFile(path.join(__dirname, '../schema/json-schema-draft-07.schema.json')),
canRead: file => file.url === 'http://json-schema.org/draft-07/schema',
}
/**
* Generates the TypeScript types for the JSON schemas.
*
* @returns {Promise<void>}
*/
async function schema() {
const outputDir = path.join(__dirname, '..', 'web', 'src', 'schema')
await mkdirp(outputDir)
const schemaDir = path.join(__dirname, '..', 'schema')
await Promise.all(
['json-schema-draft-07', 'settings', 'site'].map(async file => {
let schema = await readFile(path.join(schemaDir, `${file}.schema.json`), 'utf8')
// HACK: Rewrite absolute $refs to be relative. They need to be absolute for Monaco to resolve them
// when the schema is in a oneOf (to be merged with extension schemas).
schema = schema.replace(
/https:\/\/sourcegraph\.com\/v1\/settings\.schema\.json#\/definitions\//g,
'#/definitions/'
)
const types = await compileJSONSchema(JSON.parse(schema), 'settings.schema', {
cwd: schemaDir,
$refOptions: {
resolve: /** @type {import('json-schema-ref-parser').Options['resolve']} */ ({
draftV7resolver,
// there should be no reason to make network calls during this process,
// and if there are we've broken env for offline devs/increased dev startup time
http: false,
}),
},
})
await writeFile(path.join(outputDir, `${file}.schema.d.ts`), types)
})
)
}
async function watchSchema() {
await new Promise((_resolve, reject) => {
gulp.watch(__dirname + '/../schema/*.schema.json', schema).on('error', reject)
})
}
module.exports = { watchSchema, schema, graphQLTypes, watchGraphQLTypes }