|
| 1 | +const fs = require('fs-extra') |
| 2 | +const semver = require('semver') |
| 3 | +const toolbox = require('gluegun/toolbox') |
| 4 | +const yaml = require('js-yaml') |
| 5 | +const { loadManifest } = require('./util/load-manifest') |
| 6 | +const { getGraphTsVersion } = require('./util/versions') |
| 7 | + |
| 8 | +// If any of the manifest apiVersions are 0.0.5, replace them with 0.0.6 |
| 9 | +module.exports = { |
| 10 | + name: 'Bump mapping apiVersion from 0.0.5 to 0.0.6', |
| 11 | + predicate: async ({ sourceDir, manifestFile }) => { |
| 12 | + // Obtain the graph-ts version, if possible |
| 13 | + let graphTsVersion |
| 14 | + try { |
| 15 | + graphTsVersion = await getGraphTsVersion(sourceDir) |
| 16 | + } catch (_) { |
| 17 | + // If we cannot obtain the version, return a hint that the graph-ts |
| 18 | + // hasn't been installed yet |
| 19 | + return 'graph-ts dependency not installed yet' |
| 20 | + } |
| 21 | + |
| 22 | + let manifest = loadManifest(manifestFile) |
| 23 | + return ( |
| 24 | + // Only migrate if the graph-ts version is >= 0.23.0... |
| 25 | + // Coerce needed because we may be dealing with an alpha version |
| 26 | + // and in the `semver` library this would not return true on equality. |
| 27 | + semver.gte(semver.coerce(graphTsVersion), '0.23.0') && |
| 28 | + // ...and we have a manifest with mapping > apiVersion = 0.0.5 |
| 29 | + manifest && |
| 30 | + typeof manifest === 'object' && |
| 31 | + Array.isArray(manifest.dataSources) && |
| 32 | + (manifest.dataSources.reduce( |
| 33 | + (hasOldMappings, dataSource) => |
| 34 | + hasOldMappings || |
| 35 | + (typeof dataSource === 'object' && |
| 36 | + dataSource.mapping && |
| 37 | + typeof dataSource.mapping === 'object' && |
| 38 | + dataSource.mapping.apiVersion === '0.0.5'), |
| 39 | + false, |
| 40 | + ) || |
| 41 | + (Array.isArray(manifest.templates) && |
| 42 | + manifest.templates.reduce( |
| 43 | + (hasOldMappings, template) => |
| 44 | + hasOldMappings || |
| 45 | + (typeof template === 'object' && |
| 46 | + template.mapping && |
| 47 | + typeof template.mapping === 'object' && |
| 48 | + template.mapping.apiVersion === '0.0.5'), |
| 49 | + false, |
| 50 | + ))) |
| 51 | + ) |
| 52 | + }, |
| 53 | + apply: async ({ manifestFile }) => { |
| 54 | + // Make sure we catch all variants; we could load the manifest |
| 55 | + // and replace the values in the data structures here; unfortunately |
| 56 | + // writing that back to the file messes with the formatting more than |
| 57 | + // we'd like; that's why for now, we use a simple patching approach |
| 58 | + await toolbox.patching.replace( |
| 59 | + manifestFile, |
| 60 | + new RegExp('apiVersion: 0.0.5', 'g'), |
| 61 | + 'apiVersion: 0.0.6', |
| 62 | + ) |
| 63 | + await toolbox.patching.replace( |
| 64 | + manifestFile, |
| 65 | + new RegExp("apiVersion: '0.0.5'", 'g'), |
| 66 | + "apiVersion: '0.0.6'", |
| 67 | + ) |
| 68 | + await toolbox.patching.replace( |
| 69 | + manifestFile, |
| 70 | + new RegExp('apiVersion: "0.0.5"', 'g'), |
| 71 | + 'apiVersion: "0.0.6"', |
| 72 | + ) |
| 73 | + }, |
| 74 | +} |
0 commit comments