-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlanguage-tools.ts
75 lines (70 loc) · 2.09 KB
/
language-tools.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
66
67
68
69
70
71
72
73
74
75
import path from 'node:path'
import fs from 'node:fs'
import { runInRepo } from '../utils.ts'
import { RunOptions } from '../types.ts'
import { REGISTRY_ADDRESS } from '../registry.ts'
import YAML from 'yaml'
export async function test(options: RunOptions) {
await runInRepo({
...options,
repo: 'vuejs/language-tools',
branch: 'master',
beforeBuild: `pnpm dedupe --registry=${REGISTRY_ADDRESS}`,
build: 'build',
test: 'test',
overrideVueVersion: '@^3.5.2',
patchFiles: {
'package.json': (content) => {
const pkg = JSON.parse(content)
const versions = resolveTypeScriptVersion(options)
if (versions.typescript)
pkg.devDependencies.typescript = versions.typescript
return JSON.stringify(pkg, null, 2)
},
'test-workspace/package.json': (content) => {
const pkg = JSON.parse(content)
const versions = resolveTypeScriptVersion(options, 'test-workspace', [
'typescript-stable',
'typescript-next',
])
if (versions['typescript-stable'])
pkg.devDependencies['typescript-stable'] =
versions['typescript-stable']
if (versions['typescript-next'])
pkg.devDependencies['typescript-next'] = versions['typescript-next']
return JSON.stringify(pkg, null, 2)
},
},
})
}
function resolveTypeScriptVersion(
options: RunOptions,
importer = '.',
pkgNames = ['typescript'],
): Record<string, string> {
const data = resolveLockFile(options)
if (!data) return {}
return pkgNames.reduce(
(acc, pkgName) => {
const version =
data.importers[importer]?.devDependencies?.[pkgName]?.version
if (version) {
acc[pkgName] = `npm:typescript@${version.split('@').pop()}`
}
return acc
},
{} as Record<string, string>,
)
}
let lockFileCache: any
function resolveLockFile(options: RunOptions, dirName = 'language-tools'): any {
if (lockFileCache) return lockFileCache
const filePath = path.resolve(options.workspace, dirName, 'pnpm-lock.yaml')
try {
const content = fs.readFileSync(filePath, 'utf-8')
return (lockFileCache = YAML.parse(content))
} catch (error) {
console.error('Error reading lockfile:', error)
return null
}
}