From 00731e31099d217ec0c15fa82e71adff0fdb4387 Mon Sep 17 00:00:00 2001 From: edraitsev Date: Sun, 13 May 2018 11:39:56 +0200 Subject: [PATCH 1/2] Copy node_modules folder when symlink fails --- src/index.ts | 5 +++-- src/utils/index.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/utils/index.ts diff --git a/src/index.ts b/src/index.ts index ef134950..3c9a7ea8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { ServerlessOptions, ServerlessInstance, ServerlessFunction } from './typ import * as typescript from './typescript' import { watchFiles } from './watchFiles' +import { symlink } from './utils' // Folders const serverlessFolder = '.serverless' @@ -138,12 +139,12 @@ export class TypeScriptPlugin { async copyExtras() { // include node_modules into build if (!fs.existsSync(path.resolve(path.join(buildFolder, 'node_modules')))) { - fs.symlinkSync(path.resolve('node_modules'), path.resolve(path.join(buildFolder, 'node_modules'))) + symlink(path.resolve('node_modules'), path.resolve(path.join(buildFolder, 'node_modules'))) } // include package.json into build so Serverless can exlcude devDeps during packaging if (!fs.existsSync(path.resolve(path.join(buildFolder, 'package.json')))) { - fs.symlinkSync(path.resolve('package.json'), path.resolve(path.join(buildFolder, 'package.json'))) + symlink(path.resolve('package.json'), path.resolve(path.join(buildFolder, 'package.json'))) } // include any "extras" from the "include" section diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 00000000..56bc6c0e --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,28 @@ +import * as fs from 'fs-extra' + +export interface SymlinkException { + code: string + errno: number +} + +const isMissingSymlinkPermission = (error: SymlinkException): boolean => { + // Generally happens when no admin rights with UAC enabled on Windows. + return error.code === 'EPERM' && error.errno === -4048 +} + +const copyIfMissingSymlinkPermission = + (srcpath: string, dstpath: string, error: SymlinkException) => { + if (this.isMissingSymlinkPermission(error)) { + fs.copySync(srcpath, dstpath) + } else { + throw error + } + } + +export const symlink = (srcpath: string, dstpath: string, type?: string) => { + try { + fs.symlinkSync(srcpath, dstpath, type) + } catch (error) { + this.copyIfMissingSymlinkPermission(srcpath, dstpath, error) + } +} From 5b068744976a883931676200791b7e9f1de1ed74 Mon Sep 17 00:00:00 2001 From: EugeneDraitsev Date: Sun, 13 May 2018 11:48:22 +0200 Subject: [PATCH 2/2] Fixed some errors --- src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 56bc6c0e..59824e16 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -12,7 +12,7 @@ const isMissingSymlinkPermission = (error: SymlinkException): boolean => { const copyIfMissingSymlinkPermission = (srcpath: string, dstpath: string, error: SymlinkException) => { - if (this.isMissingSymlinkPermission(error)) { + if (isMissingSymlinkPermission(error)) { fs.copySync(srcpath, dstpath) } else { throw error @@ -23,6 +23,6 @@ export const symlink = (srcpath: string, dstpath: string, type?: string) => { try { fs.symlinkSync(srcpath, dstpath, type) } catch (error) { - this.copyIfMissingSymlinkPermission(srcpath, dstpath, error) + copyIfMissingSymlinkPermission(srcpath, dstpath, error) } }