Skip to content

Commit

Permalink
feat: Added asking whether to rewrite files (#117)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sy-records committed Aug 20, 2020
1 parent 9801089 commit f811906
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
node_modules/
yarn.lock
bin/
test_docs/
11 changes: 11 additions & 0 deletions e2e/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
16 changes: 16 additions & 0 deletions e2e/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified e2e/index.js.snap
Binary file not shown.
35 changes: 32 additions & 3 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
Expand Down Expand Up @@ -63,6 +94,4 @@ module.exports = function (path = '', local, theme) {
.replace(/^git\+/g, '')
replace(target(filename), 'repo: \'\'', `repo: '${repo}'`)
}

console.log(msg)
}
13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f811906

Please sign in to comment.