Skip to content

Fix symlink issue on windows #100

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}