Skip to content

Commit

Permalink
fix: support nested paths
Browse files Browse the repository at this point in the history
Co-authored-by: Willow (GHOST) <ghostdevv@users.noreply.github.com>
  • Loading branch information
braebo and ghostdevv committed Dec 22, 2023
1 parent e672f55 commit 57d1270
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-boxes-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'extractinator': patch
---

fix: support nested paths
17 changes: 11 additions & 6 deletions src/emit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, extname, relative, resolve } from 'node:path'
import { extname, relative, resolve } from 'node:path'
import { rm, mkdir } from 'node:fs/promises'
import { createRequire } from 'node:module'
import { emitDts } from 'svelte2tsx'
Expand Down Expand Up @@ -42,13 +42,18 @@ export async function emit_dts(input: string) {
throw new Error(`Unable to handle "${input_path}"`)
}

//? Construct the path the dts file will be at
//? e.g. /home/ghost/input/Test.svelte -> /home/ghost/output/Test.svelte.d.ts
//? e.g. /home/ghost/input/test.ts -> /home/ghost/output/test.d.ts
const dts_path = `${TEMP_DIR}/${basename(input_path.replace(input, ''), '.ts')}.d.ts`
//? Construct the output path for the dts file.
// e.g. /home/ghost/input/Test.svelte -> /home/ghost/output/Test.svelte.d.ts
// e.g. /home/ghost/input/foo/test.ts -> /home/ghost/output/foo/test.d.ts

const relative_path = relative(input, input_path)
const input_ext = extname(relative_path)
const output_ext = input_ext === '.svelte' ? '.svelte.d.ts' : '.d.ts'

const dts_path = resolve(TEMP_DIR, relative_path.replace(input_ext, output_ext))

if (!existsSync(dts_path)) {
console.error({ dts_path, path: input_path })
console.error({ dts_path, input_path })
throw new Error(`Unable to find dts path for "${input_path}"`)
}

Expand Down
24 changes: 6 additions & 18 deletions src/extractinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,16 @@ export async function extractinator(options: ExtractinatorOptions) {
//? Parsed Svelte/TS Files
const parsed_files: ParsedFile[] = []

//? Loop over all the loaded source files
for (const source_file of project.getSourceFiles()) {
//? Get the filename e.g. KitchenSink.svelte.d.ts
const dts_file_name = source_file.getBaseName()
const dts_path = source_file.getFilePath()
const src_path = dts_file_map.get(dts_path)!

//? Find the input file path from the dts file path
const input_file_path = dts.dts_file_map.get(source_file.getFilePath())!

const file_name = basename(input_file_path)

//? Work out the file extension, needs to be done in specific order since ".d.ts" is shared
const ext = dts_file_name.endsWith('.svelte.d.ts')
? '.svelte.d.ts'
: dts_file_name.endsWith('.d.ts')
? '.d.ts'
: null

l(`Processing File (${dim(ext)}) "${o(file_name)}"`)
const is_svelte = extname(src_path) === '.svelte'
const is_ts = extname(src_path) === '.ts'

const ctx: FileParserContext = {
file_name,
input_file_path,
file_name: basename(src_path),
input_file_path: relative(process.cwd(), src_path),
file: source_file,
tsdoc,
}
Expand Down

0 comments on commit 57d1270

Please sign in to comment.