Skip to content

Commit

Permalink
feat: improve cli
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 25, 2021
1 parent e830f50 commit 4468a50
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 85 deletions.
153 changes: 108 additions & 45 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,115 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import chalk from 'chalk'
import minimist from 'minimist'
import path from 'path'
import fs from 'fs-extra'
import yargs, { Argv } from 'yargs'
import { prompt } from 'enquirer'
import { blue } from 'kolorist'
import { version } from '../package.json'
import { build } from './build'
import { createServer } from './server'
import * as parser from './parser'

const argv: any = minimist(process.argv.slice(2))

console.log(chalk.cyan`Slidev ` + chalk.yellow`v${require('../package.json').version}`)

const command = argv._[0]
const entry = argv._[command ? 1 : 0] || 'slides.md'

if (!command || command === 'dev') {
import('./server')
.then(i => i.createServer(entry, argv))
.then(server => server.listen())
.catch((err) => {
console.error(chalk.red('failed to start server. error:\n'), err)
process.exit(1)
function commonOptions(args: Argv<{}>) {
return args
.positional('entry', {
default: 'slides.md',
type: 'string',
describe: 'path to the slides markdown entry',
})
}
else if (command === 'build') {
import('./build')
.then(i => i.build(entry, argv))
.catch((err) => {
console.error(chalk.red('build error:\n'), err)
process.exit(1)
.option('template', {
alias: 't',
type: 'string',
describe: 'overide theme',
})
}
else if (command === 'export') {
import('./export')
.then(i => i.genratePDF(entry, argv))
.catch((err) => {
console.error(chalk.red('export error:\n'), err)
process.exit(1)

const cli = yargs
.scriptName('slidev')
.usage('$0 [args]')
.version(version)
.showHelpOnFail(false)
.alias('h', 'help')
.alias('v', 'version')

cli.command(
'* [entry]',
'Start a local server for Slidev',
args => commonOptions(args)
.option('port', {
alias: 'p',
default: 3030,
type: 'number',
describe: 'port',
})
}
else if (command === 'format') {
import('./parser')
.then(async({ load, prettify, save }) => {
const data = await load(entry)
prettify(data)
await save(data)
.option('open', {
alias: 'o',
default: true,
type: 'boolean',
describe: 'open in browser',
})
.catch((err) => {
console.error(chalk.red('export error:\n'), err)
process.exit(1)
.help(),
async({ entry, port, open }) => {
if (!fs.existsSync(entry)) {
const { create } = await prompt<{create: boolean}>({
name: 'create',
type: 'confirm',
message: `Entry file ${entry} does not exist, do you want to create it?`,
})
if (create)
await fs.copyFile(path.resolve(__dirname, '../template.md'), entry)
else
process.exit(0)
}

const server = await createServer(entry, {
server: {
port,
open,
},
})
}
else {
console.log(chalk.red(`unknown command "${command}".`))
process.exit(1)
}
server.listen()
server.watcher.add(entry)
},
)

cli.command(
'build [entry]',
'Build hostable SPA',
args => commonOptions(args)
.help(),
async({ entry }) => {
await build(entry)
},
)

cli.command(
'format [entry]',
'Format the markdown file',
args => commonOptions(args)
.help(),
async({ entry }) => {
const data = await parser.load(entry)
parser.prettify(data)
await parser.save(data)
},
)

cli.command(
'export [entry]',
'Export slides to PDF',
args => commonOptions(args)
.option('output', {
type: 'string',
describe: 'path to the the port output',
})
.help(),
async({ entry, output }) => {
output = output || `${path.basename(entry, '.md')}.pdf`
process.env.NODE_ENV = 'production'
const { genratePDF } = await import('./export')
await genratePDF(entry, output, { logLevel: 'error' })
console.log(blue(`PDF Exported: ./${output}`))
process.exit(0)
},
)

cli.help().parse()
12 changes: 5 additions & 7 deletions packages/slidev/node/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { promises as fs } from 'fs'
import { chromium } from 'playwright'
import { InlineConfig } from 'vite'
import { PDFDocument } from 'pdf-lib'
import { green } from 'chalk'
import { yellow } from 'kolorist'
import { createServer } from './server'
import { filterDisabled, load } from './parser'

export async function genratePDF(entry = 'slides.md', config: InlineConfig = {}) {
export async function genratePDF(entry = 'slides.md', output = 'slides.pdf', config: InlineConfig = {}) {
const { slides } = filterDisabled(await load(entry))
const server = await createServer(entry, {
...config,
Expand All @@ -29,7 +29,7 @@ export async function genratePDF(entry = 'slides.md', config: InlineConfig = {})
const buffers: Buffer[] = []
const pagesCount = slides.length
for (let i = 0; i < pagesCount; i++) {
console.log(`Exporting: ${i + 1} / ${pagesCount}`)
console.log(yellow(`Rendering page ${i + 1} / ${pagesCount}`))
await page.goto(`http://localhost:${port}/${i}?print`, {
waitUntil: 'networkidle',
})
Expand Down Expand Up @@ -60,10 +60,8 @@ export async function genratePDF(entry = 'slides.md', config: InlineConfig = {})
}

const buffer = await mergedPdf.save()
await fs.writeFile('slides.pdf', buffer)
console.log(green`Exporting finished: ./slides.pdf`)

await fs.writeFile(output, buffer)
browser.close()
server.close()
process.exit(0)
return output
}
13 changes: 7 additions & 6 deletions packages/slidev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,38 @@
"@slidev/client": "workspace:*",
"@vitejs/plugin-vue": "^1.2.2",
"@vue/compiler-sfc": "^3.0.11",
"chalk": "^4.1.1",
"codemirror": "^5.61.0",
"enquirer": "^2.3.6",
"fast-glob": "^3.2.5",
"fs-extra": "^9.1.0",
"js-base64": "^3.6.0",
"js-yaml": "^3.14.1",
"kolorist": "^1.4.1",
"markdown-it-prism": "^2.1.6",
"minimist": "^1.2.5",
"monaco-editor": "^0.23.0",
"pdf-lib": "^1.16.0",
"theme-vitesse": "^0.1.9",
"vite": "^2.2.2",
"vite-plugin-components": "^0.8.4",
"vite-plugin-icons": "^0.5.0",
"vite-plugin-md": "^0.6.3",
"vite-plugin-remote-assets": "^0.1.3",
"vite-plugin-windicss": "^0.15.7",
"vue": "^3.0.11",
"windicss": "^2.5.14"
"windicss": "^2.5.14",
"yargs": "^16.2.0"
},
"devDependencies": {
"@antfu/eslint-config": "^0.6.4",
"@antfu/ni": "^0.5.8",
"@types/blueimp-md5": "^2.18.0",
"@types/codemirror": "^0.0.109",
"@types/fs-extra": "^9.0.11",
"@types/js-yaml": "^3",
"@types/minimist": "^1.2.1",
"@types/node": "^14.14.41",
"@types/prettier": "^2.2.3",
"@types/recordrtc": "^5.6.5",
"@types/yargs": "^16.0.1",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"blueimp-md5": "^2.18.0",
"cross-env": "^7.0.3",
"eslint": "^7.25.0",
"esno": "^0.5.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/slidev/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Slidev

Presentation slides for developers

> Press Space to next page
----

# Getting Started

Read the [documentations](https://slidev.antfu.me) to get started.

0 comments on commit 4468a50

Please sign in to comment.