diff --git a/src/__tests__/file-structure.test.ts b/src/__tests__/file-structure.test.ts new file mode 100644 index 0000000..bc948f2 --- /dev/null +++ b/src/__tests__/file-structure.test.ts @@ -0,0 +1,32 @@ +import { fs, vol } from 'memfs'; + +import { listFolder } from '../lib/list-folder'; + +jest.mock('fs'); +jest.mock('fs/promises', () => fs.promises); + +describe('test file-structure', () => { + beforeEach(() => { + vol.reset(); + }); + + test('get structured file array', async () => { + vol.fromJSON( + { + 'file1.js': '', + 'file2.ts': '', + 'lib/file3.vue': '', + 'lib/index.js': '' + }, + './src' + ); + + expect(await listFolder('./src', [])).toEqual([ + { ext: '.js', folder: 'src/', isDir: false, name: 'file1', path: 'src/file1.js' }, + { ext: '.ts', folder: 'src/', isDir: false, name: 'file2', path: 'src/file2.ts' }, + { ext: '.vue', folder: 'src/lib/', isDir: false, name: 'file3', path: 'src/lib/file3.vue' }, + { ext: '.js', folder: 'src/lib/index', isDir: false, name: '_index', path: 'src/lib/index.js' }, + { isDir: true, name: 'lib', path: 'src/lib' } + ]); + }); +}); diff --git a/src/index.ts b/src/index.ts index 65729a2..61826f9 100755 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import chalk from 'chalk'; import del from 'del'; import mkdirp from 'mkdirp'; @@ -73,7 +74,16 @@ export const generate = async (argv: Record) => { } } - await Promise.all(parsePromises); + const result = await Promise.all(parsePromises); + + for (const entry of result.flat()) { + console.log( + chalk.reset.inverse.bold.green(` ${entry.type.toUpperCase()} `), + `${chalk.dim(entry.relativePathSrc)}${chalk.bold(entry.file.name + entry.file.ext)} \u2192 ${chalk.dim( + entry.relativePathDest + )}${chalk.bold(entry.file.name + '.md')}` + ); + } const resultTime = (Math.abs(startTime - +new Date()) / 1000).toFixed(2); diff --git a/src/lib/parser.ts b/src/lib/parser.ts index 349e4e4..e2df7ad 100644 --- a/src/lib/parser.ts +++ b/src/lib/parser.ts @@ -1,7 +1,7 @@ import fs from 'fs/promises'; import jsdoc2md from 'jsdoc-to-markdown'; import mkdirp from 'mkdirp'; -import path from 'path'; +import { join, resolve } from 'path'; import compileTemplates from 'vue-docgen-cli/lib/compileTemplates'; import { extractConfig } from 'vue-docgen-cli/lib/docgen'; @@ -10,9 +10,12 @@ import { DirectoryFile } from '../interfaces'; import { parseVuepressFileHeader } from './comment-parser'; interface ParseReturn { + success: boolean; dest: string; - filename: string; + file: DirectoryFile; content: string; + relativePathSrc: string; + relativePathDest: string; } export const parseFile = async ( @@ -25,34 +28,42 @@ export const parseFile = async ( if (!file.folder) return null; const root = process.cwd(); - const folderInDest = path.join(root, destFolder, file.folder.replace(srcFolder, '')); - const folderInSrc = path.join(root, file.folder); - - // render file - const content = await jsdoc2md.render({ - files: [`${path.join(folderInSrc, file.name + file.ext)}`], - configure: configPath, - partial: [ - path.resolve(__filename, '../../template/header.hbs'), - path.resolve(__filename, '../../template/main.hbs'), - ...partials - ] - }); - - if (!content) { - return null; + const relativePathDest = join(destFolder, file.folder.replace(srcFolder, '')); + const folderInDest = join(root, relativePathDest); + const folderInSrc = join(root, file.folder); + + let success = true; + let fileContent = ''; + + // parse file + try { + const content = await jsdoc2md.render({ + files: [`${join(folderInSrc, file.name + file.ext)}`], + configure: configPath, + partial: [ + resolve(__filename, '../../template/header.hbs'), + resolve(__filename, '../../template/main.hbs'), + ...partials + ] + }); + + if (content) { + fileContent = parseVuepressFileHeader( + await fs.readFile(`${join(folderInSrc, file.name + file.ext)}`, 'utf-8'), + file + ); + fileContent += content; + } + } catch { + success = false; } - let fileContent = parseVuepressFileHeader( - await fs.readFile(`${path.join(folderInSrc, file.name + file.ext)}`, 'utf-8'), - file - ); - - fileContent += content; - return { + success, + file, + relativePathDest, + relativePathSrc: file.folder, dest: folderInDest, - filename: file.name, content: fileContent }; }; @@ -65,45 +76,72 @@ export const parseVueFile = async ( if (!file.folder) return null; const root = process.cwd(); - const folderInDest = path.join(root, destFolder, file.folder.replace(srcFolder, '')); - const folderInSrc = path.join(root, file.folder); + const relativePathDest = join(destFolder, file.folder.replace(srcFolder, '')); + const folderInDest = join(root, relativePathDest); + const folderInSrc = join(root, file.folder); const config = { - ...extractConfig(path.join(root, file.folder)), + ...extractConfig(join(root, file.folder)), components: file.name + file.ext, outDir: folderInDest }; - const data = await compileTemplates( - path.join(config.componentsRoot, file.name + file.ext), - config, - file.name + file.ext - ); - - await fs.unlink(`${path.join(folderInDest, file.name)}.md`); - - if (!data.content) { - return null; + let success = true; + let fileContent = ''; + + try { + // parse file + const data = await compileTemplates( + join(config.componentsRoot, file.name + file.ext), + config, + file.name + file.ext + ); + + if (data.content) { + fileContent = parseVuepressFileHeader( + await fs.readFile(`${join(folderInSrc, file.name + file.ext)}`, 'utf-8'), + file + ); + + fileContent += data.content; + } + } catch { + success = false; } - let fileContent = parseVuepressFileHeader( - await fs.readFile(`${path.join(folderInSrc, file.name + file.ext)}`, 'utf-8'), - file - ); - - fileContent += data.content; - return { + success, + file, + relativePathDest, + relativePathSrc: file.folder, dest: folderInDest, - filename: file.name, content: fileContent }; }; export const writeContentToFile = async (file: Promise) => { const data = await file; + let type = data?.success ? 'empty' : 'error'; - if (data) { - await mkdirp(data?.dest); - await fs.writeFile(`${path.join(data.dest, data.filename)}.md`, data.content, 'utf-8'); + try { + if (data && data?.content) { + const path = `${join(data.dest, data.file.name)}.md`; + + await mkdirp(data?.dest); + await fs.writeFile(path, data.content, 'utf-8'); + + type = 'success'; + } + + return { + ...data, + type + }; + } catch { + type = 'error'; } + + return { + ...data, + type + }; };