Skip to content

Commit 0b99458

Browse files
Auto-generate a PluginClass for every plugin in a plugin.ts file for its plugin directory
Actually auto-generate the class for the plugin (as a script that calls a factory function to create the class) in the `plugin.ts` file within each plugin directory.
1 parent 3258aa1 commit 0b99458

4 files changed

Lines changed: 74 additions & 70 deletions

File tree

packages/db-splitgraph/db-splitgraph.test.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { compose, graphql, rest, type DefaultBodyType } from "msw";
1616
import { defaultHost } from "@madatdata/base-client";
1717

1818
import { faker } from "@faker-js/faker";
19-
import { AirbyteGithubImportPlugin } from "./plugins/importers/airbyte-github-plugin";
19+
import { SplitgraphAirbyteGithubImportPlugin } from "./plugins/importers/generated/airbyte-github/plugin";
2020

2121
describe("importData", () => {
2222
it("returns false for unknown plugin", async () => {
@@ -80,7 +80,7 @@ const createDb = () => {
8080
graphqlEndpoint: defaultHost.baseUrls.gql,
8181
transformRequestHeaders,
8282
}),
83-
new AirbyteGithubImportPlugin({
83+
new SplitgraphAirbyteGithubImportPlugin({
8484
graphqlEndpoint: defaultHost.baseUrls.gql,
8585
transformRequestHeaders,
8686
}),
@@ -116,7 +116,7 @@ const createRealDb = () => {
116116
graphqlEndpoint: defaultHost.baseUrls.gql,
117117
}),
118118

119-
new AirbyteGithubImportPlugin({
119+
new SplitgraphAirbyteGithubImportPlugin({
120120
graphqlEndpoint: defaultHost.baseUrls.gql,
121121
}),
122122

@@ -138,7 +138,14 @@ describe.skipIf(shouldSkipIntegrationTestsForGitHubExternalDataSource())(
138138

139139
const { username: namespace } = await fetchToken(db);
140140

141-
const res = await db.importData(
141+
// NOTE: not actually asserting anything here atm, and these tests
142+
// should usually be skipped until we have a better way of integration
143+
// testing that doesn't require spam-ingesting GitHub repos into Splitgraph,
144+
// or at least has the capability to delete them afterward
145+
// SEE: packages/test-helpers/env-config.ts for hardcoded skip logic
146+
147+
// For now this is just a way to manually check everything is working
148+
await db.importData(
142149
"airbyte-github",
143150
{
144151
credentials: {
@@ -166,16 +173,6 @@ describe.skipIf(shouldSkipIntegrationTestsForGitHubExternalDataSource())(
166173
],
167174
}
168175
);
169-
170-
expect(res.response.status).toEqual(200);
171-
172-
// db.importData("airbyte-github", {}, })
173-
174-
// await db.importData("airbyte-github", { params: }
175-
176-
// db.importData("csv", {data})
177-
178-
// db.importData("airbyte-github"
179176
}, 60_000);
180177
}
181178
);

packages/db-splitgraph/plugins/generate-plugins.script.ts

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,43 @@ const targetDir = path.join(thisSourceFileDir, "importers", "generated");
1414
const generateTypes = async () => {
1515
const allPlugins = await fetchSchemas();
1616

17-
// Store list of used interface names to ensure global uniqueness so that we
17+
// Store set of used interface names to ensure global uniqueness so that we
1818
// can export * from generated files. However, note this may not work because
1919
// the intermediate types are also exported, and those will have collisions
2020
const usedInterfaceNames = new Set<string>();
2121

22+
// Set of plugin class names (same comment as above applies, i.e. uniqueness is expected anyway)
23+
const usedPluginClassNames = new Set<string>();
24+
2225
for (let plugin of allPlugins.externalPlugins) {
2326
log("generateTypes:", plugin.pluginName);
2427
let pluginTargetDir = path.join(targetDir, plugin.pluginName);
2528

2629
log("mkdir:", fdir(pluginTargetDir));
2730
await mkdir(pluginTargetDir, { recursive: true });
2831

29-
let schemas = [
32+
let schemas: [schema: any, schemaName: string, interfaceName: string][] = [
3033
[plugin.credentialsSchema, "CredentialsSchema"],
3134
[plugin.paramsSchema, "ParamsSchema"],
3235
[plugin.tableParamsSchema, "TableParamsSchema"],
33-
];
34-
35-
for (let [schema, schemaName] of schemas) {
36+
].map(([schema, schemaName]) => [
37+
schema,
38+
schemaName,
39+
(() => {
40+
// Generate a name that's safe for interface type name and hasn't been used yet
41+
// We don't expect collisions, since pluginName is unique, but just in case
42+
const interfaceName = generateName(
43+
plugin.pluginName + schemaName,
44+
usedInterfaceNames
45+
);
46+
usedInterfaceNames.add(interfaceName);
47+
48+
return interfaceName;
49+
})(),
50+
]);
51+
52+
for (let [schema, schemaName, interfaceName] of schemas) {
3653
let schemaOutFile = path.join(pluginTargetDir, `${schemaName}.ts`);
37-
38-
// Generate a name that's safe for interface type name and hasn't been used yet
39-
// We don't expect collisions, since pluginName is unique, but just in case
40-
let interfaceName = generateName(
41-
plugin.pluginName + schemaName,
42-
usedInterfaceNames
43-
);
44-
usedInterfaceNames.add(interfaceName);
45-
4654
log("interfaceName:", interfaceName);
4755

4856
let generatedTypescript = await compile(schema, interfaceName, {
@@ -52,6 +60,40 @@ const generateTypes = async () => {
5260
log("write schema:", fdir(schemaOutFile));
5361
await writeFile(schemaOutFile, generatedTypescript);
5462
}
63+
64+
const [
65+
[_cs, _csn, credentialsSchemaInterfaceName],
66+
[_ps, _psn, paramsSchemaInterfaceName],
67+
[_tps, _tpsn, tableParamsSchemaInterfaceName],
68+
] = schemas;
69+
70+
// NOTE: Concatenate "Splitgraph" outside of generated name to ensure PascalCasing
71+
// (otherwise we get "SplitgraphairbyteGitHubImportPlugin" instead of "SplitgraphAirbyteGitHubImportPlugin")
72+
let pluginClassName =
73+
"Splitgraph" +
74+
generateName(plugin.pluginName + "ImportPlugin", usedPluginClassNames);
75+
let pluginOutFile = path.join(pluginTargetDir, "plugin.ts");
76+
77+
log("create plugin: ", pluginClassName, "in", pluginOutFile);
78+
79+
await writeFile(
80+
pluginOutFile,
81+
`/** Auto-generated plugin **/
82+
83+
import type { ${paramsSchemaInterfaceName} } from "./ParamsSchema";
84+
import type { ${tableParamsSchemaInterfaceName} } from "./TableParamsSchema";
85+
import type { ${credentialsSchemaInterfaceName} } from "./CredentialsSchema";
86+
import { makeGeneratedImportPlugin } from "../../splitgraph-generated-import-plugin";
87+
88+
export const ${pluginClassName} = makeGeneratedImportPlugin<
89+
"${plugin.pluginName}",
90+
${paramsSchemaInterfaceName},
91+
${tableParamsSchemaInterfaceName},
92+
${credentialsSchemaInterfaceName}
93+
>("${plugin.pluginName}");
94+
95+
`
96+
);
5597
}
5698
};
5799

packages/db-splitgraph/plugins/importers/airbyte-github-plugin.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

packages/test-helpers/env-config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ export const shouldSkipIntegrationTests = () => {
8181
export const shouldSkipIntegrationTestsForGitHubExternalDataSource = () => {
8282
// TODO: Temporarily hardcoded to avoid ingesting a bunch of data without deleting it
8383
return true;
84+
85+
// const environmentHasGitHubPATSecret = () => {
86+
// return (
87+
// // @ts-expect-error https://stackoverflow.com/a/70711231
88+
// !!import.meta.env.VITE_TEST_GITHUB_PAT_SECRET
89+
// );
90+
// };
8491
// return shouldSkipIntegrationTests() || !environmentHasGitHubPATSecret();
8592
};
8693

8794
export const shouldSkipSeafowlTests = () => {
8895
return !environmentHasSeafowlCredential();
8996
};
90-
91-
// const environmentHasGitHubPATSecret = () => {
92-
// return (
93-
// // @ts-expect-error https://stackoverflow.com/a/70711231
94-
// !!import.meta.env.VITE_TEST_GITHUB_PAT_SECRET
95-
// );
96-
// };

0 commit comments

Comments
 (0)