Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use CLI flag --output #46

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
}