Skip to content

Commit 3d4c92f

Browse files
committed
fix: add env vars if they don't exist
1 parent a5ac978 commit 3d4c92f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/nextjs/nextjs-wizard.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,20 @@ export async function addOrUpdateEnvironmentVariables({
382382
);
383383
} else {
384384
try {
385-
const newContent = dotEnvFileContent.replace(
386-
/^NEXT_PUBLIC_POSTHOG_KEY=.*$/m,
387-
`NEXT_PUBLIC_POSTHOG_KEY=${projectApiKey}`,
388-
);
385+
let newContent = dotEnvFileContent;
386+
387+
if (dotEnvFileContent.match(/^NEXT_PUBLIC_POSTHOG_KEY=.*$/m)) {
388+
newContent = dotEnvFileContent.replace(
389+
/^NEXT_PUBLIC_POSTHOG_KEY=.*$/m,
390+
`NEXT_PUBLIC_POSTHOG_KEY=${projectApiKey}`,
391+
);
392+
} else {
393+
if (!dotEnvFileContent.endsWith('\n')) {
394+
newContent += '\n';
395+
}
396+
newContent += envVarContent;
397+
}
398+
389399
await fs.promises.writeFile(targetEnvFilePath, newContent, {
390400
encoding: 'utf8',
391401
flag: 'w',

0 commit comments

Comments
 (0)