Skip to content

Commit c1b9e16

Browse files
committed
test: update tests and add file check in vupress config generator
1 parent 91279bb commit c1b9e16

File tree

6 files changed

+43
-43
lines changed

6 files changed

+43
-43
lines changed

example/documentation/code/config.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/list-folder.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fs, vol } from 'memfs';
22

33
import { listFolder } from '../lib/list-folder';
44

5-
jest.mock('fs');
5+
jest.mock('fs', () => fs);
66
jest.mock('fs/promises', () => fs.promises);
77

88
describe('test file-structure', () => {
@@ -30,12 +30,12 @@ describe('test file-structure', () => {
3030
]);
3131

3232
expect((await listFolder('./src', [])).tree).toEqual([
33-
{ fullPath: 'src/file1', name: 'file1', path: '/file1' },
34-
{ fullPath: 'src/file2', name: 'file2', path: '/file2' },
33+
{ fullPath: 'src/file1', name: 'file1', path: '/file1', ext: '.js' },
34+
{ fullPath: 'src/file2', name: 'file2', path: '/file2', ext: '.ts' },
3535
{
3636
children: [
37-
{ fullPath: 'src/lib/file3', name: 'file3', path: '/file3' },
38-
{ fullPath: 'src/lib/_index', name: '_index', path: '/_index' }
37+
{ fullPath: 'src/lib/file3', name: 'file3', path: '/file3', ext: '.vue' },
38+
{ fullPath: 'src/lib/_index', name: '_index', path: '/_index', ext: '.js' }
3939
],
4040
name: 'lib'
4141
}

src/__tests__/sidebar.test.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
import { generateVueSidebar } from '../lib/vue-sidebar';
22

3+
jest.mock('fs', () => ({
4+
existsSync: () => true
5+
}));
6+
37
describe('test sidebar', () => {
48
test('generateVueSidebar should return valid vue config', () => {
59
const codeFolder = 'test_folder';
610
const title = 'test_folder';
711
const srcFolder = 'test_folder';
12+
const docsFolder = 'test_folder';
813

914
const fileTree = [
10-
{ name: 'class', path: '/class', fullPath: './documentation/test_folder/class' },
15+
{ fullPath: 'src/file1', name: 'file1', path: '/file1', ext: '.js' },
16+
{ fullPath: 'src/file2', name: 'file2', path: '/file2', ext: '.ts' },
1117
{
12-
name: 'lib',
1318
children: [
14-
{ name: 'test1', path: '/test1', fullPath: './documentation/test_folder/test1' },
15-
{
16-
name: 'test2',
17-
path: '/test2',
18-
fullPath: './documentation/test_folder/test2',
19-
children: [
20-
{ name: 'test3', path: '/test3', fullPath: './documentation/test_folder/test2/test3' },
21-
{ name: 'test4', path: '/test4', fullPath: './documentation/test_folder/test2/test4' }
22-
]
23-
}
24-
]
25-
},
26-
{ name: 'test', path: '/test', fullPath: './documentation/test_folder/test' },
27-
{ name: 'tests', children: [] }
19+
{ fullPath: 'src/lib/file3', name: 'file3', path: '/file3', ext: '.vue' },
20+
{ fullPath: 'src/lib/_index', name: '_index', path: '/_index', ext: '.js' }
21+
],
22+
name: 'lib'
23+
}
2824
];
2925

30-
const sidebar = generateVueSidebar({ fileTree, codeFolder, srcFolder, title });
26+
const sidebar = generateVueSidebar({ fileTree, codeFolder, docsFolder, srcFolder, title });
3127

3228
const result = {
3329
[`/${codeFolder}/`]: [
34-
{ title, collapsable: false, children: [['', '::vuepress-jsdoc-title::'], 'class', 'test'] },
30+
{ title, collapsable: false, children: [['', '::vuepress-jsdoc-title::'], 'file1', 'file2'] },
3531
{
3632
title: 'lib',
3733
collapsable: false,
38-
children: ['./documentation/test1', './documentation/test2/test3', './documentation/test2/test4']
34+
children: ['src/lib/file3', 'src/lib/_index']
3935
}
4036
]
4137
};

src/index.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ export const generate = async (argv: Record<string, string>) => {
3636

3737
const parsePromises: Promise<any>[] = [];
3838

39-
// write vuepress sidebar
40-
await fs.writeFile(
41-
`${docsFolder}/config.js`,
42-
`exports.fileTree=${JSON.stringify(tree)};exports.sidebarTree = (title = 'Mainpage') => (${JSON.stringify(
43-
generateVueSidebar({
44-
fileTree: tree,
45-
srcFolder,
46-
codeFolder,
47-
title
48-
})
49-
).replace('::vuepress-jsdoc-title::', '"+title+"')});`
50-
);
51-
5239
// print out all files
5340
for (const file of paths) {
5441
if (!file.isDir) {
@@ -86,6 +73,20 @@ export const generate = async (argv: Record<string, string>) => {
8673
// wait unitl all files resolved
8774
const result = await Promise.all(parsePromises);
8875

76+
// write vuepress sidebar
77+
await fs.writeFile(
78+
`${docsFolder}/config.js`,
79+
`exports.fileTree=${JSON.stringify(tree)};exports.sidebarTree = (title = 'Mainpage') => (${JSON.stringify(
80+
generateVueSidebar({
81+
fileTree: tree,
82+
srcFolder,
83+
docsFolder,
84+
codeFolder,
85+
title
86+
})
87+
).replace('::vuepress-jsdoc-title::', '"+title+"')});`
88+
);
89+
8990
// print stats
9091
for (const entry of result.flat()) {
9192
if (!entry.file) continue;

src/lib/list-folder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const listFolder = async (srcPath: string, exclude: string[] = [], mainPa
3535

3636
const treeEntry: FileTree = {
3737
name,
38-
...(!isDir ? { path: `/${name}`, fullPath: path.join(srcPath, name) } : {})
38+
...(!isDir ? { path: `/${name}`, fullPath: path.join(srcPath, name), ext } : {})
3939
};
4040

4141
tree.push(treeEntry);

src/lib/vue-sidebar.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import { join } from 'path';
23
interface Node {
34
name: string;
45
children: any[];
@@ -8,11 +9,13 @@ export const generateVueSidebar = ({
89
fileTree,
910
codeFolder,
1011
srcFolder,
12+
docsFolder,
1113
title
1214
}: {
1315
fileTree: any;
1416
codeFolder: string;
1517
srcFolder: string;
18+
docsFolder: string;
1619
title: string;
1720
}) => {
1821
let rootFiles = [['', '::vuepress-jsdoc-title::']];
@@ -24,10 +27,10 @@ export const generateVueSidebar = ({
2427
let newChildren: any[] = [];
2528

2629
for (const child of children) {
27-
if (fs.existsSync(child.fullPath)) {
28-
if (child.children && child.children.length > 0) {
29-
newChildren = newChildren.concat(buildChildren(child.children, child.name, depth + 1));
30-
} else if (child.fullPath) {
30+
if (child.children && child.children.length > 0) {
31+
newChildren = newChildren.concat(buildChildren(child.children, child.name, depth + 1));
32+
} else if (child.fullPath) {
33+
if (fs.existsSync(join(docsFolder, child.fullPath.replace(srcFolder, '')) + '.md')) {
3134
newChildren.push(child.fullPath.replace(`${srcFolder}/`, ''));
3235
}
3336
}

0 commit comments

Comments
 (0)