Skip to content

Commit 28c7e7b

Browse files
authored
Merge pull request #148 from docsbydoxdox/feature/jsdoc-parse-contents
[feat] Added a parseString method for parsing strings.
2 parents 8ca1637 + 1fc27c2 commit 28c7e7b

File tree

4 files changed

+123
-2
lines changed

4 files changed

+123
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
node_modules/
22

3+
cache/
4+
35
coverage/
46

57
dist/

packages/doxdox-parser-jsdoc/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"dependencies": {
1212
"jsdoc": "3.6.10",
13-
"spawn-please": "1.0.0"
13+
"spawn-please": "1.0.0",
14+
"temp": "0.9.4"
1415
},
1516
"peerDependencies": {
1617
"doxdox-core": "4.0.0-preview.8"
@@ -23,6 +24,7 @@
2324
"devDependencies": {
2425
"@types/jest": "27.4.1",
2526
"@types/node": "17.0.21",
27+
"@types/temp": "0.9.1",
2628
"jest": "27.5.1",
2729
"ts-jest": "27.1.3",
2830
"typescript": "4.5.5"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { parseString } from './index';
2+
3+
describe('jsdoc parser', () => {
4+
it('parse example jsdoc headers', async () => {
5+
await expect(
6+
parseString(
7+
'lib/utils/index.js',
8+
`/**
9+
* Finds file in path.
10+
*
11+
* console.log(await findFileInPath('./', 'package.json'));
12+
* console.log(await findFileInPath('../', 'package.json'));
13+
* console.log(await findFileInPath('~/git/github/doxdox/', '.package.json'));
14+
*
15+
* @param {string} [input] Directory to check for file.
16+
* @param {string?} [fileName = 'package.json'] File name to check for.
17+
* @return {Promise<string | null>} Path to package.json file.
18+
* @public
19+
*/
20+
21+
const findFileInPath = async (input, fileName = 'package.json') => {};
22+
23+
/**
24+
* Get the root directory of the package, supplied path or URL.
25+
*
26+
* @param {string?} [url] Optional path or URL.
27+
* @return {string} Directory path.
28+
* @public
29+
*/
30+
31+
const getRootDirPath = (url) => {};`
32+
)
33+
).resolves.toEqual(
34+
expect.objectContaining({
35+
path: 'lib/utils/index.js',
36+
methods: expect.arrayContaining([
37+
expect.objectContaining({
38+
fullName: 'findFileInPath(input, fileName)',
39+
name: 'findFileInPath',
40+
params: expect.arrayContaining([
41+
expect.objectContaining({
42+
name: 'input',
43+
types: ['string']
44+
}),
45+
expect.objectContaining({
46+
name: 'fileName',
47+
types: ['string']
48+
})
49+
]),
50+
private: false,
51+
returns: expect.arrayContaining([
52+
expect.objectContaining({
53+
name: undefined,
54+
types: ['Promise.<(string|null)>']
55+
})
56+
]),
57+
slug: 'lib-utils-index-js-findfileinpath'
58+
}),
59+
expect.objectContaining({
60+
fullName: 'getRootDirPath(url)',
61+
name: 'getRootDirPath',
62+
params: expect.arrayContaining([
63+
expect.objectContaining({
64+
name: 'url',
65+
types: ['string']
66+
})
67+
]),
68+
private: false,
69+
returns: expect.arrayContaining([
70+
expect.objectContaining({
71+
name: undefined,
72+
types: ['string']
73+
})
74+
]),
75+
slug: 'lib-utils-index-js-getrootdirpath'
76+
})
77+
])
78+
})
79+
);
80+
});
81+
it('parse empty string', async () => {
82+
await expect(parseString('test.js', '')).resolves.toEqual(
83+
expect.objectContaining({
84+
path: 'test.js',
85+
methods: []
86+
})
87+
);
88+
});
89+
});

packages/doxdox-parser-jsdoc/src/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
import { promises as fs } from 'fs';
2+
13
import { platform } from 'os';
24

35
import { dirname, join } from 'path';
46

57
import spawn from 'spawn-please';
68

9+
import temp from 'temp';
10+
711
import { findParentNodeModules, sanitizePath, slugify } from 'doxdox-core';
812

913
import { File } from 'doxdox-core';
1014

1115
import { Jsdoc } from './types';
1216

13-
export default async (cwd: string, path: string): Promise<File> => {
17+
const parser = async (cwd: string, path: string): Promise<File> => {
1418
try {
1519
const nodeModulesDir = await findParentNodeModules(
1620
dirname(sanitizePath(import.meta.url))
@@ -93,3 +97,27 @@ export default async (cwd: string, path: string): Promise<File> => {
9397

9498
return { path, methods: [] };
9599
};
100+
101+
export const parseString = async (
102+
path: string,
103+
content: string,
104+
cacheDir = './cache'
105+
): Promise<File> => {
106+
temp.track();
107+
108+
const tempDir = await temp.mkdir({ prefix: 'doxdox-', dir: cacheDir });
109+
110+
const tempPath = join(tempDir, path);
111+
112+
await fs.mkdir(dirname(tempPath), { recursive: true });
113+
114+
await fs.writeFile(tempPath, content);
115+
116+
const file = await parser(tempDir, path);
117+
118+
await temp.cleanup();
119+
120+
return file;
121+
};
122+
123+
export default parser;

0 commit comments

Comments
 (0)