Skip to content

Commit 37d4bd6

Browse files
committed
feat(error): drop ci command and exit with error code
Switch from yargs to commander to prepare newer versions Improve output style
1 parent cfdbdcd commit 37d4bd6

File tree

8 files changed

+276
-158
lines changed

8 files changed

+276
-158
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ If no command passed it will run `generate` as default
4545
| --rmPattern | -rm | | Pattern when removing files. You can ex- and include files. (glob pattern) |
4646
| --partials | -p | | jsdoc2markdown partial templates (overwrites default ones) |
4747
| --jsDocConfigPath | -c | | Path to [JsDoc Config](http://usejsdoc.org/about-configuring-jsdoc.html) (experimental) |
48-
| --ci | - | false | Set this for your continuous integration tool. This causes errors to be thrown and not ignored. You can also use the global env variable `CI` instead |
4948

5049
### config.js
5150

@@ -59,7 +58,7 @@ You can add all generated documentation files to your existing vuepress project
5958

6059
```bash
6160
# First install vuepress
62-
yarn global add vuepress
61+
yarn global add vuepress
6362

6463
# Run the CLI
6564
vuepress-jsdoc
@@ -154,19 +153,19 @@ The `./example` folder includes a full working vuepress-jsdoc example.
154153

155154
```bash
156155
# Install dependencies
157-
yarn
156+
npm install
158157

159158
# Run the CLI
160159
vuepress-jsdoc
161160

162161
# Generate docs
163-
yarn docs
162+
npm run docs
164163

165164
# Run dev server
166-
yarn dev
165+
npm run dev
167166

168167
# Generate dist folder
169-
yarn build
168+
npm run build
170169
```
171170

172171
## Contribute

bin/vuepress-jsdoc.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
#!/usr/bin/env node
2-
require('../')();
2+
'use strict';
3+
4+
const { program } = require('commander');
5+
6+
const { generate } = require('../cmds');
7+
const { version } = require('../package.json');
8+
9+
program.version(version).description('a CLI Tool to generate markdown files for vuepress');
10+
11+
program
12+
.description('Generate the md files')
13+
.option('-s, --source <string>', 'Source folder with .js or .ts files', './src')
14+
.option('-d, --dist <string>', 'Destination folder', './documentation')
15+
.option('-f, --folder <string>', 'Folder inside destination folder. Gets overwritten everytime', 'code')
16+
.option('-t, --title <string>', 'Title of your documentation', 'API')
17+
.option('-r, --readme <string>', 'Path to your custom readme')
18+
.option('-e, --exclude <string>', 'Pattern to exclude files/folders (Comma seperated) - *.test.js,exclude.js')
19+
.option(
20+
'-rm, --rmPattern [files...]',
21+
'Pattern when removing files. You can ex- and include files. (glob pattern)',
22+
[]
23+
)
24+
.option('-p, --partials [files...]', 'jsdoc2markdown partial templates (overwrites default ones)', [])
25+
.option('-c, --jsDocConfigPath <string>', 'Path to jsdoc config')
26+
.action(generate);
27+
28+
program.parse(process.argv);

cmds/index.js

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,30 @@ const statistics = {};
1818
const statusTypes = {
1919
success: 'green',
2020
error: 'red',
21-
exclude: 'blue',
21+
exclude: 'blueBright',
2222
empty: 'yellow'
2323
};
2424

2525
const extensions = ['.ts', '.js', '.tsx', '.jsx', '.vue'];
2626

27+
/**A
28+
* Add status and count to result
29+
* @param {string} file
30+
* @param {string} status
31+
* @param {boolean} isFolder
32+
*/
33+
const addToStatistics = (file, status, isFolder = false) => {
34+
const extension = !isFolder ? getExtension(file) : 'folder';
35+
36+
if (!statistics[extension]) {
37+
statistics[extension] = Object.keys(statusTypes).reduce((before, curr) => ({ ...before, [curr]: 0 }), {});
38+
}
39+
statistics[extension][status]++;
40+
};
41+
2742
/**
2843
* Default command that generate md files
29-
* @param {object} argv Arguments passed by yargs
44+
* @param {object} argv passed arguments
3045
*/
3146
async function generate(argv) {
3247
const exclude = (argv.exclude && argv.exclude.split(',')) || [argv.exclude || ''];
@@ -41,15 +56,6 @@ async function generate(argv) {
4156
// remove docs folder, except README.md
4257
const deletedPaths = await del([`${docsFolder}/**/*`, `!${docsFolder}/README.md`, ...rmPattern]);
4358

44-
const addToStatistics = (file, status, isFolder = false) => {
45-
const extension = !isFolder ? getExtension(file) : 'folder';
46-
47-
if (!statistics[extension]) {
48-
statistics[extension] = Object.keys(statusTypes).reduce((before, curr) => ({ ...before, [curr]: 0 }), {});
49-
}
50-
statistics[extension][status]++;
51-
};
52-
5359
/**
5460
* Read all files in directory
5561
* @param {string} folder
@@ -72,8 +78,8 @@ async function generate(argv) {
7278

7379
// iterate through all files in folder
7480
await asyncForEach(files, async file => {
75-
if (exclude && mm.contains(`${folder}/${file}`, exclude)) {
76-
console.log(chalk.black.bgBlue.bold(' EXCLUDE '), `${folder}/${file}`);
81+
if (exclude && mm.contains(`${chalk.dim(folder)}/${file}`, exclude)) {
82+
console.log(chalk.reset.inverse.bold.blueBright(' EXCLUDE '), `${chalk.dim(folder)}/${chalk.bold(file)}`);
7783

7884
addToStatistics(file, 'exclude');
7985
return;
@@ -100,9 +106,12 @@ async function generate(argv) {
100106
}
101107

102108
addToStatistics(file, 'success', true);
103-
} catch (err) {
104-
console.log(err);
105-
console.log(chalk.yellow('cannot create folder, because it already exists'), `${folderPath}/${file}`);
109+
} catch (error) {
110+
console.log(error.message);
111+
console.log(
112+
chalk.yellow('cannot create folder, because it already exists'),
113+
`${chalk.dim(folderPath)}/${file}`
114+
);
106115

107116
addToStatistics(file, 'error', true);
108117
}
@@ -114,11 +123,7 @@ async function generate(argv) {
114123
});
115124

116125
// read files from subfolder
117-
await readFiles(
118-
`${folder}/${file}`,
119-
depth + 1,
120-
tree.filter(treeItem => file === treeItem.name)[0].children
121-
);
126+
await readFiles(`${folder}/${file}`, depth + 1, tree.filter(treeItem => file === treeItem.name)[0].children);
122127
}
123128
// Else branch accessed when file is not a folder
124129
else {
@@ -147,17 +152,15 @@ async function generate(argv) {
147152
const isConfigExclude = error.message.includes('no input files');
148153

149154
console.log(
150-
chalk.black.bgRed.bold(isConfigExclude ? ' EXCLUDE BY CONFIG ' : ' ERROR '),
151-
`${folder}/${file} -> ${folderPath}/${fileName}.md`
155+
chalk.reset.inverse.bold.red(isConfigExclude ? ' EXCLUDE BY CONFIG ' : ' ERROR '),
156+
`${chalk.dim(folder)}/${chalk.bold(file)} \u2192 ${chalk.dim(folderPath)}/${chalk.bold(
157+
fileName + '.md'
158+
)}`
152159
);
153160

154161
if (!isConfigExclude) {
155162
console.log(error.message);
156163

157-
if (process.env.CI || argv.ci) {
158-
throw new Error(error);
159-
}
160-
161164
addToStatistics(file, 'error');
162165
}
163166
}
@@ -166,7 +169,12 @@ async function generate(argv) {
166169
if (mdFileData) {
167170
const { frontmatter, attributes } = parseVuepressComment(fileData);
168171

169-
console.log(chalk.black.bgGreen.bold(' SUCCESS '), `${folder}/${file} -> ${folderPath}/${fileName}.md`);
172+
console.log(
173+
chalk.reset.inverse.bold.green(' SUCCESS '),
174+
`${chalk.dim(folder)}/${chalk.bold(file)} \u2192 ${chalk.dim(folderPath)}/${chalk.bold(
175+
fileName + '.md'
176+
)}`
177+
);
170178

171179
let fileContent = '---\n';
172180

@@ -201,7 +209,12 @@ async function generate(argv) {
201209

202210
addToStatistics(file, 'success');
203211
} else {
204-
console.log(chalk.black.bgYellow.bold(' EMPTY '), `${folder}/${file} -> ${folderPath}/${fileName}.md`);
212+
console.log(
213+
chalk.reset.inverse.bold.yellow(' EMPTY '),
214+
`${chalk.dim(folder)}/${chalk.bold(file)} \u2192 ${chalk.dim(folderPath)}/${chalk.bold(
215+
fileName + '.md'
216+
)}`
217+
);
205218

206219
addToStatistics(file, 'empty');
207220
}
@@ -214,11 +227,7 @@ async function generate(argv) {
214227
if (error.code === 'ENOENT') {
215228
console.log('cannot find source folder');
216229
} else {
217-
console.log(error);
218-
}
219-
220-
if (process.env.CI || argv.ci) {
221-
throw new Error(error);
230+
console.log(error.message);
222231
}
223232
}
224233
};
@@ -263,13 +272,13 @@ async function generate(argv) {
263272

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

266-
//
267-
const maxExtLength = Math.max.apply(
268-
null,
269-
Object.keys(statistics).map(w => w.length)
270-
);
275+
// get longest type string
276+
const maxExtLength = Math.max.apply(null, Object.keys(statistics).map(w => w.length));
271277

272278
console.log(`\n${Array(maxExtLength + maxExtLength / 2).join('-')}`);
279+
280+
const errorCount = Object.keys(statistics).reduce((b, c) => b + statistics[c].error, 0);
281+
// iterate trough stats
273282
Object.entries(statistics)
274283
.sort()
275284
.forEach(([extension, types]) => {
@@ -286,7 +295,10 @@ async function generate(argv) {
286295
)}| ${content} - ${total} total`
287296
);
288297
});
298+
289299
console.log(`${Array(maxExtLength + maxExtLength / 2).join('-')}\nTime: ${resultTime}s\n`);
300+
301+
process.exit(errorCount ? 1 : 0);
290302
});
291303
}
292304

example/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
```bash
44
# Install dependencies
5-
yarn
5+
npm install
66

77
# Run the CLI
88
vuepress-jsdoc
99

1010
# Generate docs
11-
yarn run docs
11+
npm run docs
1212

1313
# Run dev server
14-
yarn run dev
14+
npm run dev
1515

1616
# Generate dist folder
17-
yarn run build
17+
npm run build
1818
```

helpers/vue-docgen-to-markdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function generateTags({ tags }) {
1919
}
2020

2121
function fileContent() {
22-
let contentArray = [];
22+
const contentArray = [];
2323
let line = 0;
2424

2525
return {

index.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)