From 810f9b4c8a60b981d4c3209127af7eb2bdb602a5 Mon Sep 17 00:00:00 2001 From: Sam Chung Date: Mon, 13 Dec 2021 18:16:54 +1100 Subject: [PATCH] fix: correct package individually relative path for Windows (#244) --- src/helper.ts | 8 +++++++- src/tests/helper.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/helper.ts b/src/helper.ts index 2f873caf..4abd5b74 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -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'; @@ -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, + }; } } diff --git a/src/tests/helper.test.ts b/src/tests/helper.test.ts index 297b4ba6..6dad467a 100644 --- a/src/tests/helper.test.ts +++ b/src/tests/helper.test.ts @@ -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'; @@ -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 = {