Skip to content

Commit 7b5debd

Browse files
committed
refactor: update watch mode and fix some bugs
1 parent 6097456 commit 7b5debd

File tree

6 files changed

+72
-39
lines changed

6 files changed

+72
-39
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ npm i vuepress-jsdoc -g
2222
vuepress-jsdoc --source ./src --dist ./documentation --folder code --title API --exclude=**/*/*.test.js
2323
```
2424

25+
#### Plugin (Dev-Mode) `alpha`
26+
27+
You can use `vuepress-jsdoc` also as plugin.
28+
This plugin watches you generated files.
29+
30+
```javascript
31+
// .vuepress/config.js
32+
plugins: [
33+
[
34+
require('vuepress-jsdoc').default,
35+
{
36+
folder: 'code',
37+
jsDocConfigPath: './jsdoc.json',
38+
source: './src',
39+
dist: './documentation',
40+
title: 'API'
41+
}
42+
]
43+
];
44+
```
45+
46+
#### Watch-Mode `alpha`
47+
48+
If you do not want to run`vuepress-jsdoc` again and again and again.
49+
You can simply pass `--watch` or `-w`.
50+
2551
### Commands
2652

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

4976
### config.js
5077

example/documentation/.vuepress/config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ module.exports = {
1414
jsDocConfigPath: './jsdoc.json',
1515
source: './src',
1616
dist: './documentation',
17-
title: 'API',
18-
exclude: 'class.js',
19-
partials: './partials/*.hbs',
20-
readme: './README.md',
21-
watch: true
17+
title: 'API'
2218
}
2319
]
2420
],*/

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"name": "vuepress-jsdoc",
33
"version": "3.7.1",
44
"description": "Generate jsdoc markdown files for vuepress",
5+
"main": "dist/index.js",
56
"scripts": {
67
"test": "jest",
78
"test-coverage": "cross-env NODE_ENV=test jest --coverage",
89
"test-watch": "cross-env NODE_ENV=test jest --watchAll",
910
"lint": "eslint --ext .tsx,.ts,.json,.js src/ --fix",
1011
"dev": "npm run build -- -w",
1112
"build": "tsc",
12-
"prepublishOnly": "npm ci && npm run build",
13+
"prepublishOnly": "npm i && npm run build",
1314
"prepare": "husky install",
1415
"version": "conventional-changelog -p karma -i CHANGELOG.md -s -r 0 && git add ."
1516
},

src/index.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ const parseDirectoryFile = async (file: DirectoryFile, argv: CLIArguments) => {
4141
}
4242
};
4343

44+
const createReadmeFile = async (argv: CLIArguments, deletedPaths?: string[]) => {
45+
const { srcFolder, codeFolder, docsFolder, title, readme } = parseArguments(argv);
46+
47+
let readMeContent = `### Welcome to ${title}`;
48+
const readmePath = readme || `${srcFolder}/README.md`;
49+
50+
try {
51+
readMeContent = await fs.readFile(readmePath, 'utf-8');
52+
if (deletedPaths?.some(p => p.indexOf(`${codeFolder}/README.md`) !== -1)) {
53+
console.log(
54+
`\n${chalk.white.bgBlack(' README ')} ${chalk.dim(readmePath.replace('README.md', ''))}${chalk.bold(
55+
'README.md'
56+
)} \u2192 ${chalk.dim(docsFolder)}${chalk.bold('/README.md')}`
57+
);
58+
}
59+
} catch (e) {
60+
console.log(`\n${chalk.white.bgBlack(' README ')} Add default README.md`);
61+
}
62+
63+
await fs.writeFile(`${docsFolder}/README.md`, readMeContent);
64+
};
65+
4466
const parseArguments = (argv: CLIArguments) => {
4567
return {
4668
exclude: (argv.exclude || '').split(',').filter(Boolean),
@@ -59,7 +81,7 @@ const parseArguments = (argv: CLIArguments) => {
5981
* @param {object} argv passed arguments
6082
*/
6183
export const generate = async (argv: CLIArguments) => {
62-
const { exclude, srcFolder, codeFolder, docsFolder, title, readme, rmPattern } = parseArguments(argv);
84+
const { exclude, srcFolder, codeFolder, docsFolder, title, rmPattern } = parseArguments(argv);
6385

6486
const startTime = +new Date();
6587

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

135157
// create README.md
136-
let readMeContent = `### Welcome to ${title}`;
137-
const readmePath = readme || `${srcFolder}/README.md`;
138-
139-
try {
140-
readMeContent = await fs.readFile(readmePath, 'utf-8');
141-
if (deletedPaths.some(p => p.indexOf(`${codeFolder}/README.md`) !== -1)) {
142-
console.log(
143-
`\n${chalk.white.bgBlack(' README ')} ${chalk.dim(readmePath.replace('README.md', ''))}${chalk.bold(
144-
'README.md'
145-
)} \u2192 ${chalk.dim(docsFolder)}${chalk.bold('/README.md')}`
146-
);
147-
}
148-
} catch (e) {
149-
console.log(`\n${chalk.white.bgBlack(' README ')} Add default README.md`);
150-
}
151-
152-
try {
153-
await fs.access(`${docsFolder}/README.md`);
154-
} catch (e) {
155-
await fs.writeFile(`${docsFolder}/README.md`, readMeContent);
156-
}
158+
await createReadmeFile(argv, deletedPaths);
157159

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

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

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

181+
readline.clearLine(process.stdout, 0);
182+
readline.cursorTo(process.stdout, 0);
183+
184+
if (path === 'README.md' || path === `${srcFolder}/README.md`) {
185+
await createReadmeFile(argv);
186+
}
187+
179188
if (file) {
180-
readline.clearLine(process.stdout, 0);
181-
readline.cursorTo(process.stdout, 0);
182189
process.stdout.write(chalk.dim(`update ${file.name + file.ext}`));
190+
183191
const data = await parseDirectoryFile(file, argv);
184192
if (data) {
185193
await writeContentToFile(data.content, data.dest);
@@ -201,6 +209,7 @@ export default (argv: CLIArguments, ctx) => ({
201209
name: 'vuepress-plugin-jsdoc',
202210
ready: async () => {
203211
if (!ctx.isProd) {
212+
argv.watch = true;
204213
watchFiles(argv);
205214
}
206215
}

src/interfaces.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ export interface ParseReturn {
2929
relativePathSrc: string;
3030
relativePathDest: string;
3131
}
32+
33+
export interface FileTree {
34+
name: string;
35+
path?: string;
36+
fullPath?: string;
37+
children?: FileTree[];
38+
}

src/lib/list-folder.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import fs from 'fs/promises';
22
import mm from 'micromatch';
33
import path from 'path';
44

5-
import { DirectoryFile } from '../interfaces';
6-
7-
interface FileTree {
8-
name: string;
9-
path?: string;
10-
fullPath?: string;
11-
children?: FileTree[];
12-
}
5+
import { DirectoryFile, FileTree } from '../interfaces';
136

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

0 commit comments

Comments
 (0)