Skip to content

Commit

Permalink
fix: cleanup .extractinator dir if it's unused
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdevv committed Dec 22, 2023
1 parent c6a1b86 commit 3b23557
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-mayflies-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'extractinator': patch
---

fix: cleanup .extractinator dir if it's unused
9 changes: 3 additions & 6 deletions src/emit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extname, relative, resolve } from 'node:path'
import { rm, mkdir } from 'node:fs/promises'
import { createRequire } from 'node:module'
import { get_temp_dir } from './utils/temp'
import { DEBUG_MODE } from './utils/env'
import { b, d, l } from './utils/log'
import { emitDts } from 'svelte2tsx'
Expand All @@ -11,14 +12,10 @@ const require = createRequire(import.meta.url)

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

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

//? [re]create the TEMP_DIR.
await rm(TEMP_DIR, { force: true, recursive: true })
await mkdir(TEMP_DIR, { recursive: true })

//? Use svelte2tsx to generate the dts files for Svelte/TS/JS.
await emitDts({
svelteShimsPath: require.resolve('svelte2tsx/svelte-shims-v4.d.ts'),
Expand Down Expand Up @@ -66,7 +63,7 @@ export async function emit_dts(input: string) {

return {
dts_file_map,
async cleanup() {
async cleanup_dts() {
if (DEBUG_MODE) return
await rm(TEMP_DIR, { recursive: true })
},
Expand Down
6 changes: 4 additions & 2 deletions src/extractinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { l, b, n, o, g, r, d, bd } from './utils/log'
import { parseSvelteFile } from './files/svelte'
import { parseTSFile } from './files/typescript'
import { createTSDocParser } from './comments'
import { clean_temp } from './utils/temp'
import { Project } from 'ts-morph'
import { emit_dts } from './emit'

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

const { dts_file_map, cleanup } = await emit_dts(options.input)
const { dts_file_map, cleanup_dts } = await emit_dts(options.input)

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

l(d('cleaning up...'))
await cleanup()
await cleanup_dts()
await clean_temp()

n(2)
l(bd(' Summary '))
Expand Down
25 changes: 25 additions & 0 deletions src/utils/temp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { rm, mkdir, readdir } from 'node:fs/promises'
import { resolve } from 'path'

/**
* Create a temporary directory under ".extractinator" with the given name.
* @returns The absolute path to the temp dir.
*/
export async function get_temp_dir(name: string) {
const path = resolve(`.extractinator/${name}`)

//? Cleanup the temp dir
await rm(path, { force: true, recursive: true })
await mkdir(path, { recursive: true })

return path
}

export async function clean_temp() {
const TEMP_ROOT = resolve('.extractinator')

//? Remove the .extractinator dir if it's empty.
if ((await readdir(TEMP_ROOT)).length == 0) {
await rm(TEMP_ROOT, { recursive: true })
}
}

0 comments on commit 3b23557

Please sign in to comment.