Skip to content

Commit

Permalink
feat: add support for dir/index handlers (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmarshall authored Dec 29, 2022
1 parent ad09477 commit 029194e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export function extractFunctionEntries(
func,
functionAlias,
};
} else if (fs.existsSync(path.join(cwd, path.join(fileName, 'index') + extension))) {
const entry = path.relative(cwd, path.join(fileName, 'index') + extension);
return {
entry: os.platform() === 'win32' ? entry.replace(/\\/g, '/') : entry,
func,
functionAlias,
};
}
}

Expand Down
34 changes: 33 additions & 1 deletion src/tests/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
import path from 'path';

import { extractFunctionEntries, flatDep, getDepsFromBundle, isESM } from '../helper';

Expand Down Expand Up @@ -47,6 +47,38 @@ describe('extractFunctionEntries', () => {
]);
});

it('should return entries for handlers which reference directories that contain index files', () => {
jest.mocked(fs.existsSync).mockImplementation((fPath) => {
return typeof fPath !== 'string' || fPath.endsWith('/index.ts');
});

const functionDefinitions = {
function1: {
events: [],
handler: 'dir1.handler',
},
function2: {
events: [],
handler: 'dir2.handler',
},
};

const fileNames = extractFunctionEntries(cwd, 'aws', functionDefinitions);

expect(fileNames).toStrictEqual([
{
entry: 'dir1/index.ts',
func: functionDefinitions['function1'],
functionAlias: 'function1',
},
{
entry: 'dir2/index.ts',
func: functionDefinitions['function2'],
functionAlias: 'function2',
},
]);
});

it('should return entries for handlers which reference files in folders in the working directory', () => {
jest.mocked(fs.existsSync).mockReturnValue(true);
const functionDefinitions = {
Expand Down

0 comments on commit 029194e

Please sign in to comment.