Skip to content

Commit

Permalink
feat(delete): add delete pattern and do not overwrite readme
Browse files Browse the repository at this point in the history
Add some colors
Add an example image
Replace rimraf with del

#9
  • Loading branch information
ph1p committed Jan 31, 2019
1 parent 77a5bc4 commit e08f4fe
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 321 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

This npm package is a command line script, which scans your JavaScript, Vue or Typescript source code and generates markdown files for vuepress with the help of [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).

![CLI ./example](/example/img/cli.gif)

## How to use?

```bash
Expand All @@ -28,16 +30,17 @@ If no command passed it will run `generate` as default

### Options

| Name | Alias | Default | Description |
| --------- | ----- | --------------- | -------------------------------------------------------------------------- |
| --source | -s | ./src | Source folder with .js or .ts files |
| --dist | -d | ./documentation | Destination folder |
| --folder | -f | ./code | Folder inside destination folder. Gets overwritten everytime |
| --title | -t | API | Title of your documentation |
| --help | -h | | Show help |
| --version | -v | | Show current version |
| --readme | -r | | Path to custom readme file |
| --exclude | -e | | Pattern to exclude files/folders (Comma seperated) - \*.test.js,exclude.js |
| Name | Alias | Default | Description |
| ----------- | ----- | --------------- | -------------------------------------------------------------------------- |
| --source | -s | ./src | Source folder with .js or .ts files |
| --dist | -d | ./documentation | Destination folder |
| --folder | -f | ./code | Folder inside destination folder. Gets overwritten everytime |
| --title | -t | API | Title of your documentation |
| --help | -h | | Show help |
| --version | -v | | Show current version |
| --readme | -r | | Path to custom readme file |
| --exclude | -e | | Pattern to exclude files/folders (Comma seperated) - \*.test.js,exclude.js |
| --rmPattern | -rm | | Pattern when removing files. You can ex- and include files. (glob pattern) |

### config.js

Expand Down Expand Up @@ -105,7 +108,10 @@ module.exports = {
## Custom readme

You can easily add a custom path to your readme by using the `--readme ./path/to/file.md` parameter. If you move a `README.md` inside your source folder, it should resolve it automatically.
You can set the title by passing it to the `sidebarTree('Mainpage title')` function.
You can set the title by passing it to the `sidebarTree('Mainpage title')` function inside your `./.vuepress/config.js`.

Once the README.md has been added, it is no longer overwritten!
If you want it overwritten, set `--rmPattern=./documentation/code/README.md`

## @vuepress comment block

Expand Down
77 changes: 44 additions & 33 deletions cmds/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const path = require('path');
const mkdirp = require('mkdirp');
const jsdoc2md = require('jsdoc-to-markdown');
const vuedoc = require('@vuedoc/md');
const rimraf = require('rimraf');
const del = require('del');
const mm = require('micromatch');
const chalk = require('chalk');

const vueSidebar = require('../helpers/vueSidebar');
const parseVuepressComment = require('../helpers/commentParser');
Expand All @@ -18,45 +19,55 @@ const extensions = ['.ts', '.js', '.vue'];
* Default command that generate md files
* @param {Object} argv Arguments passed by yargs
*/
function generate(argv) {
async function generate(argv) {
const exclude = argv.exclude && (argv.exclude.split(',') || [argv.exclude]);
const srcFolder = argv.source;
const codeFolder = argv.folder;
const docsFolder = `${argv.dist}/${codeFolder}`;
const title = argv.title;
const readme = argv.readme;
const rmPattern = argv.rmPattern || '';

// remove docs folder, except README.md
const deletedPaths = await del([docsFolder + '/**/*', `!${docsFolder}/README.md`, rmPattern]);

// create docs folder
mkdirp(docsFolder, async () => {
// read folder files
await readFiles(srcFolder, 0, fileTree);

await fs.writeFile(
`${docsFolder}/config.js`,
`exports.fileTree=${JSON.stringify(fileTree)};exports.sidebarTree = (title = 'Mainpage') => (${JSON.stringify(
vueSidebar({
fileTree,
codeFolder,
title
})
).replace('::vuepress-jsdoc-title::', '"+title+"')});`
);

// create README.md
let readMeContent = '### Welcome to ' + title;
let readmePath = readme || `${srcFolder}/README.md`;

// remove docs folder
rimraf(docsFolder, () =>
// create docs folder
mkdirp(docsFolder, async () => {
// read folder files
await readFiles(srcFolder, 0, fileTree);

await fs.writeFile(
`${docsFolder}/config.js`,
`exports.fileTree=${JSON.stringify(fileTree)};exports.sidebarTree = (title = 'Mainpage') => (${JSON.stringify(
vueSidebar({
fileTree,
codeFolder,
title
})
).replace('::vuepress-jsdoc-title::', '"+title+"')});`
);

// create README.md
let readMeContent = 'Welcome';
let readmePath = readme || `${srcFolder}/README.md`;

try {
readMeContent = await fs.readFile(readmePath, 'utf-8');
} catch (e) {
console.log('INFO: There is no custom readme file.');
try {
readMeContent = await fs.readFile(readmePath, 'utf-8');
if (deletedPaths.some(p => ~p.indexOf(`${codeFolder}/README.md`))) {
console.log(`\n${chalk.black.bgYellow('found')} ${readmePath} and copies content to ${docsFolder}/README.md`);
}
} catch (e) {}

// Do nothing if README.md already exists
try {
readMeContent = await fs.readFile(`${docsFolder}/README.md`, 'utf-8');
console.log(`\n${chalk.yellow(`${docsFolder}/README.md already exists`)}`);
} catch (e) {
await fs.writeFile(`${docsFolder}/README.md`, readMeContent);
})
);
}

console.log(`\n${chalk.green.bold(`Finished! 👍 `)}`);
});

/**
* Check if extension ist correct
Expand Down Expand Up @@ -103,7 +114,7 @@ function generate(argv) {
// iterate through all files in folder
await asyncForEach(files, async file => {
if (mm.contains(`${folder}/${file}`, exclude)) {
console.log('exclude', `${folder}/${file}`);
console.log(chalk.black.bgBlue('exclude'), `${folder}/${file}`);
return;
}

Expand All @@ -128,7 +139,7 @@ function generate(argv) {
}
} catch (err) {
console.log(err);
console.log(`can't create folder, because it already exists`, `${folderPath}/${file}`);
console.log(chalk.yellow(`can't create folder, because it already exists`), `${folderPath}/${file}`);
}

// Add to tree
Expand Down Expand Up @@ -165,7 +176,7 @@ function generate(argv) {
if (mdFileData) {
const { frontmatter } = parseVuepressComment(fileData);

console.log('write file', `${folderPath}/${fileName}.md`);
console.log(chalk.black.bgGreen('write file'), `${folderPath}/${fileName}.md`);

await fs.writeFile(
`${folderPath}/${fileName}.md`,
Expand Down
Binary file added example/img/cli.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e08f4fe

Please sign in to comment.