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

add basic function implementation #87

Merged
merged 9 commits into from
Jul 24, 2023
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
73 changes: 52 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/backend-function/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Be very careful editing this file. It is crafted to work around [this issue](https://github.com/npm/npm/issues/4479)

# First ignore everything
**/*

# Then add back in transpiled js and ts declaration files
!lib/**/*.js
!lib/**/*.d.ts

# Then ignore test js and ts declaration files
*.test.js
*.test.d.ts

# This leaves us with including only js and ts declaration files of functional code
3 changes: 3 additions & 0 deletions packages/backend-function/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../api-extractor.base.json"
}
28 changes: 28 additions & 0 deletions packages/backend-function/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@aws-amplify/backend-function",
"version": "0.1.0",
"type": "module",
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.js"
}
},
"types": "lib/index.d.ts",
"scripts": {
"update:api": "api-extractor run --local"
},
"dependencies": {
"@aws-amplify/function-construct": "^0.1.0",
"execa": "^7.1.1"
},
"devDependencies": {
"@aws-amplify/backend-engine": "^0.1.0",
"@aws-amplify/plugin-types": "^0.1.0"
},
"peerDependencies": {
"aws-cdk-lib": "~2.68.0",
"constructs": "^10.0.0"
}
}
67 changes: 67 additions & 0 deletions packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { beforeEach, describe, it } from 'node:test';
import { AmplifyFunctionFactory } from './factory.js';
import { App, Stack } from 'aws-cdk-lib';
import {
NestedStackResolver,
SingletonConstructContainer,
StackMetadataBackendOutputStorageStrategy,
} from '@aws-amplify/backend-engine';
import {
BackendOutputEntry,
BackendOutputStorageStrategy,
ConstructContainer,
} from '@aws-amplify/plugin-types';
import assert from 'node:assert';
import * as fs from 'fs';

describe('AmplifyFunctionFactory', () => {
let constructContainer: ConstructContainer;
let outputStorageStrategy: BackendOutputStorageStrategy<BackendOutputEntry>;

beforeEach(() => {
const app = new App();
const stack = new Stack(app, 'testStack');

constructContainer = new SingletonConstructContainer(
new NestedStackResolver(stack)
);

outputStorageStrategy = new StackMetadataBackendOutputStorageStrategy(
stack
);
});

it('creates singleton function instance', () => {
const functionFactory = new AmplifyFunctionFactory({
name: 'testFunc',
codeLocation: '../test-assets/test-lambda',
});
const instance1 = functionFactory.getInstance({
constructContainer,
outputStorageStrategy,
});
const instance2 = functionFactory.getInstance({
constructContainer,
outputStorageStrategy,
});
assert.strictEqual(instance1, instance2);
});

it('executes build command from directory where constructor is used', () => {
// the build command creates a test.txt file.
// We use the existence of this file as an indicator that the build command ran successfully
edwardfoyle marked this conversation as resolved.
Show resolved Hide resolved
const relativeTestFileLocation = '../test.txt';

new AmplifyFunctionFactory({
name: 'testFunc',
codeLocation: '../test-assets/test-lambda',
buildCommand: `touch ${relativeTestFileLocation}`,
}).getInstance({ constructContainer, outputStorageStrategy });

const testFilePath = new URL(relativeTestFileLocation, import.meta.url);

assert.ok(fs.existsSync(testFilePath));

fs.unlinkSync(testFilePath);
});
});
Loading