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..59824e16 --- /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 (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) { + copyIfMissingSymlinkPermission(srcpath, dstpath, error) + } +}