diff --git a/README.md b/README.md index 80f7cc4..db9ee0e 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,11 @@ docsify init [path] [--local false] [--theme vue] [--plugins false] - Type: boolean - Default: `false` - Description: Copy `docsify` files to the docs path, defaults to `false` using `cdn.jsdelivr.net` as the content delivery network (CDN). To explicitly set this option to `false` use `--no-local`. -- `--theme` option: - - Shorthand: `-t` - - Type: string - - Default: `vue` - - Description: Choose a theme, defaults to `vue`, other choices are `buble`, `dark` and `pure`. +- `--rcMode` option: + - Shorthand: `--rc` + - Type: boolean + - Default: `false` + - Description: Try `docsify` preview release version (`rc` resource). - `--plugins` option: - Shorthand: `-p` - Type: boolean diff --git a/lib/cli.js b/lib/cli.js index a545058..09d5fd8 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -30,6 +30,14 @@ require('yargs') requiresArg: false, type: 'boolean' }, + rcMode: { + alias: 'rc', + default: false, + desc: chalk.gray(y18n.__('init.rc')), + nargs: 0, + requiresArg: false, + type: 'boolean' + }, theme: { alias: 't', default: 'vue', @@ -48,7 +56,7 @@ require('yargs') type: 'boolean' } }), - handler: argv => run.init(argv.path, argv.local, argv.theme, argv.plugins) + handler: argv => run.init(argv.path, argv.local, argv.rcMode, argv.theme, argv.plugins) }) .command({ command: 'serve [path]', diff --git a/lib/commands/init.js b/lib/commands/init.js index 8157e8a..051fe62 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -3,7 +3,7 @@ const fs = require('fs') const cp = require('cp-file').sync const chalk = require('chalk') -const {version} = require('../../package.json') +const {version: cliVersion} = require('../../package.json') const logger = require('../util/logger') const {prompt, MultiSelect} = require('enquirer') const {cwd, exists, pkg, pwd, read, resolve} = require('../util') @@ -14,8 +14,12 @@ const replace = function (file, tpl, replace) { fs.writeFileSync(file, read(file).replace(tpl, replace), 'utf-8') } +const replaceAll = function (file, tpl, replace) { + fs.writeFileSync(file, read(file).replaceAll(tpl, replace), 'utf-8') +} + // eslint-disable-next-line -module.exports = async function (path = '', local, theme, plugins) { +module.exports = async function (path = '', local, rcMode, theme, plugins) { const msg = '\n' + chalk.green('Initialization succeeded!') + @@ -48,11 +52,11 @@ module.exports = async function (path = '', local, theme, plugins) { } } - await createFile(cwdPath, local, theme, plugins) + await createFile(cwdPath, local, rcMode, theme, plugins) console.log(msg) } -async function createFile(path, local, theme, plugins) { +async function createFile(path, local, rcMode, theme, plugins) { const target = file => resolve(path, file) const readme = exists(cwd('README.md')) || pwd('template/README.md') let main = pwd('template/index.html') @@ -97,6 +101,15 @@ async function createFile(path, local, theme, plugins) { replace(target(filename), 'repo: \'\'', `repo: '${repo}'`) } + let resourceVersion = 'rc' + + if (!rcMode) { + const {major, prerelease = [], version} = semver.parse(cliVersion) || {} + resourceVersion = prerelease.length ? version : String(major) + } + + replaceAll(target(filename), '__docsifyVersion__', `${resourceVersion}`) + // Return early if not opted for plugins if (!plugins) { return replace(target(filename), '\n _plugins_', '') @@ -147,11 +160,8 @@ async function createFile(path, local, theme, plugins) { replace(target(filename), ' _plugins_', '_plugin'.repeat(answers.length + 1)) - const {prerelease = [], major} = semver.parse(version) || {} - const pluginVersion = prerelease.length ? prerelease[0] : String(major) - answers.forEach(plugin => { - const url = `//cdn.jsdelivr.net/npm/docsify@${pluginVersion}/dist/plugins/${plugin}.min.js` + const url = `//cdn.jsdelivr.net/npm/docsify@${resourceVersion}/dist/plugins/${plugin}.min.js` replace(target(filename), '_plugin', ` \n`) }) diff --git a/lib/template/index.html b/lib/template/index.html index 5e8192e..602c0c7 100644 --- a/lib/template/index.html +++ b/lib/template/index.html @@ -5,8 +5,8 @@