MCPcopy
hub / github.com/triggerdotdev/trigger.dev / setEnvironmentVariable

Function setEnvironmentVariable

packages/cli/src/utils/env.ts:53–91  ·  view source on GitHub ↗
(
  dir: string,
  fileName: string,
  variableName: string,
  value: string,
  replaceValue: boolean = true,
  renderer: (value: string) => string = (value) => value
)

Source from the content-addressed store, hash-verified

51}
52
53async function setEnvironmentVariable(
54 dir: string,
55 fileName: string,
56 variableName: string,
57 value: string,
58 replaceValue: boolean = true,
59 renderer: (value: string) => string = (value) => value
60) {
61 const path = pathModule.join(dir, fileName);
62 const envFileExists = await pathExists(path);
63
64 if (!envFileExists) {
65 await fs.writeFile(path, "");
66 }
67
68 const envFileContent = await fs.readFile(path, "utf-8");
69
70 if (envFileContent.includes(variableName)) {
71 if (!replaceValue) {
72 logger.info(
73 `☑ Skipping setting ${variableName}=${renderer(value)} because it already exists`
74 );
75 return;
76 }
77 // Update the existing value
78 const updatedEnvFileContent = envFileContent.replace(
79 new RegExp(`${variableName}=.*\\n`, "g"),
80 `${variableName}=${value}\n`
81 );
82
83 await fs.writeFile(path, updatedEnvFileContent);
84
85 logger.success(`✔ Set ${variableName}=${renderer(value)} in ${fileName}`);
86 } else {
87 await fs.appendFile(path, `\n${variableName}=${value}`);
88
89 logger.success(`✔ Added ${variableName}=${renderer(value)} to ${fileName}`);
90 }
91}

Calls 3

pathExistsFunction · 0.90
replaceMethod · 0.80
infoMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…