Skip to content

Commit

Permalink
fix: use CLI flag --output (#46)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
D4nte committed Jul 28, 2022
1 parent aa0601c commit 58dc0ba
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/protons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import path from 'path'
import { promisify } from 'util'
import fs from 'fs/promises'

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

const types: Record<string, string> = {
Expand Down Expand Up @@ -285,7 +286,11 @@ function defineModule (def: ClassDef): ModuleDef {
return moduleDef
}

export async function generate (source: string, flags: any) {
interface Flags {
output?: string
}

export async function generate (source: string, flags: Flags) {
// convert .protobuf to .json
const json = await promisify(pbjs)(['-t', 'json', source])

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

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

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

0 comments on commit 58dc0ba

Please sign in to comment.