@@ -8,22 +8,24 @@ import fs from 'fs-extra'
88import openBrowser from 'open'
99import type { Argv } from 'yargs'
1010import yargs from 'yargs'
11- import prompts from 'prompts'
1211import { blue , bold , cyan , dim , gray , green , underline , yellow } from 'kolorist'
1312import type { LogLevel , ViteDevServer } from 'vite'
14- import type { SlidevConfig , SlidevPreparserExtension } from '@slidev/types'
13+ import type { SlidevConfig , SlidevData , SlidevPreparserExtension } from '@slidev/types'
1514import isInstalledGlobally from 'is-installed-globally'
1615import equal from 'fast-deep-equal'
1716import { verifyConfig } from '@slidev/parser'
1817import { injectPreparserExtensionLoader } from '@slidev/parser/fs'
18+ import { uniq } from '@antfu/utils'
1919import { checkPort } from 'get-port-please'
2020import { version } from '../package.json'
2121import { createServer } from './server'
2222import type { ResolvedSlidevOptions } from './options'
23- import { getAddonRoots , getClientRoot , getThemeRoots , getUserRoot , isPath , resolveOptions } from './options'
24- import { resolveThemeName } from './themes'
23+ import { resolveOptions } from './options'
24+ import { getThemeMeta , resolveTheme } from './themes'
2525import { parser } from './parser'
2626import { loadSetups } from './plugins/setupNode'
27+ import { getRoots } from './resolver'
28+ import { resolveAddons } from './addons'
2729
2830const CONFIG_RESTART_FIELDS : ( keyof SlidevConfig ) [ ] = [
2931 'highlighter' ,
@@ -33,17 +35,19 @@ const CONFIG_RESTART_FIELDS: (keyof SlidevConfig)[] = [
3335 'css' ,
3436 'mdc' ,
3537 'editor' ,
38+ 'theme' ,
3639]
3740
3841injectPreparserExtensionLoader ( async ( headmatter ?: Record < string , unknown > , filepath ?: string ) => {
3942 const addons = headmatter ?. addons as string [ ] ?? [ ]
40- const roots = /* uniq */ ( [
41- getUserRoot ( { } ) . userRoot ,
42- ...await getAddonRoots ( addons , '' ) ,
43- await getClientRoot ( ) ,
43+ const { clientRoot, userRoot } = await getRoots ( )
44+ const roots = uniq ( [
45+ clientRoot ,
46+ userRoot ,
47+ ...await resolveAddons ( addons ) ,
4448 ] )
4549 const mergeArrays = ( a : SlidevPreparserExtension [ ] , b : SlidevPreparserExtension [ ] ) => a . concat ( b )
46- return await loadSetups ( roots , 'preparser.ts' , { filepath, headmatter } , [ ] , mergeArrays )
50+ return await loadSetups ( clientRoot , roots , 'preparser.ts' , { filepath, headmatter } , [ ] , mergeArrays )
4751} )
4852
4953const cli = yargs ( process . argv . slice ( 2 ) )
@@ -104,22 +108,6 @@ cli.command(
104108 . strict ( )
105109 . help ( ) ,
106110 async ( { entry, theme, port : userPort , open, log, remote, tunnel, force, inspect, bind } ) => {
107- if ( ! fs . existsSync ( entry ) && ! entry . endsWith ( '.md' ) )
108- entry = `${ entry } .md`
109-
110- if ( ! fs . existsSync ( entry ) ) {
111- const { create } = await prompts ( {
112- name : 'create' ,
113- type : 'confirm' ,
114- initial : 'Y' ,
115- message : `Entry file ${ yellow ( `"${ entry } "` ) } does not exist, do you want to create it?` ,
116- } )
117- if ( create )
118- await fs . copyFile ( new URL ( '../template.md' , import . meta. url ) , entry )
119- else
120- process . exit ( 0 )
121- }
122-
123111 let server : ViteDevServer | undefined
124112 let port = 3030
125113
@@ -148,15 +136,30 @@ cli.command(
148136 logLevel : log as LogLevel ,
149137 } ,
150138 {
151- async onDataReload ( newData , data ) {
152- if ( ! theme && await resolveThemeName ( newData . config . theme ) !== await resolveThemeName ( data . config . theme ) ) {
139+ async loadData ( ) {
140+ const { data : oldData , entry } = options
141+ const loaded = await parser . load ( options . userRoot , entry )
142+
143+ const themeRaw = theme || loaded . headmatter . theme as string || 'default'
144+ if ( options . themeRaw !== themeRaw ) {
153145 console . log ( yellow ( '\n restarting on theme change\n' ) )
154146 initServer ( )
147+ return false
155148 }
156- else if ( CONFIG_RESTART_FIELDS . some ( i => ! equal ( newData . config [ i ] , data . config [ i ] ) ) ) {
149+ // Because themeRaw is not changed, we don't resolve it again
150+ const themeMeta = options . themeRoots [ 0 ] ? await getThemeMeta ( themeRaw , options . themeRoots [ 0 ] ) : undefined
151+ const newData : SlidevData = {
152+ ...loaded ,
153+ themeMeta,
154+ config : parser . resolveConfig ( loaded . headmatter , themeMeta , entry ) ,
155+ }
156+
157+ if ( CONFIG_RESTART_FIELDS . some ( i => ! equal ( newData . config [ i ] , oldData . config [ i ] ) ) ) {
157158 console . log ( yellow ( '\n restarting on config change\n' ) )
158159 initServer ( )
160+ return false
159161 }
162+ return newData
160163 } ,
161164 } ,
162165 ) )
@@ -351,23 +354,18 @@ cli.command(
351354 default : 'theme' ,
352355 } ) ,
353356 async ( { entry, dir, theme : themeInput } ) => {
354- const { userRoot } = getUserRoot ( { entry } )
355- const data = await parser . load ( userRoot , entry )
356- const theme = await resolveThemeName ( themeInput || data . config . theme )
357- if ( theme === 'none' ) {
357+ const roots = await getRoots ( )
358+ const data = await parser . load ( roots . userRoot , entry )
359+ const themeRaw = themeInput || ( data . headmatter . theme as string ) || 'default'
360+ if ( themeRaw === 'none' ) {
358361 console . error ( 'Cannot eject theme "none"' )
359362 process . exit ( 1 )
360363 }
361- if ( isPath ( theme ) ) {
364+ if ( '/.' . includes ( themeRaw [ 0 ] ) || ( themeRaw [ 0 ] !== '@' && themeRaw . includes ( '/' ) ) ) {
362365 console . error ( 'Theme is already ejected' )
363366 process . exit ( 1 )
364367 }
365- const roots = await getThemeRoots ( theme , entry )
366- if ( ! roots . length ) {
367- console . error ( `Could not find theme "${ theme } "` )
368- process . exit ( 1 )
369- }
370- const root = roots [ 0 ]
368+ const [ name , root ] = ( await resolveTheme ( themeRaw , entry ) ) as [ string , string ]
371369
372370 await fs . copy ( root , path . resolve ( dir ) , {
373371 filter : i => ! / n o d e _ m o d u l e s | .g i t / . test ( path . relative ( root , i ) ) ,
@@ -379,7 +377,7 @@ cli.command(
379377 parser . prettifySlide ( firstSlide )
380378 await parser . save ( data . entry )
381379
382- console . log ( `Theme "${ theme } " ejected successfully to "${ dirPath } "` )
380+ console . log ( `Theme "${ name } " ejected successfully to "${ dirPath } "` )
383381 } ,
384382 )
385383 } ,
0 commit comments