Skip to content

Commit

Permalink
refactor: update watch mode and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ph1p committed Sep 2, 2021
1 parent 6097456 commit 7b5debd
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 39 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ npm i vuepress-jsdoc -g
vuepress-jsdoc --source ./src --dist ./documentation --folder code --title API --exclude=**/*/*.test.js
```

#### Plugin (Dev-Mode) `alpha`

You can use `vuepress-jsdoc` also as plugin.
This plugin watches you generated files.

```javascript
// .vuepress/config.js
plugins: [
[
require('vuepress-jsdoc').default,
{
folder: 'code',
jsDocConfigPath: './jsdoc.json',
source: './src',
dist: './documentation',
title: 'API'
}
]
];
```

#### Watch-Mode `alpha`

If you do not want to run`vuepress-jsdoc` again and again and again.
You can simply pass `--watch` or `-w`.

### Commands

If no command passed it will run `generate` as default
Expand All @@ -45,6 +71,7 @@ If no command passed it will run `generate` as default
| --rmPattern | -rm | | Pattern when removing files. You can ex- and include files. (glob pattern) |
| --partials | -p | | jsdoc2markdown partial templates (overwrites default ones) |
| --jsDocConfigPath | -c | | Path to [JsDoc Config](http://usejsdoc.org/about-configuring-jsdoc.html) (experimental) |
| --watch | -w | | Watch changes and update markdown files |

### config.js

Expand Down
6 changes: 1 addition & 5 deletions example/documentation/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ module.exports = {
jsDocConfigPath: './jsdoc.json',
source: './src',
dist: './documentation',
title: 'API',
exclude: 'class.js',
partials: './partials/*.hbs',
readme: './README.md',
watch: true
title: 'API'
}
]
],*/
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"name": "vuepress-jsdoc",
"version": "3.7.1",
"description": "Generate jsdoc markdown files for vuepress",
"main": "dist/index.js",
"scripts": {
"test": "jest",
"test-coverage": "cross-env NODE_ENV=test jest --coverage",
"test-watch": "cross-env NODE_ENV=test jest --watchAll",
"lint": "eslint --ext .tsx,.ts,.json,.js src/ --fix",
"dev": "npm run build -- -w",
"build": "tsc",
"prepublishOnly": "npm ci && npm run build",
"prepublishOnly": "npm i && npm run build",
"prepare": "husky install",
"version": "conventional-changelog -p karma -i CHANGELOG.md -s -r 0 && git add ."
},
Expand Down
59 changes: 34 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ const parseDirectoryFile = async (file: DirectoryFile, argv: CLIArguments) => {
}
};

const createReadmeFile = async (argv: CLIArguments, deletedPaths?: string[]) => {
const { srcFolder, codeFolder, docsFolder, title, readme } = parseArguments(argv);

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

try {
readMeContent = await fs.readFile(readmePath, 'utf-8');
if (deletedPaths?.some(p => p.indexOf(`${codeFolder}/README.md`) !== -1)) {
console.log(
`\n${chalk.white.bgBlack(' README ')} ${chalk.dim(readmePath.replace('README.md', ''))}${chalk.bold(
'README.md'
)} \u2192 ${chalk.dim(docsFolder)}${chalk.bold('/README.md')}`
);
}
} catch (e) {
console.log(`\n${chalk.white.bgBlack(' README ')} Add default README.md`);
}

await fs.writeFile(`${docsFolder}/README.md`, readMeContent);
};

const parseArguments = (argv: CLIArguments) => {
return {
exclude: (argv.exclude || '').split(',').filter(Boolean),
Expand All @@ -59,7 +81,7 @@ const parseArguments = (argv: CLIArguments) => {
* @param {object} argv passed arguments
*/
export const generate = async (argv: CLIArguments) => {
const { exclude, srcFolder, codeFolder, docsFolder, title, readme, rmPattern } = parseArguments(argv);
const { exclude, srcFolder, codeFolder, docsFolder, title, rmPattern } = parseArguments(argv);

const startTime = +new Date();

Expand Down Expand Up @@ -133,27 +155,7 @@ export const generate = async (argv: CLIArguments) => {
}

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

try {
readMeContent = await fs.readFile(readmePath, 'utf-8');
if (deletedPaths.some(p => p.indexOf(`${codeFolder}/README.md`) !== -1)) {
console.log(
`\n${chalk.white.bgBlack(' README ')} ${chalk.dim(readmePath.replace('README.md', ''))}${chalk.bold(
'README.md'
)} \u2192 ${chalk.dim(docsFolder)}${chalk.bold('/README.md')}`
);
}
} catch (e) {
console.log(`\n${chalk.white.bgBlack(' README ')} Add default README.md`);
}

try {
await fs.access(`${docsFolder}/README.md`);
} catch (e) {
await fs.writeFile(`${docsFolder}/README.md`, readMeContent);
}
await createReadmeFile(argv, deletedPaths);

const resultTime = (Math.abs(startTime - +new Date()) / 1000).toFixed(2);

Expand All @@ -167,7 +169,7 @@ const watchFiles = (argv: CLIArguments) => {

if (argv.watch) {
console.log('\n---\n\n👀 watching files...');
const watcher = chokidar.watch(srcFolder, {
const watcher = chokidar.watch([srcFolder, argv.readme, `${srcFolder}/README.md`].filter(Boolean), {
ignored: /(^|[\/\\])\../,
persistent: true
});
Expand All @@ -176,10 +178,16 @@ const watchFiles = (argv: CLIArguments) => {
const lsFolder = await listFolder(srcFolder, exclude);
const file = lsFolder.paths.find(p => p.path === path);

readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);

if (path === 'README.md' || path === `${srcFolder}/README.md`) {
await createReadmeFile(argv);
}

if (file) {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
process.stdout.write(chalk.dim(`update ${file.name + file.ext}`));

const data = await parseDirectoryFile(file, argv);
if (data) {
await writeContentToFile(data.content, data.dest);
Expand All @@ -201,6 +209,7 @@ export default (argv: CLIArguments, ctx) => ({
name: 'vuepress-plugin-jsdoc',
ready: async () => {
if (!ctx.isProd) {
argv.watch = true;
watchFiles(argv);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ export interface ParseReturn {
relativePathSrc: string;
relativePathDest: string;
}

export interface FileTree {
name: string;
path?: string;
fullPath?: string;
children?: FileTree[];
}
9 changes: 1 addition & 8 deletions src/lib/list-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import fs from 'fs/promises';
import mm from 'micromatch';
import path from 'path';

import { DirectoryFile } from '../interfaces';

interface FileTree {
name: string;
path?: string;
fullPath?: string;
children?: FileTree[];
}
import { DirectoryFile, FileTree } from '../interfaces';

export const listFolder = async (srcPath: string, exclude: string[] = [], mainPath?: string, tree: FileTree[] = []) => {
const paths: DirectoryFile[] = [];
Expand Down

0 comments on commit 7b5debd

Please sign in to comment.