-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
options.ts
177 lines (155 loc) · 4.31 KB
/
options.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { dirname, join, resolve } from 'path'
import type Vue from '@vitejs/plugin-vue'
import type Icons from 'unplugin-icons/vite'
import type Components from 'unplugin-vue-components/vite'
import type Markdown from 'vite-plugin-md'
import type WindiCSS from 'vite-plugin-windicss'
import type RemoteAssets from 'vite-plugin-remote-assets'
import type ServerRef from 'vite-plugin-vue-server-ref'
import type { ArgumentsType } from '@antfu/utils'
import { uniq } from '@antfu/utils'
import type { SlidevMarkdown } from '@slidev/types'
import _debug from 'debug'
import { parser } from './parser'
import { packageExists, resolveImportPath } from './utils'
import { getThemeMeta, promptForThemeInstallation, resolveThemeName } from './themes'
import { getAddons } from './addons'
const debug = _debug('slidev:options')
export interface SlidevEntryOptions {
/**
* Markdown entry
*
* @default 'slides.md'
*/
entry?: string
/**
* Theme id
*/
theme?: string
/**
* Remote password
*/
remote?: string
/**
* Root path
*
* @default process.cwd()
*/
userRoot?: string
}
export interface ResolvedSlidevOptions {
data: SlidevMarkdown
entry: string
userRoot: string
cliRoot: string
clientRoot: string
theme: string
themeRoots: string[]
addonRoots: string[]
roots: string[]
mode: 'dev' | 'build'
remote?: string
}
export interface SlidevPluginOptions extends SlidevEntryOptions {
vue?: ArgumentsType<typeof Vue>[0]
markdown?: ArgumentsType<typeof Markdown>[0]
components?: ArgumentsType<typeof Components>[0]
windicss?: ArgumentsType<typeof WindiCSS>[0]
icons?: ArgumentsType<typeof Icons>[0]
remoteAssets?: ArgumentsType<typeof RemoteAssets>[0]
serverRef?: ArgumentsType<typeof ServerRef>[0]
}
export interface SlidevServerOptions {
onDataReload?: (newData: SlidevMarkdown, data: SlidevMarkdown) => void
}
export function getClientRoot() {
return dirname(resolveImportPath('@slidev/client/package.json', true))
}
export function getCLIRoot() {
return resolve(__dirname, '..')
}
export function isPath(name: string) {
return name.startsWith('/') || /^\.\.?[\/\\]/.test(name)
}
export function getThemeRoots(name: string, entry: string) {
if (!name)
return []
// TODO: handle theme inherit
return [getRoot(name, entry)]
}
export function getAddonRoots(addons: string[], entry: string) {
if (addons.length === 0)
return []
return addons.map(name => getRoot(name, entry))
}
export function getRoot(name: string, entry: string) {
if (isPath(name))
return resolve(dirname(entry), name)
return dirname(resolveImportPath(`${name}/package.json`, true))
}
export function getUserRoot(options: SlidevEntryOptions) {
const { entry: rawEntry = 'slides.md', userRoot = process.cwd() } = options
const fullEntry = resolve(userRoot, rawEntry)
return { entry: fullEntry, userRoot: dirname(fullEntry) }
}
export async function resolveOptions(
options: SlidevEntryOptions,
mode: ResolvedSlidevOptions['mode'],
promptForInstallation = true,
): Promise<ResolvedSlidevOptions> {
const { remote } = options
const {
entry,
userRoot,
} = getUserRoot(options)
const data = await parser.load(entry)
const theme = resolveThemeName(options.theme || data.config.theme)
if (promptForInstallation) {
if (await promptForThemeInstallation(theme) === false)
process.exit(1)
}
else {
if (!packageExists(theme)) {
console.error(`Theme "${theme}" not found, have you installed it?`)
process.exit(1)
}
}
const clientRoot = getClientRoot()
const cliRoot = getCLIRoot()
const themeRoots = getThemeRoots(theme, entry)
const addons = await getAddons(userRoot, data.config)
const addonRoots = getAddonRoots(addons, entry)
const roots = uniq([clientRoot, ...themeRoots, userRoot])
if (themeRoots.length) {
const themeMeta = await getThemeMeta(theme, join(themeRoots[0], 'package.json'))
data.themeMeta = themeMeta
if (themeMeta)
data.config = parser.resolveConfig(data.headmatter, themeMeta)
}
debug({
config: data.config,
mode,
entry,
theme,
userRoot,
clientRoot,
cliRoot,
themeRoots,
addonRoots,
roots,
remote,
})
return {
data,
mode,
entry,
theme,
userRoot,
clientRoot,
cliRoot,
themeRoots,
addonRoots,
roots,
remote,
}
}