Skip to content

Commit 3b23557

Browse files
committed
fix: cleanup .extractinator dir if it's unused
1 parent c6a1b86 commit 3b23557

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

.changeset/lucky-mayflies-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'extractinator': patch
3+
---
4+
5+
fix: cleanup .extractinator dir if it's unused

src/emit.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { extname, relative, resolve } from 'node:path'
22
import { rm, mkdir } from 'node:fs/promises'
33
import { createRequire } from 'node:module'
4+
import { get_temp_dir } from './utils/temp'
45
import { DEBUG_MODE } from './utils/env'
56
import { b, d, l } from './utils/log'
67
import { emitDts } from 'svelte2tsx'
@@ -11,14 +12,10 @@ const require = createRequire(import.meta.url)
1112

1213
export async function emit_dts(input: string) {
1314
//? Generate a unique TEMP_DIR for this instance of extractinator.
14-
const TEMP_DIR = resolve(`.extractinator/dts-${Date.now()}`)
15+
const TEMP_DIR = await get_temp_dir(`dts-${Date.now()}`)
1516

1617
l(d(`Writing ${b('dts')} files to "${b(TEMP_DIR)}"\n`))
1718

18-
//? [re]create the TEMP_DIR.
19-
await rm(TEMP_DIR, { force: true, recursive: true })
20-
await mkdir(TEMP_DIR, { recursive: true })
21-
2219
//? Use svelte2tsx to generate the dts files for Svelte/TS/JS.
2320
await emitDts({
2421
svelteShimsPath: require.resolve('svelte2tsx/svelte-shims-v4.d.ts'),
@@ -66,7 +63,7 @@ export async function emit_dts(input: string) {
6663

6764
return {
6865
dts_file_map,
69-
async cleanup() {
66+
async cleanup_dts() {
7067
if (DEBUG_MODE) return
7168
await rm(TEMP_DIR, { recursive: true })
7269
},

src/extractinator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { l, b, n, o, g, r, d, bd } from './utils/log'
66
import { parseSvelteFile } from './files/svelte'
77
import { parseTSFile } from './files/typescript'
88
import { createTSDocParser } from './comments'
9+
import { clean_temp } from './utils/temp'
910
import { Project } from 'ts-morph'
1011
import { emit_dts } from './emit'
1112

1213
export async function extractinator(options: ExtractinatorOptions) {
1314
//? ts-morph project
1415
const project = new Project()
1516

16-
const { dts_file_map, cleanup } = await emit_dts(options.input)
17+
const { dts_file_map, cleanup_dts } = await emit_dts(options.input)
1718

1819
//? Load all the generated .d.ts files
1920
for (const dts_path of dts_file_map.keys()) {
@@ -92,7 +93,8 @@ export async function extractinator(options: ExtractinatorOptions) {
9293
}
9394

9495
l(d('cleaning up...'))
95-
await cleanup()
96+
await cleanup_dts()
97+
await clean_temp()
9698

9799
n(2)
98100
l(bd(' Summary '))

src/utils/temp.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { rm, mkdir, readdir } from 'node:fs/promises'
2+
import { resolve } from 'path'
3+
4+
/**
5+
* Create a temporary directory under ".extractinator" with the given name.
6+
* @returns The absolute path to the temp dir.
7+
*/
8+
export async function get_temp_dir(name: string) {
9+
const path = resolve(`.extractinator/${name}`)
10+
11+
//? Cleanup the temp dir
12+
await rm(path, { force: true, recursive: true })
13+
await mkdir(path, { recursive: true })
14+
15+
return path
16+
}
17+
18+
export async function clean_temp() {
19+
const TEMP_ROOT = resolve('.extractinator')
20+
21+
//? Remove the .extractinator dir if it's empty.
22+
if ((await readdir(TEMP_ROOT)).length == 0) {
23+
await rm(TEMP_ROOT, { recursive: true })
24+
}
25+
}

0 commit comments

Comments
 (0)