-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.