From 816cd2640bdca41461482cc79ce580bdf80ac9b2 Mon Sep 17 00:00:00 2001 From: Julian Grinblat Date: Tue, 12 Jan 2021 01:20:14 +0900 Subject: [PATCH] fix(*): handle no node_modules directory --- src/index.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index a249ce58..f1ab38e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,16 @@ import { watchFiles } from './watchFiles' const SERVERLESS_FOLDER = '.serverless' const BUILD_FOLDER = '.build' +// handles broken symlinks +const symlinkExistsSync = (path: string) => { + try { + fs.lstatSync(path); + return true; + } catch (e) { + return false; + } +} + export class TypeScriptPlugin { private originalServicePath: string private isWatching: boolean @@ -191,14 +201,17 @@ export class TypeScriptPlugin { // copy development dependencies during packaging if (isPackaging) { - if (fs.existsSync(outModulesPath)) { + // symlinkExistsSync handles the broken symlink case + if (fs.existsSync(outModulesPath) || symlinkExistsSync(outModulesPath)) { fs.unlinkSync(outModulesPath) } - fs.copySync( - path.resolve('node_modules'), - path.resolve(path.join(BUILD_FOLDER, 'node_modules')) - ) + if (fs.existsSync(path.resolve('node_modules'))) { + fs.copySync( + path.resolve('node_modules'), + outModulesPath + ) + } } else { if (!fs.existsSync(outModulesPath)) { await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')