Skip to content

Commit 9d91730

Browse files
committed
refactor: move output handling to cli
1 parent 5888849 commit 9d91730

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/cli.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { mkdir, writeFile } from 'node:fs/promises'
12
import { extractinator } from './extractinator'
2-
import { join, resolve } from 'node:path'
3+
import { resolve } from 'node:path'
34
import sade from 'sade'
45
import './types.d'
56

@@ -20,7 +21,23 @@ cli.command('extract <input> <output>')
2021
input = resolve(input)
2122
output = resolve(output)
2223

23-
await extractinator(input, output, options['tsdoc-config'])
24+
const extracted_files = await extractinator({
25+
tsdocConfigPath: options['tsdoc-config'],
26+
input,
27+
})
28+
29+
//? Make sure the output directory exists
30+
await mkdir(output, { recursive: true })
31+
32+
//? Write out all the files that we extracted
33+
for (const file of extracted_files) {
34+
// todo could potentially collide, should mimic original folder structure
35+
await writeFile(
36+
`${output}/${file.fileName}.doc.json`,
37+
JSON.stringify(file, null, 2),
38+
'utf-8',
39+
)
40+
}
2441
})
2542

2643
cli.parse(process.argv)

src/extractinator.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ import { basename } from 'node:path'
99
import { Project } from 'ts-morph'
1010
import { emit_dts } from './emit'
1111

12-
export async function extractinator(input: string, output: string, tsdocConfigPath?: string) {
12+
export interface ExtractinatorOptions {
13+
tsdocConfigPath?: string
14+
input: string
15+
}
16+
17+
export async function extractinator(options: ExtractinatorOptions) {
1318
const project = new Project()
1419

1520
//? Generate the .d.ts files
16-
const dts = await emit_dts(input)
21+
const dts = await emit_dts(options.input)
1722

1823
//? Load all the generated .d.ts files
1924
for (const dts_path of dts.dts_file_map.keys()) {
2025
project.addSourceFileAtPath(dts_path)
2126
}
2227

23-
//? Make sure the output directory exists
24-
await mkdir(output, { recursive: true })
25-
26-
const tsdoc = createTSDocParser(tsdocConfigPath)
28+
const tsdoc = createTSDocParser(options.tsdocConfigPath)
2729

2830
//? Map of input_file_path:file
2931
const parsed_files = new Map<string, ParsedFile>()
@@ -87,15 +89,7 @@ export async function extractinator(input: string, output: string, tsdocConfigPa
8789
n()
8890
}
8991

90-
for (const [input_file_path, file] of parsed_files) {
91-
// todo could potentially collide
92-
await writeFile(
93-
`${output}/${basename(input_file_path)}.doc.json`,
94-
JSON.stringify(file, null, 2),
95-
'utf-8',
96-
)
97-
}
98-
99-
//? Cleanup
10092
await dts.cleanup()
93+
94+
return Array.from(parsed_files.values())
10195
}

0 commit comments

Comments
 (0)