/
load.ts
93 lines (81 loc) · 1.99 KB
/
load.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import fs from 'fs'
import JoyCon from 'joycon'
import path from 'path'
import { bundleRequire } from 'bundle-require'
import { defineConfig } from './'
import { jsoncParse } from './utils'
const joycon = new JoyCon()
const loadJson = async (filepath: string) => {
try {
return jsoncParse(await fs.promises.readFile(filepath, 'utf8'))
} catch (error) {
if (error instanceof Error) {
throw new Error(
`Failed to parse ${path.relative(process.cwd(), filepath)}: ${
error.message
}`
)
} else {
throw error
}
}
}
const jsonLoader = {
test: /\.json$/,
load(filepath: string) {
return loadJson(filepath)
},
}
joycon.addLoader(jsonLoader)
export async function loadTsupConfig(
cwd: string
): Promise<{ path?: string; data?: ReturnType<typeof defineConfig> }> {
const configJoycon = new JoyCon()
const configPath = await configJoycon.resolve({
files: [
'tsup.config.ts',
'tsup.config.js',
'tsup.config.cjs',
'tsup.config.mjs',
'tsup.config.json',
'package.json',
],
cwd,
stopDir: path.parse(cwd).root,
packageKey: 'tsup',
})
if (configPath) {
if (configPath.endsWith('.json')) {
let data = await loadJson(configPath)
if (configPath.endsWith('package.json')) {
data = data.tsup
}
if (data) {
return { path: configPath, data }
}
return {}
}
const config = await bundleRequire({
filepath: configPath,
})
return {
path: configPath,
data: config.mod.tsup || config.mod.default || config.mod,
}
}
return {}
}
export async function loadPkg(cwd: string) {
const { data } = await joycon.load(['package.json'], cwd, path.dirname(cwd))
return data || {}
}
export async function getDeps(cwd: string) {
const data = await loadPkg(cwd)
const deps = Array.from(
new Set([
...Object.keys(data.dependencies || {}),
...Object.keys(data.peerDependencies || {}),
])
)
return deps
}