-
Notifications
You must be signed in to change notification settings - Fork 388
/
writeRequiredUIVersion.ts
65 lines (53 loc) · 1.82 KB
/
writeRequiredUIVersion.ts
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
// // This overwrites the compiled ./lib/requiredSanityUiVersion.js with a the
// // actual version we currently depend on in the `sanity` module
// import fs from 'fs/promises'
// import pkg from '../package.json'
// const template = (version: string) => `exports.REQUIRED_UI_VERSION = '${version}'`
// let builtFile
// try {
// builtFile = require.resolve('../lib/cjs/requiredSanityUiVersion.js')
// } catch (error) {
// // eslint-disable-next-line no-console
// console.error(
// 'Error: Unable to resolve "requiredSanityUiVersion.js" in ./lib. Please make sure the project has been successfully built.'
// )
// process.exit(1)
// }
// let version = pkg.dependencies['@sanity/ui']
// if (typeof version === 'string' && version.startsWith('^')) {
// version = version.slice(1)
// }
// fs.writeFile(builtFile, template(version || 'latest'))
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */
/* eslint-disable no-process-exit */
/* eslint-disable no-sync */
import fs from 'fs'
import globby from 'globby'
import path from 'path'
import semver from 'semver'
import pkg from '../package.json'
const rawVersion = pkg.dependencies['@sanity/ui']
const v = semver.parse(
rawVersion.startsWith('^') || rawVersion.startsWith('~') ? rawVersion.slice(1) : rawVersion,
)
if (!v) {
console.log(`the version of @sanity/ui is invalid: ${rawVersion}`)
process.exit(1)
}
const version = `${v.major}.${v.minor}.${v.patch}`
globby([
path.resolve(__dirname, '../lib/**/*.js'),
path.resolve(__dirname, '../lib/**/*.mjs'),
]).then((files) => {
for (const file of files) {
const buf = fs.readFileSync(file, 'utf8')
fs.writeFileSync(
file,
buf
.toString()
.replace('REQUIRED_UI_VERSION="0.0.0-development"', `REQUIRED_UI_VERSION="${version}"`),
'utf8',
)
}
})