Skip to content

Commit

Permalink
Merge pull request #200 from open-rpc/fix/add-ts-name-config
Browse files Browse the repository at this point in the history
fix: add ts-name config for package json name + rs-name for crate
  • Loading branch information
shanejonas authored May 24, 2019
2 parents 052716a + 9a88f26 commit 784c000
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ $ open-rpc-generator-client --help
Usage: open-rpc-generator-client [options]

Options:
-d, --document [openrpcDocument] JSON string or a Path/Url pointing to an open rpc schema
-h, --help output usage information
-v, --version output the version number
-d, --document [openrpcDocument] JSON string or a Path/Url pointing to an open rpc schema (default: "./openrpc.json")
-o, --outputDir [outputDirectory] output directory that the clients will be generated into (default: "./")
--ts-name [packageName] Name that will go in the package.json for the typescript client (default: "template-client")
--rs-name [crateName] Name that will go in the crate name for the rust client (default: "template-client")
-h, --help output usage information
```

### Generating a Client

```shell
$ open-rpc-generator-client \
-d https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/petstore-openrpc.json \
-d https://raw.githubusercontent.com/open-rpc/examples/master/service-descriptions/petstore-openrpc.json
```

Using the `open-rpc-generator-client` command, then passing an example OpenRPC document `petstore-openrpc.json` in the directory of `Petstore`.
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@iarna/toml": "^2.2.3",
"@open-rpc/schema-utils-js": "^1.9.0",
"@open-rpc/typings": "^1.0.4",
"commander": "^2.19.0",
Expand Down
21 changes: 18 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ program
)
.option(
"-o, --outputDir [outputDirectory]",
"JSON string or a Path/Url pointing to an open rpc schema",
"./openrpc.json",
"output directory that the clients will be generated into",
"./",
)
.option(
"--ts-name [packageName]",
"Name that will go in the package.json for the typescript client",
"template-client",
)
.option(
"--rs-name [crateName]",
"Name that will go in the crate name for the rust client",
"template-client",
)
.action(async () => {
let openrpcDocument: OpenRPC;
Expand All @@ -30,7 +40,12 @@ program
return;
}

await orpcGenerator({ outDir, openrpcDocument });
await orpcGenerator({
openrpcDocument,
outDir,
rsName: program.rsName || "template-client",
tsName: program.tsName || "template-client",
});

console.log("Done!"); // tslint:disable-line
})
Expand Down
2 changes: 2 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe(`Examples to generate Js clients`, () => {
await clientGen({
openrpcDocument: await parseOpenRPCDocument(example),
outDir: exampleOutDir,
rsName: "template-client",
tsName: "template-client",
});

await expect(stat(exampleOutDir)).resolves.toBeTruthy();
Expand Down
57 changes: 53 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { exec } from "child_process";
import * as fs from "fs";
import { ensureDir, emptyDir, copy, move } from "fs-extra";
import { ensureDir, emptyDir, copy, move, readFile } from "fs-extra";
import * as path from "path";
import { promisify } from "util";
import { map, trim, startCase } from "lodash";
import { startCase } from "lodash";
import { OpenRPC } from "@open-rpc/meta-schema";
import TOML from "@iarna/toml";

import MethodTypings from "@open-rpc/typings";

Expand Down Expand Up @@ -34,6 +35,51 @@ const compileTemplate = async (
});
};

const processRust = async (
destinationDirectoryName: string,
generatorOptions: IGeneratorOptions,
) => {
const cargoTOMLPath = path.join(destinationDirectoryName, "Cargo.toml");
const fileContents = await readFile(cargoTOMLPath);
const cargoTOML = TOML.parse(fileContents.toString());
const updatedCargo = TOML.stringify({
...cargoTOML,
package: {
...cargoTOML.package as object,
name: generatorOptions.rsName,
version: generatorOptions.openrpcDocument.info.version,
},
});
await writeFile(cargoTOMLPath, updatedCargo);
};

const processTypescript = async (
destinationDirectoryName: string,
generatorOptions: IGeneratorOptions,
) => {
const packagePath = path.join(destinationDirectoryName, "package.json");
const fileContents = await readFile(packagePath);
const pkg = JSON.parse(fileContents.toString());
const updatedPkg = JSON.stringify({
...pkg,
name: generatorOptions.tsName,
version: generatorOptions.openrpcDocument.info.version,
});
await writeFile(packagePath, updatedPkg);
};

const postProcessStatic = async (
destinationDirectoryName: string,
generatorOptions: IGeneratorOptions,
language: string,
) => {
if (language === "rust") {
return processRust(destinationDirectoryName, generatorOptions);
} else {
return processTypescript(destinationDirectoryName, generatorOptions);
}
};

const moveFiles = async (dirName: string, file1: string, file2: string) => {
try {
await move(path.join(dirName, file1), path.join(dirName, file2));
Expand All @@ -48,11 +94,13 @@ const copyStatic = async (destinationDirectoryName: string, language: string) =>
const staticPath = path.join(__dirname, "../", `/templates/${language}/static`);
await copy(staticPath, destinationDirectoryName);

moveFiles(destinationDirectoryName, "_package.json", "package.json");
moveFiles(destinationDirectoryName, "gitignore", ".gitignore");
await moveFiles(destinationDirectoryName, "_package.json", "package.json");
await moveFiles(destinationDirectoryName, "gitignore", ".gitignore");
};

interface IGeneratorOptions {
rsName: string;
tsName: string;
outDir: string;
openrpcDocument: OpenRPC;
}
Expand All @@ -74,6 +122,7 @@ export default async (generatorOptions: IGeneratorOptions) => {
const destinationDirectoryName = `${outDir}/${language}`;
await cleanBuildDir(destinationDirectoryName);
await copyStatic(destinationDirectoryName, language);
await postProcessStatic(destinationDirectoryName, generatorOptions, language);
await writeFile(`${destinationDirectoryName}/src/${languageFilenameMap[language]}`, compiledResult, "utf8");

await bootstrapGeneratedPackage(destinationDirectoryName, language);
Expand Down

0 comments on commit 784c000

Please sign in to comment.