Skip to content

Commit

Permalink
test: update tests and add file check in vupress config generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ph1p committed Sep 2, 2021
1 parent 91279bb commit c1b9e16
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion example/documentation/code/config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/__tests__/list-folder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fs, vol } from 'memfs';

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

jest.mock('fs');
jest.mock('fs', () => fs);
jest.mock('fs/promises', () => fs.promises);

describe('test file-structure', () => {
Expand Down Expand Up @@ -30,12 +30,12 @@ describe('test file-structure', () => {
]);

expect((await listFolder('./src', [])).tree).toEqual([
{ fullPath: 'src/file1', name: 'file1', path: '/file1' },
{ fullPath: 'src/file2', name: 'file2', path: '/file2' },
{ fullPath: 'src/file1', name: 'file1', path: '/file1', ext: '.js' },
{ fullPath: 'src/file2', name: 'file2', path: '/file2', ext: '.ts' },
{
children: [
{ fullPath: 'src/lib/file3', name: 'file3', path: '/file3' },
{ fullPath: 'src/lib/_index', name: '_index', path: '/_index' }
{ fullPath: 'src/lib/file3', name: 'file3', path: '/file3', ext: '.vue' },
{ fullPath: 'src/lib/_index', name: '_index', path: '/_index', ext: '.js' }
],
name: 'lib'
}
Expand Down
34 changes: 15 additions & 19 deletions src/__tests__/sidebar.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import { generateVueSidebar } from '../lib/vue-sidebar';

jest.mock('fs', () => ({
existsSync: () => true
}));

describe('test sidebar', () => {
test('generateVueSidebar should return valid vue config', () => {
const codeFolder = 'test_folder';
const title = 'test_folder';
const srcFolder = 'test_folder';
const docsFolder = 'test_folder';

const fileTree = [
{ name: 'class', path: '/class', fullPath: './documentation/test_folder/class' },
{ fullPath: 'src/file1', name: 'file1', path: '/file1', ext: '.js' },
{ fullPath: 'src/file2', name: 'file2', path: '/file2', ext: '.ts' },
{
name: 'lib',
children: [
{ name: 'test1', path: '/test1', fullPath: './documentation/test_folder/test1' },
{
name: 'test2',
path: '/test2',
fullPath: './documentation/test_folder/test2',
children: [
{ name: 'test3', path: '/test3', fullPath: './documentation/test_folder/test2/test3' },
{ name: 'test4', path: '/test4', fullPath: './documentation/test_folder/test2/test4' }
]
}
]
},
{ name: 'test', path: '/test', fullPath: './documentation/test_folder/test' },
{ name: 'tests', children: [] }
{ fullPath: 'src/lib/file3', name: 'file3', path: '/file3', ext: '.vue' },
{ fullPath: 'src/lib/_index', name: '_index', path: '/_index', ext: '.js' }
],
name: 'lib'
}
];

const sidebar = generateVueSidebar({ fileTree, codeFolder, srcFolder, title });
const sidebar = generateVueSidebar({ fileTree, codeFolder, docsFolder, srcFolder, title });

const result = {
[`/${codeFolder}/`]: [
{ title, collapsable: false, children: [['', '::vuepress-jsdoc-title::'], 'class', 'test'] },
{ title, collapsable: false, children: [['', '::vuepress-jsdoc-title::'], 'file1', 'file2'] },
{
title: 'lib',
collapsable: false,
children: ['./documentation/test1', './documentation/test2/test3', './documentation/test2/test4']
children: ['src/lib/file3', 'src/lib/_index']
}
]
};
Expand Down
27 changes: 14 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ export const generate = async (argv: Record<string, string>) => {

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

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

// print out all files
for (const file of paths) {
if (!file.isDir) {
Expand Down Expand Up @@ -86,6 +73,20 @@ export const generate = async (argv: Record<string, string>) => {
// wait unitl all files resolved
const result = await Promise.all(parsePromises);

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

// print stats
for (const entry of result.flat()) {
if (!entry.file) continue;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/list-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const listFolder = async (srcPath: string, exclude: string[] = [], mainPa

const treeEntry: FileTree = {
name,
...(!isDir ? { path: `/${name}`, fullPath: path.join(srcPath, name) } : {})
...(!isDir ? { path: `/${name}`, fullPath: path.join(srcPath, name), ext } : {})
};

tree.push(treeEntry);
Expand Down
11 changes: 7 additions & 4 deletions src/lib/vue-sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import { join } from 'path';
interface Node {
name: string;
children: any[];
Expand All @@ -8,11 +9,13 @@ export const generateVueSidebar = ({
fileTree,
codeFolder,
srcFolder,
docsFolder,
title
}: {
fileTree: any;
codeFolder: string;
srcFolder: string;
docsFolder: string;
title: string;
}) => {
let rootFiles = [['', '::vuepress-jsdoc-title::']];
Expand All @@ -24,10 +27,10 @@ export const generateVueSidebar = ({
let newChildren: any[] = [];

for (const child of children) {
if (fs.existsSync(child.fullPath)) {
if (child.children && child.children.length > 0) {
newChildren = newChildren.concat(buildChildren(child.children, child.name, depth + 1));
} else if (child.fullPath) {
if (child.children && child.children.length > 0) {
newChildren = newChildren.concat(buildChildren(child.children, child.name, depth + 1));
} else if (child.fullPath) {
if (fs.existsSync(join(docsFolder, child.fullPath.replace(srcFolder, '')) + '.md')) {
newChildren.push(child.fullPath.replace(`${srcFolder}/`, ''));
}
}
Expand Down

0 comments on commit c1b9e16

Please sign in to comment.