diff --git a/.gitignore b/.gitignore index 6746c28..725b7b1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules/ yarn.lock bin/ +test_docs/ diff --git a/e2e/index.js b/e2e/index.js index 5fa8233..6822f5f 100644 --- a/e2e/index.js +++ b/e2e/index.js @@ -25,3 +25,14 @@ test('rejects promise due to error on passing in an unknown command', async t => const {stderr} = await execa(rootCommand, ['junkcmd'], {reject: false}) t.snapshot(stderr) }) + +test('init the docs directory', async t => { + // If you get `./test_docs already exists.`, delete the test_docs directory manually. + const {stdout} = await execa(rootCommand, ['init', './test_docs'], {reject: false, timeout: 3000}) + t.snapshot(stdout) +}) + +test('init the docs directory twice', async t => { + const {stdout} = await execa(rootCommand, ['init', './'], {reject: false, input: 'n'}) + t.snapshot(stdout) +}) diff --git a/e2e/index.js.md b/e2e/index.js.md index 2fe1595..a06f032 100644 --- a/e2e/index.js.md +++ b/e2e/index.js.md @@ -4,6 +4,22 @@ The actual snapshot is saved in `index.js.snap`. Generated by [AVA](https://ava.li). +## init the docs directory + +> Snapshot 1 + + `␊ + Initialization succeeded! Please run docsify serve ./test_docs␊ + ` + +## init the docs directory twice + +> Snapshot 1 + + `./ already exists.␊ + [?25l? Are you sure you want to rewrite it? (y/N) false✔ Are you sure you want to rewrite it? (y/N) false␊ + [?25h` + ## rejects promise due to error on passing in an unknown command > Snapshot 1 diff --git a/e2e/index.js.snap b/e2e/index.js.snap index 3f8aa78..7693d2c 100644 Binary files a/e2e/index.js.snap and b/e2e/index.js.snap differ diff --git a/lib/commands/init.js b/lib/commands/init.js index 91a29be..3d2c451 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -3,6 +3,7 @@ const fs = require('fs') const cp = require('cp-file').sync const chalk = require('chalk') +const {prompt} = require('enquirer') const {cwd, exists, pkg, pwd, read, resolve} = require('../util') const replace = function (file, tpl, replace) { @@ -18,7 +19,37 @@ module.exports = function (path = '', local, theme) { chalk.inverse(`docsify serve ${path}`) + '\n' - path = cwd(path || '.') + const cwdPath = cwd(path || '.') + + if (exists(cwdPath)) { + console.log(chalk.red(`${path || '.'}`) + ' already exists.') + + prompt({ + type: 'confirm', + name: 'rewrite', + symbols: { + separator: '' + }, + message: 'Are you sure you want to rewrite it?' + }) + .then(answers => { + if (answers.rewrite === false) { + return process.exit(0) + } + + createFile(cwdPath, local, theme) + console.log(msg) + }) + .catch(console.error) + + return false + } + + createFile(cwdPath, local, theme) + console.log(msg) +} + +function createFile(path, local, theme) { const target = file => resolve(path, file) const readme = exists(cwd('README.md')) || pwd('template/README.md') let main = pwd('template/index.html') @@ -63,6 +94,4 @@ module.exports = function (path = '', local, theme) { .replace(/^git\+/g, '') replace(target(filename), 'repo: \'\'', `repo: '${repo}'`) } - - console.log(msg) } diff --git a/package-lock.json b/package-lock.json index 93e520c..4695555 100644 --- a/package-lock.json +++ b/package-lock.json @@ -601,6 +601,11 @@ } } }, + "ansi-colors": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", + "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==" + }, "ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", @@ -3407,6 +3412,14 @@ "once": "^1.4.0" } }, + "enquirer": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", + "integrity": "sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==", + "requires": { + "ansi-colors": "^4.1.1" + } + }, "equal-length": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/equal-length/-/equal-length-1.0.1.tgz", diff --git a/package.json b/package.json index ffd6efb..ded444f 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "cp-file": "^7.0.0", "docsify": "^4.10.2", "docsify-server-renderer": ">=4", + "enquirer": "^2.3.6", "fs-extra": "^8.1.0", "get-port": "^5.0.0", "livereload": "^0.9.1",