Skip to content

Commit

Permalink
feat: refactor code to support an override output directory
Browse files Browse the repository at this point in the history
The output directory outDir has alot of automated default settings
that are not the best for all situations. outPath overrides outDir
allowing components to be placed in a specific location without
a directory hierarchy.

fixes #655
  • Loading branch information
zcstarr committed Aug 27, 2021
1 parent 6a9a092 commit 6e7937e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ export interface IClientConfig {
type: "client";
name: string;
language: "typescript" | "rust";
outPath?: string;
}

export interface IServerConfig {
type: "server";
name: string;
language: "typescript";
outPath?: string;
}

export interface IDocsConfig {
type: "docs";
name: string;
language: "gatsby";
outPath?: string;
}

// Experimental
Expand All @@ -24,6 +27,7 @@ export interface ICustomConfig {
customComponent: string;
customType?: string;
openRPCPath?: string | null;
outPath?: string;
language: "typescript" | "rust";
}

Expand Down
24 changes: 21 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,21 @@ describe(`Examples to generate Js clients`, () => {
return expect(genProm).rejects.toBeInstanceOf(OpenRPCDocumentDereferencingError);
});


forEach(examples, (example: OpenRPC, exampleName: string) => {
it(`rejects configurations without outDir or outPath`, async ()=>{
const promGen = clientGen({
openrpcDocument: await parseOpenRPCDocument(example),
components: [
{ type: "client", language: "typescript", name: "testclient-ts" },
]
});
expect(promGen).rejects.toBeInstanceOf(Error);
});

it(`creates a new client for example: ${exampleName} and regenerates after`, async () => {
const exampleOutDir = `${testDir}/${exampleName}`;
expect.assertions(2);
expect.assertions(5);

await clientGen({
openrpcDocument: await parseOpenRPCDocument(example),
Expand All @@ -79,11 +90,14 @@ describe(`Examples to generate Js clients`, () => {
{ type: "docs", language: "gatsby", name: "testserver-gatsby" },
{ type: "custom", language: "typescript", name: "custom-stuff", "customComponent":"./src/custom-test-component.js", customType:"client"},
{ type: "custom", language: "typescript", name: "custom-stuff2", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: null},
{ type: "custom", language: "typescript", name: "custom-stuff3", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: "tmpz"}
{ type: "custom", language: "typescript", name: "custom-stuff3", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: "tmpz"},
{ type: "custom", language: "typescript", name: "custom-stuff4", "customComponent":"./src/custom-test-component.js", customType:"client",
openRPCPath: "tmpy", outPath: `${exampleOutDir}/special`}
],
});

await expect(stat(exampleOutDir)).resolves.toBeTruthy();
await expect(stat(`${exampleOutDir}/special`)).resolves.toBeTruthy();

await clientGen({
openrpcDocument: await parseOpenRPCDocument(example),
Expand All @@ -95,10 +109,14 @@ describe(`Examples to generate Js clients`, () => {
{ type: "docs", language: "gatsby", name: "testserver-gatsby" },
{ type: "custom", language: "typescript", name: "custom-stuff", "customComponent":"./src/custom-test-component.js", customType:"client"},
{ type: "custom", language: "typescript", name: "custom-stuff2", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: null},
{ type: "custom", language: "typescript", name: "custom-stuff3", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: "tmpz"}
{ type: "custom", language: "typescript", name: "custom-stuff3", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: "tmpz"},
{ type: "custom", language: "typescript", name: "custom-stuff4", "customComponent":"./src/custom-test-component.js", customType:"client",
openRPCPath: "tmpy", outPath: `${exampleOutDir}/special`}
],
});

await expect(stat(`${exampleOutDir}/special`)).resolves.toBeTruthy();

await expect(stat(exampleOutDir)).resolves.toBeTruthy();
}, 100000);
});
Expand Down
22 changes: 16 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const copyStaticForComponent = async (
};

export interface IGeneratorOptions {
outDir: string;
outDir?: string;
openrpcDocument: OpenRPC | string;
components: TComponentConfig[];
}
Expand All @@ -124,7 +124,7 @@ const writeOpenRpcDocument = async (
): Promise<string | undefined> => {
if(component.openRPCPath === undefined) return;
const toWrite = typeof doc === "string" ? await parseOpenRPCDocument(doc, { dereference: false }) : doc;
const openRPCPath = `${outDir}/${component.type}/${component.language}/${component.openRPCPath}`
const openRPCPath = `${outDir}/${component.openRPCPath}`
await ensureDir(openRPCPath);
const destinationDirectoryName = `${openRPCPath}/openrpc.json`;
await writeFile(destinationDirectoryName, JSON.stringify(toWrite, undefined, " "), "utf8");
Expand Down Expand Up @@ -179,10 +179,20 @@ export default async (generatorOptions: IGeneratorOptions) => {
const methodTypings = new Typings(dereffedDocument);

for (const componentConfig of generatorOptions.components) {
const outPath = componentConfig.outPath;
if(outPath === undefined && outDir === undefined){
console.error("No output path specified");
throw new Error("No output path specified");
}
const component = await getComponentFromConfig(componentConfig)
const destDir = await prepareOutputDirectory(outDir, component);
await copyStaticForComponent(destDir, component, dereffedDocument, methodTypings);
await writeOpenRpcDocument(outDir, openrpcDocument, component);
await compileTemplate(destDir, component, dereffedDocument, methodTypings);
let destDir = outPath;
if(!outPath){
destDir = await prepareOutputDirectory(outDir!, component);
}else {
await ensureDir(outPath);
}
await copyStaticForComponent(destDir!, component, dereffedDocument, methodTypings);
await writeOpenRpcDocument(destDir!, openrpcDocument, component);
await compileTemplate(destDir!, component, dereffedDocument, methodTypings);
}
};

0 comments on commit 6e7937e

Please sign in to comment.