-
Notifications
You must be signed in to change notification settings - Fork 196
/
mapping_api_version_0_0_2.js
61 lines (59 loc) · 2.09 KB
/
mapping_api_version_0_0_2.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
57
58
59
60
61
const fs = require('fs-extra')
const semver = require('semver')
const toolbox = require('gluegun/toolbox')
const yaml = require('js-yaml')
const { getGraphTsVersion } = require('./util/versions')
// If any of the manifest apiVersions are 0.0.2, replace them with 0.0.3
module.exports = {
name: 'Bump mapping apiVersion from 0.0.2 to 0.0.3',
predicate: async ({ sourceDir, manifestFile }) => {
// Obtain the graph-ts version, if possible
let graphTsVersion
try {
graphTsVersion = getGraphTsVersion(sourceDir)
} catch (_) {
// If we cannot obtain the version, return a hint that the graph-ts
// hasn't been installed yet
return 'graph-ts dependency not installed yet'
}
let manifest = yaml.safeLoad(fs.readFileSync(manifestFile, 'utf-8'))
return (
// Only migrate if the graph-ts version is > 0.5.1...
semver.gt(graphTsVersion, '0.12.0') &&
// ...and we have a manifest with mapping > apiVersion = 0.0.2
manifest &&
typeof manifest === 'object' &&
Array.isArray(manifest.dataSources) &&
manifest.dataSources.reduce(
(hasOldMappings, dataSource) =>
hasOldMappings ||
(typeof dataSource === 'object' &&
dataSource.mapping &&
typeof dataSource.mapping === 'object' &&
dataSource.mapping.apiVersion === '0.0.2'),
false,
)
)
},
apply: async ({ manifestFile }) => {
// Make sure we catch all variants; we could load the manifest
// and replace the values in the data structures here; unfortunately
// writing that back to the file messes with the formatting more than
// we'd like; that's why for now, we use a simple patching approach
await toolbox.patching.replace(
manifestFile,
new RegExp('apiVersion: 0.0.2', 'g'),
'apiVersion: 0.0.3',
)
await toolbox.patching.replace(
manifestFile,
new RegExp("apiVersion: '0.0.2'", 'g'),
"apiVersion: '0.0.3'",
)
await toolbox.patching.replace(
manifestFile,
new RegExp('apiVersion: "0.0.2"', 'g'),
'apiVersion: "0.0.3"',
)
},
}