Skip to content

Commit a776ac8

Browse files
authored
fix user mounted file paths for ddn workspace (#97)
1 parent 56ecbac commit a776ac8

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

src/app/context.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const FUNCTIONS_TS_FILE_TEMPLATE_FILE_NAME = "functions.ejs"; // name of the tem
4040

4141
const NODE_VERSION = "node20";
4242

43+
const OPENAPI_SWAGGER_FILE_NAME = "swagger.json";
44+
const DEFAULT_CONFIGURATION_DIRECTORY = "/etc/connector/";
45+
4346
/**
4447
* Context is a singleton class that holds the configuration of the app
4548
*/
@@ -189,4 +192,29 @@ export class Context {
189192
public getNodeVersion(): string {
190193
return NODE_VERSION;
191194
}
195+
196+
/**
197+
* Use this function to get filepaths for user files relating to the connector
198+
* The DDN CLI is responsible for setting the correct env vars
199+
* 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
200+
*/
201+
public getUserMountedFilePath(): string {
202+
// Check for HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH environment variable
203+
if (process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH) {
204+
return process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH;
205+
}
206+
207+
// Check for HASURA_CONFIGURATION_DIRECTORY environment variable
208+
if (process.env.HASURA_CONFIGURATION_DIRECTORY) {
209+
return process.env.HASURA_CONFIGURATION_DIRECTORY;
210+
}
211+
212+
// If neither variable is present, log warning and return default path
213+
logger.warn(`Neither HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH nor HASURA_CONFIGURATION_DIRECTORY environment variables are set. Using default path (${DEFAULT_CONFIGURATION_DIRECTORY}).`);
214+
return DEFAULT_CONFIGURATION_DIRECTORY;
215+
}
216+
217+
public getDefaultOpenapiDocumentFileUri(): string {
218+
return path.join(this.getUserMountedFilePath(), OPENAPI_SWAGGER_FILE_NAME);
219+
}
192220
}

src/app/writer/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as types from "../types";
66
import * as logger from "../../util/logger";
77
import { exit } from "process";
88
import { execSync } from "child_process";
9+
import * as context from "../context";
910

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

2425
logger.info("running npm install :: installing dependencies");
25-
if (process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH) {
26-
execSync("npm install ", { stdio: "inherit", cwd: process.env.HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH });
27-
} else {
28-
execSync("npm install ", { stdio: "inherit" });
29-
}
30-
26+
const mountedPath = context.getInstance().getUserMountedFilePath();
27+
execSync("npm install ", { stdio: "inherit", cwd: mountedPath });
3128
logger.info("all dependencies installed");
3229
} catch (e) {
3330
if (e instanceof apiWriter.SimilarFileContentsError) {

src/cli/init.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Command } from "commander";
22
import { resolve } from "path";
33
import * as logger from "../util/logger";
4+
import * as context from "../app/context";
45

56
export const cmd = new Command("init")
67
.description(
@@ -15,13 +16,13 @@ Further reading:
1516
)
1617
.option(
1718
"--open-api <value>",
18-
"URI of OAS Document. Defaults to /etc/connector/swagger.json",
19-
"/etc/connector/swagger.json",
19+
`URI of OAS Document. Defaults to ${context.getInstance().getDefaultOpenapiDocumentFileUri()}`,
20+
context.getInstance().getDefaultOpenapiDocumentFileUri(),
2021
)
2122
.option(
2223
"--out-dir <value>",
23-
"Output Directory. Defaults to /etc/connector/",
24-
"/etc/connector/",
24+
`Output Directory. Defaults to ${context.getInstance().getUserMountedFilePath()}`,
25+
context.getInstance().getUserMountedFilePath(),
2526
)
2627
.action((args, cmd) => {
2728
main(args.openApi, resolve(args.outDir));

src/cli/update.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ Further reading:
1818
.addOption(
1919
new Option(
2020
"--open-api <uri or filepath>",
21-
"URI or file path of OAS Document. Usually ${HASURA_CONFIGURATION_DIRECTORY}/swagger.json",
21+
`URI or file path of OAS Document. Defaults to ${context.getInstance().getDefaultOpenapiDocumentFileUri()}`,
2222
)
23-
.default("./swagger.json")
23+
.default(context.getInstance().getDefaultOpenapiDocumentFileUri())
2424
.env("NDC_OAS_DOCUMENT_URI"),
2525
)
2626
.addOption(
27-
new Option("--output-directory <directory>", "Output Directory")
28-
.default("./")
27+
new Option("--output-directory <directory>", `Output Directory. Defaults to ${context.getInstance().getUserMountedFilePath()}`)
28+
.default(context.getInstance().getUserMountedFilePath())
2929
.env("HASURA_CONFIGURATION_DIRECTORY"),
3030
)
3131
.addOption(

0 commit comments

Comments
 (0)