Skip to content

Commit d6496ab

Browse files
author
Divyendu Singh
authored
Merge pull request #140 from prisma/fix/windows-symlink
Fix Windows symlink EPERM error
2 parents b8f12d6 + 1329556 commit d6496ab

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

example/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"devDependencies": {
77
"@types/lodash": "4.14.91",
8+
"@types/node": "^11.13.0",
89
"serverless-plugin-typescript": "1.1.5"
910
}
1011
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"prepublish": "npm run build",
1313
"precommit": "npm run test",
14-
"build": "rm -rf dist && tsc",
14+
"build": "rimraf dist && tsc",
1515
"pretest": "npm run lint",
1616
"test": "jest",
1717
"lint": "tslint -c tslint.json 'src/**/*.ts'"
@@ -32,6 +32,7 @@
3232
"@types/lodash": "4.14.123",
3333
"jest": "24.5.0",
3434
"mock-fs": "4.8.0",
35+
"rimraf": "^2.6.3",
3536
"ts-jest": "24.0.1",
3637
"tslint": "5.14.0",
3738
"typescript": "^3.4.1"

src/index.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,17 @@ export class TypeScriptPlugin {
136136
}
137137

138138
async copyExtras() {
139-
// include node_modules into build
140-
if (!fs.existsSync(path.resolve(path.join(buildFolder, 'node_modules')))) {
141-
fs.symlinkSync(path.resolve('node_modules'), path.resolve(path.join(buildFolder, 'node_modules')), 'junction')
139+
const outPkgPath = path.resolve(path.join(buildFolder, 'package.json'))
140+
const outModulesPath = path.resolve(path.join(buildFolder, 'node_modules'))
141+
142+
// Link or copy node_modules and package.json to .build so Serverless can
143+
// exlcude devDeps during packaging
144+
if (!fs.existsSync(outModulesPath)) {
145+
await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')
142146
}
143147

144-
// include package.json into build so Serverless can exlcude devDeps during packaging
145-
if (!fs.existsSync(path.resolve(path.join(buildFolder, 'package.json')))) {
146-
fs.symlinkSync(path.resolve('package.json'), path.resolve(path.join(buildFolder, 'package.json')), 'file')
148+
if (!fs.existsSync(outPkgPath)) {
149+
await this.linkOrCopy(path.resolve('package.json'), outPkgPath, 'file')
147150
}
148151

149152
// include any "extras" from the "include" section
@@ -209,6 +212,23 @@ export class TypeScriptPlugin {
209212
fs.removeSync(path.join(this.originalServicePath, buildFolder))
210213
}
211214

215+
/**
216+
* Attempt to symlink a given path or directory and copy if it fails with an
217+
* `EPERM` error.
218+
*/
219+
private async linkOrCopy(
220+
srcPath: string,
221+
dstPath: string,
222+
type?: 'dir' | 'junction' | 'file'
223+
): Promise<void> {
224+
return fs.symlink(srcPath, dstPath, type)
225+
.catch(error => {
226+
if (error.code === 'EPERM' && error.errno === -4048) {
227+
return fs.copy(srcPath, dstPath)
228+
}
229+
throw error
230+
})
231+
}
212232
}
213233

214234
module.exports = TypeScriptPlugin

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -2974,7 +2974,7 @@ ret@~0.1.10:
29742974
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
29752975
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==
29762976

2977-
rimraf@^2.5.4, rimraf@^2.6.2:
2977+
rimraf@^2.5.4, rimraf@^2.6.2, rimraf@^2.6.3:
29782978
version "2.6.3"
29792979
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
29802980
integrity sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==

0 commit comments

Comments
 (0)