Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions docs/plugins/tools/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,24 @@ Sometimes you may change `base` or use new domain for your site, so you may want

To solve this, the plugin provide `vp-redirect` cli.

```shell
Usage:
$ vp-redirect generate [sourceDir]
```plain
Usage: vp-redirect [options] <source> [output]

Generate redirect site for current VuePress project

Arguments:
source Source directory of VuePress project
output Output folder (default: .vuepress/redirect relative to source folder)

Options:
--hostname <hostname> Hostname to redirect to (E.g.: https://new.example.com/) (default: /)
-c, --config <config> Set path to config file
-o, --output <output> Set the output directory (default: .vuepress/redirect)
--cache <cache> Set the directory of the cache files
-t, --temp <temp> Set the directory of the temporary files
--hostname <hostname> Hostname to redirect to (E.g.: https://new.example.com/) (default: "/")
-c, --config [config] Set path to config file
--cache [cache] Set the directory of the cache files
--temp [temp] Set the directory of the temporary files
--clean-cache Clean the cache files before generation
--clean-temp Clean the temporary files before generation
-h, --help Display this message
-V, --version output the version number
-h, --help display help for command
```

You need to pass in VuePress project source dir and also set the `hostname` option. The redirect helper cli will initialize your VuePress project to get pages, then generate and output the redirect html files to the output directory.
Expand Down
25 changes: 16 additions & 9 deletions docs/zh/plugins/tools/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,26 @@ redirect({

为了解决这个问题,插件提供了 `vp-redirect` 脚手架。

```shell
使用:
$ vp-redirect generate [源文件夹]
```plain
使用: vp-redirect [选项] <源文件夹> [输出文件夹]

Options:
为当前 VuePress 项目生成重定向站点

Generate redirect site for current VuePress project

参数:
源文件夹 VuePress 项目的源文件夹
输出文件夹 输出文件夹 (默认: .vuepress/redirect 相对于源文件夹)

选项:
--hostname <hostname> 重定向到的域名 (例如: https://new.example.com/) (默认: /)
-c, --config <config> 设置配置文件路径
-o, --output <output> 设置输出目录 (默认: .vuepress/redirect)
--cache <cache> 设置缓存文件的目录
-t, --temp <temp> 设置临时文件的目录
-c, --config [config] 设置配置文件路径
--cache [cache] 设置缓存文件的目录
--temp [temp] 设置临时文件的目录
--clean-cache 生成前清理缓存文件
--clean-temp 生成前清理临时文件
-h, --help 显示此消息
-V, --version 输出版本号
-h, --help 显示帮助信息
```

你需要传入 VuePress 项目源目录并设置 `hostname` 选项。重定向助手脚手架将初始化你的 VuePress 项目以获取页面,然后在输出目录生成重定向 html 文件。
Expand Down
2 changes: 1 addition & 1 deletion plugins/tools/plugin-redirect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@vuepress/helper": "workspace:*",
"@vueuse/core": "^12.4.0",
"cac": "^6.7.14",
"commander": "^13.0.0",
"vue": "^3.5.13"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion plugins/tools/plugin-redirect/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { rollupBundle } from '../../../scripts/rollup.js'

export default [
...rollupBundle('cli/index', {
external: ['cac'],
external: ['commander'],
}),
...rollupBundle('node/index', {
dtsExternal: ['@vuepress/helper/shared'],
Expand Down
205 changes: 103 additions & 102 deletions plugins/tools/plugin-redirect/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { removeEndingSlash, removeLeadingSlash } from '@vuepress/helper'
import { cac } from 'cac'
import { createCommand } from 'commander'
import {
loadUserConfig,
resolveAppConfig,
Expand All @@ -16,117 +16,118 @@ import { getRedirectHTML } from '../node/generate/getRedirectHTML.js'

interface RedirectCommandOptions {
hostname: string
output?: string
config?: string
cache: string
cache?: string
temp?: string
cleanCache?: boolean
cleanTemp?: boolean
}

const cli = cac('vp-redirect')
const program = createCommand('vp-redirect')

cli
.command(
'generate [source-dir]',
'Generate redirect site using VuePress project under source folder',
)
program
.summary('Generate redirect site')
.description('Generate redirect site for current VuePress project')
.option(
'--hostname <hostname>',
'Hostname to redirect to (E.g.: https://new.example.com/)',
{ default: '/' },
)
.option('-c, --config <config>', 'Set path to config file')
.option(
'-o, --output <output>',
'Set the output directory (default: .vuepress/redirect)',
'/',
)
.option('--cache <cache>', 'Set the directory of the cache files')
.option('-t, --temp <temp>', 'Set the directory of the temporary files')
.option('-c, --config [config]', 'Set path to config file')
.option('--cache [cache]', 'Set the directory of the cache files')
.option('--temp [temp]', 'Set the directory of the temporary files')
.option('--clean-cache', 'Clean the cache files before generation')
.option('--clean-temp', 'Clean the temporary files before generation')
.action(async (sourceDir: string, commandOptions: RedirectCommandOptions) => {
if (!sourceDir) {
cli.outputHelp()
return
}

// ensure NODE_ENV is set
process.env.NODE_ENV ??= 'production'

// resolve app config from cli options
const cliAppConfig = resolveCliAppConfig(sourceDir, {})

// resolve user config file
const userConfigPath = commandOptions.config
? resolveUserConfigPath(commandOptions.config)
: resolveUserConfigConventionalPath(cliAppConfig.source)

const { userConfig } = await loadUserConfig(userConfigPath)

// resolve the final app config to use
const appConfig = resolveAppConfig({
defaultAppConfig: {},
cliAppConfig,
userConfig,
})

if (appConfig === null) return

// create vuepress app
const app = createBuildApp(appConfig)

// use user-config plugin
app.use(transformUserConfigToPlugin(userConfig, cliAppConfig.source))

// clean temp and cache
if (commandOptions.cleanTemp === true) {
logger.info('Cleaning temp...')
await fs.remove(app.dir.temp())
}
if (commandOptions.cleanCache === true) {
logger.info('Cleaning cache...')
await fs.remove(app.dir.cache())
}

const outputFolder = commandOptions.output
? path.join(process.cwd(), commandOptions.output)
: path.join(app.dir.source(), '.vuepress', 'redirect')

// empty output directory
await fs.emptyDir(outputFolder)

// initialize vuepress app to get pages
logger.info('Initializing VuePress and preparing data...')

// initialize vuepress app to get pages
await app.init()

logger.info('Generating redirect pages...')

// redirect all pages
await Promise.all(
app.pages.map((page) => {
const redirectUrl = `${removeEndingSlash(commandOptions.hostname)}${
app.siteData.base
}${removeLeadingSlash(page.path)}`
const destLocation = path.join(
outputFolder,
removeLeadingSlash(page.path.replace(/\/$/, '/index.html')),
)

return fs
.ensureDir(path.dirname(destLocation))
.then(() => fs.writeFile(destLocation, getRedirectHTML(redirectUrl)))
}),
)
})

cli.command('').action(() => {
cli.outputHelp()
})

cli.help()
cli.version(pkg.version)

cli.parse()
.argument('<source>', 'Source directory of VuePress project')
.argument(
'[output]',
'Output folder (default: .vuepress/redirect relative to source folder)',
)
.action(
async (
sourceDir: string,
output: string | undefined,
commandOptions: RedirectCommandOptions,
) => {
// ensure NODE_ENV is set
process.env.NODE_ENV ??= 'production'

if (!fs.existsSync(sourceDir)) {
program.error(`Source directory ${sourceDir} does not exist!`)
}

// resolve app config from cli options
const cliAppConfig = resolveCliAppConfig(sourceDir, {})

// resolve user config file
const userConfigPath = commandOptions.config
? resolveUserConfigPath(commandOptions.config)
: resolveUserConfigConventionalPath(cliAppConfig.source)

const { userConfig } = await loadUserConfig(userConfigPath)

// resolve the final app config to use
const appConfig = resolveAppConfig({
defaultAppConfig: {},
cliAppConfig,
userConfig,
})

if (appConfig === null) return

// create vuepress app
const app = createBuildApp(appConfig)

// use user-config plugin
app.use(transformUserConfigToPlugin(userConfig, cliAppConfig.source))

// clean temp and cache
if (commandOptions.cleanTemp === true) {
logger.info('Cleaning temp...')
await fs.remove(app.dir.temp())
}
if (commandOptions.cleanCache === true) {
logger.info('Cleaning cache...')
await fs.remove(app.dir.cache())
}

const outputFolder = output
? path.join(process.cwd(), output)
: path.join(app.dir.source(), '.vuepress', 'redirect')

// empty output directory
await fs.emptyDir(outputFolder)

// initialize vuepress app to get pages
logger.info('Initializing VuePress and preparing data...')

// initialize vuepress app to get pages
await app.init()

logger.info('Generating redirect pages...')

// redirect all pages
await Promise.all(
app.pages.map((page) => {
const redirectUrl = `${removeEndingSlash(commandOptions.hostname)}${
app.siteData.base
}${removeLeadingSlash(page.path)}`
const destLocation = path.join(
outputFolder,
removeLeadingSlash(page.path.replace(/\/$/, '/index.html')),
)

return fs
.ensureDir(path.dirname(destLocation))
.then(() =>
fs.writeFile(destLocation, getRedirectHTML(redirectUrl)),
)
}),
)
},
)

program.version(pkg.version)
program.showHelpAfterError('add --help for additional information')

await program.parseAsync()
24 changes: 15 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tools/create-vuepress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"dependencies": {
"@inquirer/prompts": "^7.2.1",
"cac": "^6.7.14",
"commander": "^13.0.0",
"execa": "^9.5.2"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion tools/create-vuepress/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { rollupBundle } from '../../scripts/rollup.js'

export default rollupBundle('index', {
external: [/^node:/, '@inquirer/prompts', 'cac', 'execa'],
external: [/^node:/, '@inquirer/prompts', 'commander', 'execa'],
dts: false,
})
Loading
Loading