Skip to content

Commit 53f8560

Browse files
committed
feat(cli): new command theme eject
1 parent f90e56d commit 53f8560

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

packages/slidev/node/cli.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { LogLevel, ViteDevServer } from 'vite'
99
import * as parser from '@slidev/parser/fs'
1010
import { version } from '../package.json'
1111
import { createServer } from './server'
12-
import { ResolvedSlidevOptions, resolveOptions } from './options'
12+
import { getThemeRoots, isRelative, ResolvedSlidevOptions, resolveOptions } from './options'
1313
import { resolveThemeName } from './themes'
1414

1515
const cli = yargs
@@ -155,6 +155,56 @@ cli.command(
155155
},
156156
)
157157

158+
cli.command(
159+
'theme [subcommand]',
160+
'Theme related operations',
161+
(command) => {
162+
return command
163+
.command(
164+
'eject',
165+
'Eject current theme into local file system',
166+
args => commonOptions(args)
167+
.option('dir', {
168+
type: 'string',
169+
default: 'theme',
170+
}),
171+
async({ entry, dir, theme: themeInput }) => {
172+
const data = await parser.load(entry)
173+
const theme = resolveThemeName(themeInput || data.config.theme)
174+
if (theme === 'none') {
175+
console.error('Can not eject theme "none"')
176+
process.exit(1)
177+
}
178+
if (isRelative(theme)) {
179+
console.error('Theme is already ejected')
180+
process.exit(1)
181+
}
182+
const roots = getThemeRoots(theme, entry)
183+
if (!roots.length) {
184+
console.error(`Does not found theme "${theme}"`)
185+
process.exit(1)
186+
}
187+
const root = roots[0]
188+
189+
await fs.copy(root, path.resolve(dir), {
190+
filter: i => !/node_modules|.git/.test(path.relative(root, i)),
191+
})
192+
193+
const dirPath = `./${dir}`
194+
data.slides[0].frontmatter.theme = dirPath
195+
data.slides[0].raw = ''
196+
await parser.save(data)
197+
198+
console.log(`Theme "${theme}" ejected successfully to "${dirPath}"`)
199+
},
200+
)
201+
},
202+
() => {
203+
cli.showHelp()
204+
process.exit(1)
205+
},
206+
)
207+
158208
cli.command(
159209
'export [entry]',
160210
'Export slides to PDF',

0 commit comments

Comments
 (0)