Skip to content

Commit 58dc0ba

Browse files
authored
fix: use CLI flag --output (#46)
The flag was not used to determine the output dir. Now, if the flag is set, it is used as output dir, if not set, directory containing the proto file is used.
1 parent aa0601c commit 58dc0ba

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

packages/protons/src/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import path from 'path'
33
import { promisify } from 'util'
44
import fs from 'fs/promises'
55

6-
function pathWithExtension (input: string, extension: string) {
7-
return path.join(path.dirname(input), path.basename(input).split('.').slice(0, -1).join('.') + extension)
6+
function pathWithExtension (input: string, extension: string, outputDir?: string) {
7+
const output = outputDir ?? path.dirname(input)
8+
return path.join(output, path.basename(input).split('.').slice(0, -1).join('.') + extension)
89
}
910

1011
const types: Record<string, string> = {
@@ -285,7 +286,11 @@ function defineModule (def: ClassDef): ModuleDef {
285286
return moduleDef
286287
}
287288

288-
export async function generate (source: string, flags: any) {
289+
interface Flags {
290+
output?: string
291+
}
292+
293+
export async function generate (source: string, flags: Flags) {
289294
// convert .protobuf to .json
290295
const json = await promisify(pbjs)(['-t', 'json', source])
291296

@@ -318,5 +323,5 @@ export async function generate (source: string, flags: any) {
318323

319324
const content = lines.join('\n').trim()
320325

321-
await fs.writeFile(pathWithExtension(source, '.ts'), content + '\n')
326+
await fs.writeFile(pathWithExtension(source, '.ts', flags.output), content + '\n')
322327
}

0 commit comments

Comments
 (0)