-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: auto generate sidebar * test: generate _sidebar.md * Apply suggestions from code review Co-authored-by: James George <jamesgeorge998001@gmail.com> * test: Add the sidebar file already exists test Co-authored-by: James George <jamesgeorge998001@gmail.com>
- Loading branch information
1 parent
f7e6b37
commit e83bfcb
Showing
10 changed files
with
168 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const test = require('ava') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const {run} = require('../helpers/test-utils.js') | ||
|
||
const genPath = path.join(__dirname, 'generate-cmd') | ||
const docsPath = path.join(genPath, 'docs') | ||
|
||
test.before('create temp directory', () => { | ||
// Cleanup if the directory already exists | ||
if (fs.existsSync(genPath)) { | ||
fs.rmdirSync(genPath, {recursive: true}) | ||
} | ||
|
||
fs.mkdirSync(genPath) | ||
}) | ||
|
||
test.after('cleanup', () => { | ||
fs.rmdirSync(genPath, {recursive: true}) | ||
}) | ||
|
||
test('generate _sidebar.md', t => { | ||
run(['init', 'docs'], {cwd: genPath}) | ||
run(['generate', 'docs'], {cwd: genPath}) | ||
// Check for existence | ||
t.true(fs.existsSync(path.join(docsPath, '_sidebar.md'))) | ||
|
||
const {stderr} = run(['generate', 'docs'], {cwd: genPath}) | ||
t.is(stderr, 'The sidebar file \'_sidebar.md\' already exists.') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const os = require('os') | ||
const {cwd, exists} = require('../util') | ||
const chalk = require('chalk') | ||
const path = require('path') | ||
const ignoreFiles = ['_navbar', '_coverpage', '_sidebar'] | ||
|
||
// eslint-disable-next-line | ||
module.exports = function (path = '', sidebar) { | ||
const cwdPath = cwd(path || '.') | ||
|
||
if (exists(cwdPath)) { | ||
if (sidebar) { | ||
const sidebarPath = cwdPath + '/' + sidebar || '_sidebar.md' | ||
|
||
if (!exists(sidebarPath)) { | ||
genSidebar(cwdPath, sidebarPath) | ||
console.log(chalk.green(`Successfully generated the sidebar file '${sidebar}'.`)) | ||
return true | ||
} | ||
|
||
console.error(chalk.red(`The sidebar file '${sidebar}' already exists.`)) | ||
return false | ||
} | ||
} | ||
|
||
console.error(chalk.red(`${cwdPath}`) + ' directory does not exist.') | ||
} | ||
|
||
function genSidebar(cwdPath, sidebarPath) { | ||
let tree = '' | ||
let lastPath = '' | ||
let nodeName = '' | ||
getDirFiles(cwdPath, function (pathname) { | ||
path.relative(pathname, cwdPath) | ||
pathname = pathname.replace(cwdPath + '/', '') | ||
let filename = path.basename(pathname, '.md') | ||
let splitPath = pathname.split(path.sep) | ||
|
||
if (ignoreFiles.indexOf(filename) === -1) { | ||
nodeName = '- [' + filename + '](' + pathname + ')' + os.EOL | ||
} | ||
|
||
if (splitPath.length > 1) { | ||
if (splitPath[0] !== lastPath) { | ||
lastPath = splitPath[0] | ||
tree += os.EOL + '- ' + splitPath[0] + os.EOL | ||
} | ||
|
||
tree += ' ' + nodeName | ||
} else { | ||
if (lastPath !== '') { | ||
lastPath = '' | ||
tree += os.EOL | ||
} | ||
|
||
tree += nodeName | ||
} | ||
}) | ||
fs.writeFile(sidebarPath, tree, 'utf8', err => { | ||
if (err) { | ||
console.error(chalk.red(`Couldn't generate the sidebar file, error: ${err.message}`)) | ||
} | ||
}) | ||
} | ||
|
||
function getDirFiles(dir, callback) { | ||
fs.readdirSync(dir).forEach(function (file) { | ||
let pathname = path.join(dir, file) | ||
|
||
if (fs.statSync(pathname).isDirectory()) { | ||
getDirFiles(pathname, callback) | ||
} else if (path.extname(file) === '.md') { | ||
callback(pathname) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
init: require('./commands/init'), | ||
serve: require('./commands/serve'), | ||
start: require('./commands/start') | ||
start: require('./commands/start'), | ||
generate: require('./commands/generate') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters