Skip to content

Commit

Permalink
feat: cli for syntax transformer (#105)
Browse files Browse the repository at this point in the history
* feat: cli for syntax transformer

Close #40
  • Loading branch information
adriantam authored Jan 17, 2023
1 parent 5dd865a commit 20e5b8b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 6 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"build:grammar": "npx nearleyc src/parser/openfga.ne > src/parser/grammar.ts",
"build:ts": "rm -rf dist/ && tsc --outDir dist/",
"build": "npm run build:grammar; npm run build:ts",
"build:npx": "tsc --build tsconfig.json",
"clean": "rm -r dist/",
"prepublishOnly": "npm run build",
"test": "npm run build:grammar; jest --config ./tests/jest.config.js",
"typecheck": "npm run build:grammar; tsc --skipLibCheck",
Expand All @@ -17,6 +19,9 @@
"format:check": "prettier --check {src,tests}/**",
"format:fix": "prettier --write {src,tests}/**"
},
"bin": {
"@openfga/syntax-transformer": "./dist/bin/index.js"
},
"keywords": [
"openfga",
"authorization",
Expand All @@ -28,6 +33,7 @@
"author": "OpenFGA",
"dependencies": {
"@openfga/sdk": "^0.2.0",
"assert-never": "^1.2.1",
"nearley": "^2.20.1"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions src/bin/index.ts
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;
5 changes: 5 additions & 0 deletions src/cmds/index.ts
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];
59 changes: 59 additions & 0 deletions src/cmds/transform.ts
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;
}
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
import constants, { enums } from "./constants";
import formatter from "./formatter";
import transformer from "./transformer";
Expand Down
12 changes: 6 additions & 6 deletions src/samples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ type AuthorizationModel = {
type_definitions: TypeDefinition[];
};

const entitlements = import("./entitlements.json") as Promise<AuthorizationModel>;
const entitlements = import("./entitlements.json") as unknown as Promise<AuthorizationModel>;
const expenses = import("./expenses.json") as any as Promise<AuthorizationModel>;
const gdrive = import("./gdrive.json") as Promise<AuthorizationModel>;
const generic = import("./generic.json") as Promise<AuthorizationModel>;
const github = import("./github.json") as Promise<AuthorizationModel>;
const gdrive = import("./gdrive.json") as unknown as Promise<AuthorizationModel>;
const generic = import("./generic.json") as unknown as Promise<AuthorizationModel>;
const github = import("./github.json") as unknown as Promise<AuthorizationModel>;
const iot = import("./iot.json") as Promise<AuthorizationModel>;
const slack = import("./slack.json") as Promise<AuthorizationModel>;
const customRoles = import("./custom-roles.json") as Promise<AuthorizationModel>;
const slack = import("./slack.json") as unknown as Promise<AuthorizationModel>;
const customRoles = import("./custom-roles.json") as unknown as Promise<AuthorizationModel>;

const sampleAuthorizationModels: Record<string, Promise<Required<Omit<AuthorizationModel, "id">>>> = {
entitlements,
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"compilerOptions": {
"declaration": true,
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es6",
"module": "commonjs",
"resolveJsonModule": true,
Expand Down

0 comments on commit 20e5b8b

Please sign in to comment.