Skip to content

Commit

Permalink
docs: fix watch-mode and add more informations
Browse files Browse the repository at this point in the history
  • Loading branch information
ph1p committed Sep 3, 2021
1 parent 453019f commit c356bcc
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 72 deletions.
8 changes: 5 additions & 3 deletions documentation/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ module.exports = {
require('../../dist/index.js').default,
{
folder: 'code',
jsDocConfigPath: './jsdoc.json',
source: './src',
source: './dist',
dist: './documentation',
title: 'API'
title: 'API',
partials: ['./example/partials/*.hbs'],
readme: './README.md',
exclude: '**/*.d.ts,**/interfaces.*,**/constants.*,**/cmds.*'
}
]
],
Expand Down
14 changes: 1 addition & 13 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkExtension, getExtension, getFilename, asyncForEach } from '../lib/utils';
import { checkExtension, getExtension, getFilename } from '../lib/utils';

describe('test utils', () => {
test('getExtension should return true', () => {
Expand All @@ -19,16 +19,4 @@ describe('test utils', () => {
// @ts-expect-error check empty method
expect(getFilename()).toBe('');
});
test('asyncForEach should run array async', async () => {
const promise1 = Promise.resolve(1);
const promise2 = Promise.resolve(2);

const results: number[] = [];

await asyncForEach([promise1, promise2], async result => {
results.push(await result);
});

expect(results).toEqual([1, 2]);
});
});
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { generateVueSidebar } from './lib/vue-sidebar';

/**
* Create the sidebar
* @param options
* @param options {object}
* @returns {Promise}
*/
const createVuepressSidebar = options =>
Expand All @@ -37,8 +37,8 @@ const createVuepressSidebar = options =>

/**
* Parse file
* @param file
* @param argv
* @param file {DirectoryFile}
* @param argv {CLIArguments}
* @returns {Promise}
*/
const parseDirectoryFile = async (file: DirectoryFile, argv: CLIArguments) => {
Expand Down Expand Up @@ -83,7 +83,7 @@ const createReadmeFile = async (argv: CLIArguments, deletedPaths?: string[]) =>

/**
* Parse all CLI arguments
* @param argv
* @param argv {CLIArguments}
* @returns {object} all arguments
*/
const parseArguments = (argv: CLIArguments) => {
Expand Down Expand Up @@ -187,6 +187,10 @@ export const generate = async (argv: CLIArguments) => {
watchFiles(argv);
};

/**
* Watch files in source folder
* @param argv {CLIArguments}
*/
const watchFiles = (argv: CLIArguments) => {
const { exclude, srcFolder, codeFolder, docsFolder, title } = parseArguments(argv);

Expand Down Expand Up @@ -230,8 +234,8 @@ const watchFiles = (argv: CLIArguments) => {

/**
* The vuepress plugins
* @param argv
* @param ctx
* @param argv {CLIArguments}
* @param ctx {object}
* @returns {object}
*/
const plugin = (argv: CLIArguments, ctx) => ({
Expand Down
8 changes: 4 additions & 4 deletions src/lib/comment-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DirectoryFile } from '../interfaces';

/**
* Search in file for @vuepress comment
* @param fileContent
* @param fileContent {string} content of given file
* @returns {object} object of found frontmatter data
*/
export const parseComment = (fileContent: string) => {
Expand Down Expand Up @@ -45,9 +45,9 @@ export const parseComment = (fileContent: string) => {
};

/**
* Helper function to get header as strctured markdown
* @param content : ;
* @param file
* Helper function to get header as structured markdown
* @param content {string} file content
* @param file {object} file object
* @returns {string} markdown header
*/
export const parseVuepressFileHeader = (content: string, file: DirectoryFile) => {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/list-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { DirectoryFile, FileTree } from '../interfaces';

/**
* Recursively traverse folders and return exluded files, a file list and a file tree.
* @param srcPath
* @param exclude
* @param mainPath
* @param tree
* @param srcPath {string} path to source dir
* @param exclude {array} exluded file patter list
* @param mainPath {string} path to hold source dir
* @param tree {object} tree array
* @returns {object} paths array, tree, excluded array
*/
export const listFolder = async (srcPath: string, exclude: string[] = [], mainPath?: string, tree: FileTree[] = []) => {
Expand Down
22 changes: 11 additions & 11 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { parseVuepressFileHeader } from './comment-parser';

/**
* Parse a typescript or javascript file
* @param file
* @param srcFolder
* @param destFolder
* @param configPath
* @param partials
* @param file {DirectoryFile}
* @param srcFolder {string}
* @param destFolder {string}
* @param configPath {string}
* @param partials {string | string[]}
* @returns {object} file data
*/
export const parseFile = async (
Expand Down Expand Up @@ -91,9 +91,9 @@ export const parseFile = async (

/**
* Parse a vue file
* @param file
* @param srcFolder
* @param destFolder
* @param file {DirectoryFile}
* @param srcFolder {string}
* @param destFolder {string}
* @returns {object} file data
*/
export const parseVueFile = async (
Expand Down Expand Up @@ -153,9 +153,9 @@ export const parseVueFile = async (

/**
* Write content on disk
* @param parseData
* @param dest
* @returns {object | null} null or type with some data of the saved file
* @param parseData {ParseReturn | null}
* @param dest {string}
* @returns {Promise | null} null or type with some data of the saved file
*/
export const writeContentToFile = async (parseData: ParseReturn | null, dest: string) => {
const root = process.cwd();
Expand Down
22 changes: 4 additions & 18 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,26 @@
*/
/**
* Get extension of file
* @param path
* @param path {string}
* @returns {string} extension of file
*/
export const getExtension = (path: string) => path.substring(path.length, path.lastIndexOf('.'));

/**
* Check if extension ist correct
* @param path
* @param extensions
* @param path {string}
* @param extensions {string[]}
* @returns {boolean}
*/
export const checkExtension = (path: string, extensions: string[]) => extensions.indexOf(getExtension(path)) >= 0;

/**
* Get filename without extension
* @param path
* @param path {string}
* @returns {string} filename
*/
export const getFilename = (path: string) =>
path
?.split('/')
?.pop()
?.substring(0, path.lastIndexOf('.')) || '';

/**
* Async foreach loop
* @param array
* @callback callback
*/
export const asyncForEach = async (
array: any[],
callback: (result: any, index: number, array: any[]) => Promise<void>
) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
24 changes: 11 additions & 13 deletions src/lib/vue-sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
*/
import fs from 'fs';
import { join } from 'path';
interface Node {
name: string;
children: any[];
}

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

/**
* Runs through the given tree structure and creates a vuepress config
* @param data Informations to build config
* @param data.fileTree tree strcture
* @param data.codeFolder ./code/ folder
* @param data.srcFolder ./src/ folder
* @param data.docsFolder ./documentation/ folder
* @param data.title title string
* @param data.fileTree {array} tree strcture
* @param data.codeFolder {string}./code/ folder
* @param data.srcFolder {string} ./src/ folder
* @param data.docsFolder {string} ./documentation/ folder
* @param data.title {string} title string
* @returns {object} returns the vuepress menu strcture
*/
export const generateVueSidebar = ({
Expand All @@ -35,9 +33,9 @@ export const generateVueSidebar = ({
title: string;
}) => {
let rootFiles = [['', '::vuepress-jsdoc-title::']];
rootFiles = rootFiles.concat(fileTree.filter((file: Node) => !file.children).map((file: Node) => file.name));
rootFiles = rootFiles.concat(fileTree.filter((file: FileTree) => !file.children).map((file: FileTree) => file.name));

const rootFolder = fileTree.filter((file: Node) => file.children && file.children.length > 0);
const rootFolder = fileTree.filter((file: FileTree) => file.children && file.children.length > 0);

const buildChildren = (children: any[], name: string, depth: number) => {
let newChildren: any[] = [];
Expand All @@ -55,10 +53,10 @@ export const generateVueSidebar = ({
return newChildren;
};

const tree = rootFolder.map((folder: Node) => ({
const tree = rootFolder.map((folder: FileTree) => ({
title: folder.name,
collapsable: false,
children: buildChildren(folder.children, folder.name, 0)
children: buildChildren(folder.children!, folder.name, 0)
}));

return {
Expand Down

0 comments on commit c356bcc

Please sign in to comment.