Skip to content

Commit f811906

Browse files
authored
feat: Added asking whether to rewrite files (#117)
* feat: Added asking whether to rewrite files * fix typo * fix ci * fix ci * Update init.js * add tests * update fix ci fix ci fix ci * change inquirer to enquirer * update symbols
1 parent 9801089 commit f811906

File tree

7 files changed

+74
-3
lines changed

7 files changed

+74
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
node_modules/
55
yarn.lock
66
bin/
7+
test_docs/

e2e/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ test('rejects promise due to error on passing in an unknown command', async t =>
2525
const {stderr} = await execa(rootCommand, ['junkcmd'], {reject: false})
2626
t.snapshot(stderr)
2727
})
28+
29+
test('init the docs directory', async t => {
30+
// If you get `./test_docs already exists.`, delete the test_docs directory manually.
31+
const {stdout} = await execa(rootCommand, ['init', './test_docs'], {reject: false, timeout: 3000})
32+
t.snapshot(stdout)
33+
})
34+
35+
test('init the docs directory twice', async t => {
36+
const {stdout} = await execa(rootCommand, ['init', './'], {reject: false, input: 'n'})
37+
t.snapshot(stdout)
38+
})

e2e/index.js.md

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ The actual snapshot is saved in `index.js.snap`.
44

55
Generated by [AVA](https://ava.li).
66

7+
## init the docs directory
8+
9+
> Snapshot 1
10+
11+
`␊
12+
Initialization succeeded! Please run docsify serve ./test_docs␊
13+
`
14+
15+
## init the docs directory twice
16+
17+
> Snapshot 1
18+
19+
`./ already exists.␊
20+
[?25l? Are you sure you want to rewrite it? (y/N) false✔ Are you sure you want to rewrite it? (y/N) false␊
21+
[?25h`
22+
723
## rejects promise due to error on passing in an unknown command
824

925
> Snapshot 1

e2e/index.js.snap

194 Bytes
Binary file not shown.

lib/commands/init.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const fs = require('fs')
44
const cp = require('cp-file').sync
55
const chalk = require('chalk')
6+
const {prompt} = require('enquirer')
67
const {cwd, exists, pkg, pwd, read, resolve} = require('../util')
78

89
const replace = function (file, tpl, replace) {
@@ -18,7 +19,37 @@ module.exports = function (path = '', local, theme) {
1819
chalk.inverse(`docsify serve ${path}`) +
1920
'\n'
2021

21-
path = cwd(path || '.')
22+
const cwdPath = cwd(path || '.')
23+
24+
if (exists(cwdPath)) {
25+
console.log(chalk.red(`${path || '.'}`) + ' already exists.')
26+
27+
prompt({
28+
type: 'confirm',
29+
name: 'rewrite',
30+
symbols: {
31+
separator: ''
32+
},
33+
message: 'Are you sure you want to rewrite it?'
34+
})
35+
.then(answers => {
36+
if (answers.rewrite === false) {
37+
return process.exit(0)
38+
}
39+
40+
createFile(cwdPath, local, theme)
41+
console.log(msg)
42+
})
43+
.catch(console.error)
44+
45+
return false
46+
}
47+
48+
createFile(cwdPath, local, theme)
49+
console.log(msg)
50+
}
51+
52+
function createFile(path, local, theme) {
2253
const target = file => resolve(path, file)
2354
const readme = exists(cwd('README.md')) || pwd('template/README.md')
2455
let main = pwd('template/index.html')
@@ -63,6 +94,4 @@ module.exports = function (path = '', local, theme) {
6394
.replace(/^git\+/g, '')
6495
replace(target(filename), 'repo: \'\'', `repo: '${repo}'`)
6596
}
66-
67-
console.log(msg)
6897
}

package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"cp-file": "^7.0.0",
5555
"docsify": "^4.10.2",
5656
"docsify-server-renderer": ">=4",
57+
"enquirer": "^2.3.6",
5758
"fs-extra": "^8.1.0",
5859
"get-port": "^5.0.0",
5960
"livereload": "^0.9.1",

0 commit comments

Comments
 (0)