Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/app/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const FUNCTIONS_TS_FILE_TEMPLATE_DIRECTORY = "./functions"; // relative path (to
const FUNCTIONS_TS_FILE_TEMPLATE_FILE_NAME = "functions.ejs"; // name of the template ejs file that renders the functions.ts file

const NODE_VERSION = "node20";
const OPENAPI_SWAGGER_FILE_NAME = "swagger.json";

/**
* Context is a singleton class that holds the configuration of the app
Expand Down Expand Up @@ -189,4 +190,29 @@ export class Context {
public getNodeVersion(): string {
return NODE_VERSION;
}

/**
* Use this function to get filepaths for user files relating to the connector
* The DDN CLI is responsible for setting the correct env vars
* The reason we need this correction is because the filepaths can change on whether the connector is being run via the CLI or via the Docker image
*/
public getUserMountedFilePath(): string {
// Check for HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH environment variable
if (process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH) {
return process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH;
}

// Check for HASURA_CONFIGURATION_DIRECTORY environment variable
if (process.env.HASURA_CONFIGURATION_DIRECTORY) {
return process.env.HASURA_CONFIGURATION_DIRECTORY;
}

// If neither variable is present, log warning and return default path
logger.warn("Neither HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH nor HASURA_CONFIGURATION_DIRECTORY environment variables are set. Using default path (/etc/connector/).");
return "/etc/connector/";
}

public getDefaultOpenapiDocumentFileUri(): string {
return path.join(this.getUserMountedFilePath(), OPENAPI_SWAGGER_FILE_NAME);
}
}
9 changes: 3 additions & 6 deletions src/app/writer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as types from "../types";
import * as logger from "../../util/logger";
import { exit } from "process";
import { execSync } from "child_process";
import * as context from "../context";

export async function writeToFileSystem(codeToWrite: types.GeneratedCode[]) {
try {
Expand All @@ -22,12 +23,8 @@ export async function writeToFileSystem(codeToWrite: types.GeneratedCode[]) {
await functionsWriter.writeToFileSystem(functionsTsCode, apiTsCode);

logger.info("running npm install :: installing dependencies");
if (process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH) {
execSync("npm install ", { stdio: "inherit", cwd: process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH });
} else {
execSync("npm install ", { stdio: "inherit" });
}

const mountedPath = context.getInstance().getUserMountedFilePath();
execSync("npm install ", { stdio: "inherit", cwd: mountedPath });
logger.info("all dependencies installed");
} catch (e) {
if (e instanceof apiWriter.SimilarFileContentsError) {
Expand Down
9 changes: 5 additions & 4 deletions src/cli/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from "commander";
import { resolve } from "path";
import * as logger from "../util/logger";
import * as context from "../app/context";

export const cmd = new Command("init")
.description(
Expand All @@ -15,13 +16,13 @@ Further reading:
)
.option(
"--open-api <value>",
"URI of OAS Document. Defaults to /etc/connector/swagger.json",
"/etc/connector/swagger.json",
`URI of OAS Document. Defaults to ${context.getInstance().getDefaultOpenapiDocumentFileUri()}`,
context.getInstance().getDefaultOpenapiDocumentFileUri(),
)
.option(
"--out-dir <value>",
"Output Directory. Defaults to /etc/connector/",
"/etc/connector/",
`Output Directory. Defaults to ${context.getInstance().getUserMountedFilePath()}`,
context.getInstance().getUserMountedFilePath(),
)
.action((args, cmd) => {
main(args.openApi, resolve(args.outDir));
Expand Down
8 changes: 4 additions & 4 deletions src/cli/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ Further reading:
.addOption(
new Option(
"--open-api <uri or filepath>",
"URI or file path of OAS Document. Usually ${HASURA_CONFIGURATION_DIRECTORY}/swagger.json",
`URI or file path of OAS Document. Defaults to ${context.getInstance().getDefaultOpenapiDocumentFileUri()}`,
)
.default("./swagger.json")
.default(context.getInstance().getDefaultOpenapiDocumentFileUri())
.env("NDC_OAS_DOCUMENT_URI"),
)
.addOption(
new Option("--output-directory <directory>", "Output Directory")
.default("./")
new Option("--output-directory <directory>", `Output Directory. Defaults to ${context.getInstance().getUserMountedFilePath()}`)
.default(context.getInstance().getUserMountedFilePath())
.env("HASURA_CONFIGURATION_DIRECTORY"),
)
.addOption(
Expand Down