Skip to content

Commit

Permalink
fix: correct package individually relative path for Windows (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Dec 13, 2021
1 parent 273a3a6 commit 810f9b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import * as os from 'os';
import { uniq } from 'ramda';
import * as Serverless from 'serverless';
import * as matchAll from 'string.prototype.matchall';
Expand Down Expand Up @@ -48,7 +49,12 @@ export function extractFileNames(
for (const extension of extensions) {
// Check if the .{extension} files exists. If so return that to watch
if (fs.existsSync(path.join(cwd, fileName + extension))) {
return { entry: path.relative(cwd, fileName + extension), func, functionAlias };
const entry = path.relative(cwd, fileName + extension);
return {
entry: os.platform() === 'win32' ? entry.replace(/\\/g, '/') : entry,
func,
functionAlias,
};
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/tests/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import * as os from 'os';
import { mocked } from 'ts-jest/utils';

import { extractFileNames } from '../helper';
Expand Down Expand Up @@ -102,6 +104,28 @@ describe('extractFileNames', () => {
]);
});

it('should return entries for handlers on a Windows platform', () => {
mocked(fs.existsSync).mockReturnValue(true);
jest.spyOn(path, 'relative').mockReturnValueOnce('src\\file1.ts');
jest.spyOn(os, 'platform').mockReturnValueOnce('win32');
const functionDefinitions = {
function1: {
events: [],
handler: 'file1.handler',
},
};

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

expect(fileNames).toStrictEqual([
{
entry: 'src/file1.ts',
func: functionDefinitions['function1'],
functionAlias: 'function1',
},
]);
});

it('should throw an error if the handlers reference a file which does not exist', () => {
mocked(fs.existsSync).mockReturnValue(false);
const functionDefinitions = {
Expand Down

0 comments on commit 810f9b4

Please sign in to comment.