Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added static context to clear the .amplify/generated/env Directory before synthesis #2054

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
743354e
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
ab7f76f
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
7c55adc
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
36727ba
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
aa2f7d9
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
0302517
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
0adf873
chore: add changeset
vigy02 Sep 19, 2024
a5b5d3c
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
753701a
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
955cecc
fix: clearing the .amplify/generated/env/ before synthesis
vigy02 Sep 19, 2024
2a2b188
Merge branch 'main' of https://github.com/vigy02/amplify-backend
vigy02 Sep 19, 2024
fc0be7e
fix: Clear generated env directory before shim generation
vigy02 Sep 20, 2024
6c916e0
fix: Clear generated env directory before shim generation
vigy02 Sep 20, 2024
cab3f1a
fix: Clear generated env directory before shim generation
vigy02 Sep 20, 2024
954e1c0
fix: Clear generated env directory before shim generation
vigy02 Sep 20, 2024
d9212d8
chore: add changeset
vigy02 Sep 20, 2024
c09e1ee
fix: Clear generated env directory before shim generation
vigy02 Sep 20, 2024
05e1fd5
fix: Clear generated env directory before shim generation
vigy02 Sep 20, 2024
7714224
Merge branch 'issue-dir-clean'
vigy02 Sep 20, 2024
91faece
Merge branch 'aws-amplify:main' into main
vigy02 Sep 20, 2024
3f31b86
fix: added static context to clear the .amplify/generated/env Directo…
vigy02 Sep 23, 2024
863e92e
fix: added static context to clear the .amplify/generated/env Directo…
vigy02 Sep 23, 2024
9b0fd91
fix: added static context to clear the .amplify/generated/env Directo…
vigy02 Sep 23, 2024
75a0ee2
fix: Clear generated env directory before shim generation
vigy02 Sep 23, 2024
1b26f9a
Merge branch 'issue-dir-clean'
vigy02 Sep 23, 2024
5977d8f
Merge branch 'aws-amplify:main' into main
vigy02 Sep 23, 2024
94416a7
fix: Clear generated env directory before shim generation
vigy02 Sep 23, 2024
83a5cd8
Merge branch 'main' of https://github.com/vigy02/amplify-backend
vigy02 Sep 23, 2024
cc69686
fix: Clear generated env directory before shim generation
vigy02 Sep 23, 2024
f4348e6
fix: Clear generated env directory before shim generation
vigy02 Sep 23, 2024
b886eed
fix: Clear generated env directory before shim generation
vigy02 Sep 23, 2024
4f2d18c
fix: added static context to clear the .amplify/generated/env Directo…
vigy02 Sep 23, 2024
7f78fc8
fix: added static context to clear the .amplify/generated/env Directo…
vigy02 Sep 23, 2024
5d0d04a
fix: Clear generated env directory before shim generation
vigy02 Sep 24, 2024
34eb820
fix: Clear generated env directory before shim generation
vigy02 Sep 24, 2024
5d557d3
Merge branch 'aws-amplify:main' into clear-env-issue
vigy02 Sep 24, 2024
223ced4
fix: Clear generated env directory before shim generation
vigy02 Sep 24, 2024
7fc8e13
fix: Clear generated env directory before shim generation
vigy02 Sep 25, 2024
cfd19f7
fix: Clear generated env directory before shim generation
vigy02 Sep 25, 2024
4047803
Update the test case description
vigy02 Oct 8, 2024
2b312c4
Merge branch 'main' into e2e/vigy/clear-env-issue
vigy02 Oct 10, 2024
a6fcbcf
Merge branch 'main' into e2e/vigy/clear-env-issue
vigy02 Nov 6, 2024
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
6 changes: 6 additions & 0 deletions .changeset/dirty-years-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@aws-amplify/backend-function': patch
'@aws-amplify/backend': patch
---

fix: Clear generated env directory before shim generation
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { describe, it, mock } from 'node:test';
import { beforeEach, describe, it, mock } from 'node:test';
import fs from 'fs';
import fsp from 'fs/promises';
import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js';
import assert from 'assert';
import { pathToFileURL } from 'url';
import path from 'path';

