Skip to content

Commit

Permalink
Merge pull request #490 from open-rpc/fix/upgrade-deps
Browse files Browse the repository at this point in the history
fix: upgrade deps
  • Loading branch information
BelfordZ authored Aug 5, 2020
2 parents b1c1619 + f9e9d45 commit cd3069f
Show file tree
Hide file tree
Showing 8 changed files with 1,746 additions and 8,403 deletions.
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12.14.0
12.18.3
9,974 changes: 1,664 additions & 8,310 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@etclabscore/json-schema-to-types": "^1.10.1",
"@iarna/toml": "2.2.5",
"@open-rpc/typings": "^1.8.0",
"commander": "5.1.0",
"fs-extra": "9.0.1",
"inquirer": "^7.0.4",
"lodash": "4.17.19"
"@iarna/toml": "^2.2.5",
"@json-schema-tools/transpiler": "^1.4.1",
"@open-rpc/typings": "^1.11.2",
"commander": "^6.0.0",
"fs-extra": "^9.0.1",
"inquirer": "^7.3.3",
"lodash": "^4.17.19"
},
"devDependencies": {
"@open-rpc/examples": "^1.3.5",
"@open-rpc/meta-schema": "^1.6.0",
"@open-rpc/schema-utils-js": "^1.12.0",
"@open-rpc/server-js": "^1.6.2",
"@open-rpc/examples": "^1.6.0",
"@open-rpc/meta-schema": "^1.12.7",
"@open-rpc/schema-utils-js": "^1.14.0",
"@open-rpc/server-js": "^1.8.0",
"@types/connect": "^3.4.33",
"@types/cors": "^2.8.6",
"@types/fs-extra": "9.0.1",
"@types/jest": "26.0.8",
"@types/fs-extra": "^9.0.1",
"@types/inquirer": "^7.3.0",
"@types/lodash": "4.14.158",
"jest": "^25.1.0",
"ts-jest": "25.5.1",
"tslint": "6.1.3",
"typescript": "^3.7.5"
"@types/lodash": "^4.14.158",
"jest": "^26.2.1",
"ts-jest": "^26.1.4",
"tslint": "^6.1.2",
"typescript": "^3.9.7"
}
}
15 changes: 9 additions & 6 deletions src/components/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ jsonrpc_client!(pub struct <%= className %> {

const hooks: IHooks = {
afterCopyStatic: [
async (dest, frm, component) => {
async (dest, frm, component): Promise<void> => {
if (component.language === "typescript") {
await move(path.join(dest, "_package.json"), path.join(dest, "package.json"), { overwrite: true });
return await move(path.join(dest, "_package.json"), path.join(dest, "package.json"), { overwrite: true });
}
},
],
afterCompileTemplate: [
async (dest, frm, component, openrpcDocument) => {
async (dest, frm, component, openrpcDocument): Promise<void> => {
if (component.language === "typescript") {
const packagePath = path.join(dest, "package.json");
const fileContents = await readFile(packagePath);
Expand All @@ -191,8 +191,11 @@ const hooks: IHooks = {
name: component.name,
version: openrpcDocument.info.version,
});
await writeFile(packagePath, updatedPkg);
} else if (component.language === "rust") {

return await writeFile(packagePath, updatedPkg);
}

if (component.language === "rust") {
const cargoTOMLPath = path.join(dest, "Cargo.toml");
const fileContents = await readFile(cargoTOMLPath);
const cargoTOML = TOML.parse(fileContents.toString());
Expand All @@ -204,7 +207,7 @@ const hooks: IHooks = {
version: openrpcDocument.info.version,
},
});
await writeFile(cargoTOMLPath, updatedCargo);
return await writeFile(cargoTOMLPath, updatedCargo);
}
},
],
Expand Down
6 changes: 2 additions & 4 deletions src/components/docs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as path from "path";
import { move, ensureDir, remove } from "fs-extra";
import { remove } from "fs-extra";
import { IHooks } from "..";
import * as fs from "fs";
import { promisify } from "util";
import { template, startCase } from "lodash";
import { ContentDescriptorObject, ExamplePairingObject, ExampleObject, MethodObject } from "@open-rpc/meta-schema";
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const access = promisify(fs.access);

const indexTemplate = template(`import React, { useEffect } from "react";
import { Grid, Typography, Box, Button } from "@material-ui/core";
Expand Down Expand Up @@ -88,7 +86,7 @@ module.exports = {

const hooks: IHooks = {
afterCopyStatic: [
async (dest, frm, component, openrpcDocument) => {
async (dest, frm, component, openrpcDocument): Promise<void> => {
const destPath = path.join(dest, "package.json");
const tmplPath = path.join(dest, "_package.json");

Expand Down
12 changes: 6 additions & 6 deletions src/components/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from "path";
import { move, ensureDir, remove } from "fs-extra";
import { ensureDir, remove } from "fs-extra";
import { IHooks } from "..";
import * as fs from "fs";
import { promisify } from "util";
import { template } from "lodash";
import { ContentDescriptorObject, ExamplePairingObject, ExampleObject, MethodObject } from "@open-rpc/meta-schema";
import { ContentDescriptorObject, ExamplePairingObject, ExampleObject } from "@open-rpc/meta-schema";
const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);
const access = promisify(fs.access);
Expand All @@ -31,7 +31,7 @@ const generatedTypingsTemplate = template(`<%= methodTypings.toString("typescrip

const hooks: IHooks = {
afterCopyStatic: [
async (dest, frm, component, openrpcDocument) => {
async (dest, frm, component, openrpcDocument): Promise<void> => {
onlyHandleTS(component);
const destPath = path.join(dest, "package.json");
const tmplPath = path.join(dest, "_package.json");
Expand Down Expand Up @@ -67,14 +67,14 @@ const hooks: IHooks = {
},
],
afterCompileTemplate: [
async (dest, frm, component, openrpcDocument, typings) => {
async (dest, frm, component, openrpcDocument, typings): Promise<void> => {
onlyHandleTS(component);

const methodsFolder = `${dest}/src/methods/`;
await ensureDir(methodsFolder);

// Only write new one if there isnt one already.
await Promise.all(openrpcDocument.methods.map(async (method) => {
for (const method of openrpcDocument.methods) {
const methodFileName = `${methodsFolder}/${method.name}.ts`;

const functionAliasName = typings.getTypingNames("typescript", method).method;
Expand Down Expand Up @@ -117,7 +117,7 @@ const hooks: IHooks = {
}

await writeFile(methodFileName, codeToWrite, "utf8");
}));
}

const imports = openrpcDocument.methods.map(({ name }) => `import ${name} from "./${name}";`);
const methodMappingStr = [
Expand Down
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fsx, { emptyDir } from "fs-extra";
import examples from "@open-rpc/examples";
import { promisify } from "util";
import { forEach } from "lodash";
import { parseOpenRPCDocument, OpenRPCDocumentValidationError } from "@open-rpc/schema-utils-js";
import { parseOpenRPCDocument, OpenRPCDocumentDereferencingError } from "@open-rpc/schema-utils-js";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";

const stat = promisify(fs.stat);
Expand Down Expand Up @@ -61,7 +61,7 @@ describe(`Examples to generate Js clients`, () => {
};
const genProm = clientGen(testDocument);

return expect(genProm).rejects.toBeInstanceOf(OpenRPCDocumentValidationError);
return expect(genProm).rejects.toBeInstanceOf(OpenRPCDocumentDereferencingError);
});

forEach(examples, (example: OpenRPC, exampleName: string) => {
Expand Down
102 changes: 45 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { startCase, TemplateExecutor } from "lodash";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import { parseOpenRPCDocument } from "@open-rpc/schema-utils-js";

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

import clientComponent from "./components/client";
import serverComponent from "./components/server";
import docsComponent from "./components/docs";

const writeFile = promisify(fs.writeFile);

const moveFiles = async (dirName: string, file1: string, file2: string) => {
const moveFiles = async (dirName: string, file1: string, file2: string): Promise<any> => {
try {
await move(path.join(dirName, file1), path.join(dirName, file2));
return await move(path.join(dirName, file1), path.join(dirName, file2));
} catch (error) {
// do nothing
return;
}
};

Expand All @@ -27,7 +27,7 @@ type FHook = (
fromDir: string,
component: TComponentConfig,
openrpcDocument: OpenRPC,
methodTypings: MethodTypings,
Typings: Typings,
) => Promise<any>;

export interface IHooks {
Expand Down Expand Up @@ -62,24 +62,23 @@ const copyStaticForComponent = async (
destinationDirectoryName: string,
component: TComponentConfig,
dereffedDocument: OpenRPC,
methodTypings: MethodTypings,
typings: Typings,
) => {
const staticPath = getComponentTemplatePath(component);

const hooks = componentHooks[component.type];
const { beforeCopyStatic, afterCopyStatic } = hooks;

if (beforeCopyStatic && beforeCopyStatic.length && beforeCopyStatic.length > 0) {
await Promise.all(
beforeCopyStatic.map(
(hookFn: any) => hookFn(
destinationDirectoryName,
staticPath,
component,
dereffedDocument,
methodTypings,
),
),
);
for (const hookFn of beforeCopyStatic) {
await hookFn(
destinationDirectoryName,
staticPath,
component,
dereffedDocument,
typings,
);
}
}

await copy(staticPath, destinationDirectoryName, { overwrite: true });
Expand All @@ -91,15 +90,15 @@ const copyStaticForComponent = async (

// this is where we would do things like move _package.json to package.json, etc, etc
if (afterCopyStatic && afterCopyStatic.length && afterCopyStatic.length > 0) {
await Promise.all(
afterCopyStatic.map((hookFn: any) => hookFn(
for (const hookFn of afterCopyStatic) {
await hookFn(
destinationDirectoryName,
staticPath,
component,
dereffedDocument,
methodTypings,
)),
);
typings,
);
}
}
};

Expand Down Expand Up @@ -129,12 +128,7 @@ export interface IGeneratorOptions {
components: TComponentConfig[];
}

const languageFilenameMap: any = {
rust: "lib.rs",
typescript: "index.ts",
};

const prepareOutputDirectory = async (outDir: string, component: TComponentConfig) => {
const prepareOutputDirectory = async (outDir: string, component: TComponentConfig): Promise<string> => {
const destinationDirectoryName = `${outDir}/${component.type}/${component.language}`;
await ensureDir(destinationDirectoryName);
return destinationDirectoryName;
Expand All @@ -144,7 +138,7 @@ const writeOpenRpcDocument = async (
outDir: string,
doc: OpenRPC | string,
component: TComponentConfig,
) => {
): Promise<string> => {
const toWrite = typeof doc === "string" ? await parseOpenRPCDocument(doc, { dereference: false }) : doc;
const destinationDirectoryName = `${outDir}/${component.type}/${component.language}/src/openrpc.json`;
await writeFile(destinationDirectoryName, JSON.stringify(toWrite, undefined, " "), "utf8");
Expand All @@ -155,43 +149,39 @@ const compileTemplate = async (
destDir: string,
component: TComponentConfig,
dereffedDocument: OpenRPC,
methodTypings: MethodTypings,
) => {
typings: Typings,
): Promise<boolean> => {
const templatedPath = `${getComponentTemplatePath(component)}/templated`;

const hooks = componentHooks[component.type];
const { beforeCompileTemplate, afterCompileTemplate } = hooks;

if (beforeCompileTemplate && beforeCompileTemplate.length && beforeCompileTemplate.length > 0) {
await Promise.all(
beforeCompileTemplate.map(
(hookFn: any) => hookFn(destDir, templatedPath, component, dereffedDocument, methodTypings),
),
);
for (const hookFn of beforeCompileTemplate) {
await hookFn(destDir, templatedPath, component, dereffedDocument, typings);
}
}

// 1. read files in the templated directory,
// 2. for each one, pass in the template params
const templates = hooks.templateFiles[component.language];
await Promise.all(
templates.map(async (t) => {
const result = t.template({
className: startCase(dereffedDocument.info.title).replace(/\s/g, ""),
methodTypings,
openrpcDocument: dereffedDocument,
});

await writeFile(`${destDir}/${t.path}`, result, "utf8");
}),
);
for (const t of templates) {
const result = t.template({
className: startCase(dereffedDocument.info.title).replace(/\s/g, ""),
methodTypings: typings,
openrpcDocument: dereffedDocument,
});

await writeFile(`${destDir}/${t.path}`, result, "utf8");
}

if (afterCompileTemplate && afterCompileTemplate.length && afterCompileTemplate.length > 0) {
await Promise.all(
afterCompileTemplate.map(
(hookFn: any) => hookFn(destDir, templatedPath, component, dereffedDocument, methodTypings),
),
);
for (const hookFn of afterCompileTemplate) {
await hookFn(destDir, templatedPath, component, dereffedDocument, typings);
}
}

return true;
};

export default async (generatorOptions: IGeneratorOptions) => {
Expand All @@ -206,14 +196,12 @@ export default async (generatorOptions: IGeneratorOptions) => {
throw e;
}

const methodTypings = new MethodTypings(dereffedDocument);
const methodTypings = new Typings(dereffedDocument);

const componentGeneratorPromises = generatorOptions.components.map(async (component) => {
for (const component of generatorOptions.components) {
const destDir = await prepareOutputDirectory(outDir, component);
await copyStaticForComponent(destDir, component, dereffedDocument, methodTypings);
await writeOpenRpcDocument(outDir, openrpcDocument, component);
await compileTemplate(destDir, component, dereffedDocument, methodTypings);
});

await Promise.all(componentGeneratorPromises);
}
};

0 comments on commit cd3069f

Please sign in to comment.