Skip to content

Commit

Permalink
feat(result): print out result of file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ph1p committed Sep 1, 2021
1 parent 53c423d commit c99cf50
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 51 deletions.
32 changes: 32 additions & 0 deletions src/__tests__/file-structure.test.ts
Original file line number Diff line number Diff line change
@@ -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' }
]);
});
});
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import chalk from 'chalk';
import del from 'del';
import mkdirp from 'mkdirp';

Expand Down Expand Up @@ -73,7 +74,16 @@ export const generate = async (argv: Record<string, string>) => {
}
}

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);

Expand Down
138 changes: 88 additions & 50 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 (
Expand All @@ -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
};
};
Expand All @@ -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<ParseReturn | null>) => {
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
};
};

0 comments on commit c99cf50

Please sign in to comment.