-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cli for syntax transformer (#105)
* feat: cli for syntax transformer Close #40
- Loading branch information
Showing
8 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env node | ||
|
||
import yargs from "yargs"; | ||
import { hideBin } from "yargs/helpers"; | ||
import { commands } from "../cmds"; | ||
|
||
yargs(hideBin(process.argv)) | ||
.command(commands as any) | ||
.demandCommand() | ||
.recommendCommands() | ||
.strict() | ||
.help() | ||
.completion().argv; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable import/namespace */ | ||
import { CommandModule } from "yargs"; | ||
import * as transform from "./transform"; | ||
|
||
export const commands: CommandModule[] = [transform as CommandModule]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { readFile } from "fs/promises"; | ||
import { Options as YargsOptions } from "yargs"; | ||
|
||
import checkDSL from "../validator"; | ||
import transformSyntax from "../transformer"; | ||
import { assertNever } from "assert-never"; | ||
|
||
type fromOption = "dsl" | "json"; | ||
|
||
interface CommandArgs { | ||
from: fromOption; | ||
inputFile: string; | ||
} | ||
|
||
exports.command = "transform"; | ||
exports.desc = "Transform "; | ||
exports.builder = { | ||
from: { | ||
describe: "whether we want to transform from dsl or json", | ||
choices: ["dsl", "json"], | ||
required: true, | ||
}, | ||
inputFile: { | ||
describe: "Configuration file. It must be in DSL syntax.", | ||
type: "string", | ||
required: true, | ||
}, | ||
} as Record<keyof CommandArgs, YargsOptions>; | ||
|
||
async function loadFile(inputFile: string) { | ||
return readFile(inputFile, "utf-8"); | ||
} | ||
|
||
exports.handler = async (argv: CommandArgs) => { | ||
try { | ||
const fileContents = await loadFile(argv.inputFile); | ||
switch (argv.from) { | ||
case "dsl": { | ||
const validateResult = checkDSL.checkDSL(fileContents); | ||
if (validateResult.length) { | ||
throw new Error(`Invalid DSL with error ${JSON.stringify(validateResult)}`); | ||
} | ||
const transformedResult = transformSyntax.friendlySyntaxToApiSyntax(fileContents); | ||
console.log(JSON.stringify(transformedResult, null, 4)); | ||
break; | ||
} | ||
case "json": { | ||
const transformedResult = transformSyntax.apiSyntaxToFriendlySyntax(JSON.parse(fileContents)); | ||
console.log(transformedResult); | ||
break; | ||
} | ||
default: | ||
assertNever(argv.from); | ||
} | ||
} catch (err) { | ||
console.error(err as Error); | ||
process.exitCode = 1; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters