Skip to content

Commit

Permalink
Merge pull request #167 from open-rpc/fix/switch-compiler
Browse files Browse the repository at this point in the history
feat: switch to json-schema-to-types
  • Loading branch information
BelfordZ authored Jan 15, 2020
2 parents d0d6006 + 28e9489 commit 4eb8985
Show file tree
Hide file tree
Showing 5 changed files with 562 additions and 670 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
test:
<<: *defaults
docker:
- image: circleci/node:10
- image: circleci/node:lts
steps:
- checkout
- restore_cache: *restore-deps-cache
Expand All @@ -42,7 +42,7 @@ jobs:
build:
<<: *defaults
docker:
- image: circleci/node:10
- image: circleci/node:lts
steps:
- checkout
- restore_cache: *restore-deps-cache
Expand All @@ -53,7 +53,7 @@ jobs:
release:
<<: *defaults
docker:
- image: circleci/node:10
- image: circleci/node:lts
steps:
- checkout
- restore_cache: *restore-deps-cache
Expand Down
28 changes: 20 additions & 8 deletions bin/build.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#!/usr/bin/env node

const schema = require('../schema.json');
const { compile } = require('json-schema-to-typescript');
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const writeFile = promisify(fs.writeFile);
const { ensureDir } = require('fs-extra');
const {listReleases} = require("@etclabscore/dl-github-releases");

// errors if you try to run with $ref to draft 7 json schema
schema.definitions.schema.$ref = undefined;
const {JsonSchemaToTypes} = require("@etclabscore/json-schema-to-types");
const refParser = require("json-schema-ref-parser");

const generateTypes = async (s) => {
const ts = await compile(s, "OpenRPC");
const parsed = await refParser.dereference(s);
// the title set is particularly ugly, so we set a new one
parsed.definitions.schema.title = "JSONSchema";

// we must fix a bug with the deref util...
parsed.definitions.contentDescriptorObject.properties.schema = parsed.definitions.schema;

const transpiler = new JsonSchemaToTypes(parsed);
const ts = transpiler.toTs();
const dir = path.resolve(__dirname, "../build/src/");
await ensureDir(dir);
await writeFile(`${dir}/index.d.ts`, ts, "utf8");
Expand All @@ -29,12 +35,18 @@ const setOpenRPCVersionEnum = async (s) => {
const build = async () => {
const withVersionEnum = await setOpenRPCVersionEnum(schema);

await generateTypes(withVersionEnum);

const dir = path.resolve(__dirname, "../build/");
await ensureDir(dir);

await writeFile(`${dir}/schema.json`, JSON.stringify(withVersionEnum, undefined, " "));
console.log("wrote schema.json");

try {
await generateTypes(withVersionEnum);
} catch (e) {
console.error(e);
process.exit(1);
}


console.log("Finished building");
};
Expand Down
Loading

0 comments on commit 4eb8985

Please sign in to comment.