Skip to content

Commit 67ae226

Browse files
committed
Added temp for handling temp files.
Updated tests.
1 parent 4616371 commit 67ae226

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
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.0",
2526
"@types/node": "17.0.19",
27+
"@types/temp": "0.9.1",
2628
"jest": "27.5.1",
2729
"ts-jest": "27.1.3",
2830
"typescript": "4.5.5"

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ describe('jsdoc parser', () => {
44
it('parse example jsdoc header', async () => {
55
await expect(
66
parseString(
7-
'cache/',
8-
'utils.js',
7+
'lib/utils/index.js',
98
`/**
109
* Finds file in path.
1110
*
@@ -33,7 +32,7 @@ const getRootDirPath = (url) => {}`
3332
)
3433
).resolves.toEqual(
3534
expect.objectContaining({
36-
path: 'utils.js',
35+
path: 'lib/utils/index.js',
3736
methods: expect.arrayContaining([
3837
expect.objectContaining({
3938
fullName: 'findFileInPath(input, fileName)',
@@ -55,7 +54,7 @@ const getRootDirPath = (url) => {}`
5554
types: ['Promise.<(string|null)>']
5655
})
5756
]),
58-
slug: 'utils-js-findfileinpath'
57+
slug: 'lib-utils-index-js-findfileinpath'
5958
}),
6059
expect.objectContaining({
6160
fullName: 'getRootDirPath(url)',
@@ -73,14 +72,14 @@ const getRootDirPath = (url) => {}`
7372
types: ['string']
7473
})
7574
]),
76-
slug: 'utils-js-getrootdirpath'
75+
slug: 'lib-utils-index-js-getrootdirpath'
7776
})
7877
])
7978
})
8079
);
8180
});
8281
it('parse empty string', async () => {
83-
await expect(parseString('cache/', 'test.js', '')).resolves.toEqual(
82+
await expect(parseString('test.js', '')).resolves.toEqual(
8483
expect.objectContaining({
8584
path: 'test.js',
8685
methods: []

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { dirname, join } from 'path';
66

77
import spawn from 'spawn-please';
88

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

1113
import { File } from 'doxdox-core';
@@ -97,10 +99,14 @@ const parser = async (cwd: string, path: string): Promise<File> => {
9799
};
98100

99101
export const parseString = async (
100-
tempDir: string,
101102
path: string,
102-
content: string
103+
content: string,
104+
cacheDir = './cache'
103105
): Promise<File> => {
106+
temp.track();
107+
108+
const tempDir = await temp.mkdir({ prefix: 'doxdox-', dir: cacheDir });
109+
104110
const tempPath = `${tempDir}/${path}`;
105111

106112
await fs.mkdir(dirname(tempPath), { recursive: true });
@@ -109,12 +115,7 @@ export const parseString = async (
109115

110116
const file = await parser(tempDir, path);
111117

112-
try {
113-
await fs.unlink(tempPath);
114-
await fs.rm(tempDir, { recursive: true });
115-
} catch (error) {
116-
console.log(error);
117-
}
118+
await temp.cleanup();
118119

119120
return file;
120121
};

0 commit comments

Comments
 (0)