void describe('FunctionEnvironmentTypeGenerator', () => {
beforeEach(() => {
resetFactoryGlobalState();
});
void it('generates a type definition file', () => {
const fsOpenSyncMock = mock.method(fs, 'openSync');
const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null);
Expand Down Expand Up @@ -101,4 +105,54 @@ void describe('FunctionEnvironmentTypeGenerator', () => {

mock.restoreAll();
});
void it('clears the generated env directory even if there are multiple calls', async () => {
const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true);
const fsRmSyncMock = mock.method(fs, 'rmSync', () => {});

new FunctionEnvironmentTypeGenerator('testFunction1');

assert.equal(fsExistsSyncMock.mock.calls.length, 1);
assert.equal(fsRmSyncMock.mock.calls.length, 1);

new FunctionEnvironmentTypeGenerator('testFunction2');

assert.equal(fsExistsSyncMock.mock.calls.length, 1);
assert.equal(fsRmSyncMock.mock.calls.length, 1);

fsExistsSyncMock.mock.restore();
fsRmSyncMock.mock.restore();
});
void it("don't clear the generated env directory if it doesn't exist", async () => {
const fsExistsSyncMock = mock.method(fs, 'existsSync', () => false);
const fsRmSyncMock = mock.method(fs, 'rmSync', () => {});

new FunctionEnvironmentTypeGenerator('testFunction');

assert.equal(fsExistsSyncMock.mock.calls.length, 1);
assert.equal(fsRmSyncMock.mock.calls.length, 0);

fsExistsSyncMock.mock.restore();
fsRmSyncMock.mock.restore();
});
void it('ensure correct directory is deleted', async () => {
const pathToDelete = path.join(
process.cwd(),
'.amplify',
'generated',
'env'
);
const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true);
const fsRmSyncMock = mock.method(fs, 'rmSync', () => {});

new FunctionEnvironmentTypeGenerator('testFunction');

assert.equal(fsExistsSyncMock.mock.calls.length, 1);
assert.equal(fsRmSyncMock.mock.calls[0].arguments[0], pathToDelete);

fsExistsSyncMock.mock.restore();
fsRmSyncMock.mock.restore();
});
const resetFactoryGlobalState = () => {
FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized = false;
};
});
32 changes: 28 additions & 4 deletions packages/backend-function/src/function_env_type_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { EOL } from 'os';
* Generates a typed process.env shim for environment variables
*/
export class FunctionEnvironmentTypeGenerator {
// Using this to ensure directory is cleared once per synthesis
static isEnvDirectoryInitialized = false;
private readonly header =
'// This file is auto-generated by Amplify. Edits will be overwritten.';

// The variable gets updated when the fully typed file is updated.
private readonly envAssignment = 'export const env = process.env';

private typeDefFilePath: string;
Expand All @@ -21,9 +22,14 @@ export class FunctionEnvironmentTypeGenerator {
* Initialize typed process.env shim file name and location
*/
constructor(private readonly functionName: string) {
this.typeDefFilePath = `${process.cwd()}/.amplify/generated/env/${
this.functionName
}.ts`;
this.typeDefFilePath = path.join(
process.cwd(),
'.amplify',
'generated',
'env',
`${this.functionName}.ts`
);
this.clearGeneratedEnvDirectory();
}

/**
Expand Down Expand Up @@ -97,4 +103,22 @@ export class FunctionEnvironmentTypeGenerator {

fs.writeFileSync(this.typeDefFilePath, content);
};
/**
* Clear existing files and subdirectories in the generated env directory
*/
private clearGeneratedEnvDirectory = (): void => {
if (FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized) {
return;
}
const pathToDelete = path.join(
process.cwd(),
'.amplify',
'generated',
'env'
);
if (fs.existsSync(pathToDelete)) {
fs.rmSync(pathToDelete, { recursive: true, force: true });
FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized = true;
}
};
}
Loading