Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix Windows symlink EPERM error #140

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2019
Merged
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
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"devDependencies": {
"@types/lodash": "4.14.91",
"@types/node": "^11.13.0",
"serverless-plugin-typescript": "1.1.5"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"prepublish": "npm run build",
"precommit": "npm run test",
"build": "rm -rf dist && tsc",
"build": "rimraf dist && tsc",
"pretest": "npm run lint",
"test": "jest",
"lint": "tslint -c tslint.json 'src/**/*.ts'"
Expand All @@ -32,6 +32,7 @@
"@types/lodash": "4.14.123",
"jest": "24.5.0",
"mock-fs": "4.8.0",
"rimraf": "^2.6.3",
"ts-jest": "24.0.1",
"tslint": "5.14.0",
"typescript": "^3.4.1"
Expand Down
32 changes: 26 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,17 @@ 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')), 'junction')
const outPkgPath = path.resolve(path.join(buildFolder, 'package.json'))
const outModulesPath = path.resolve(path.join(buildFolder, 'node_modules'))

// Link or copy node_modules and package.json to .build so Serverless can
// exlcude devDeps during packaging
if (!fs.existsSync(outModulesPath)) {
await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction')
}

// 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')), 'file')
if (!fs.existsSync(outPkgPath)) {
await this.linkOrCopy(path.resolve('package.json'), outPkgPath, 'file')
}

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

/**
* Attempt to symlink a given path or directory and copy if it fails with an
* `EPERM` error.
*/
private async linkOrCopy(
srcPath: string,
dstPath: string,
type?: 'dir' | 'junction' | 'file'
): Promise<void> {
return fs.symlink(srcPath, dstPath, type)
.catch(error => {
if (error.code === 'EPERM' && error.errno === -4048) {
return fs.copy(srcPath, dstPath)
}
throw error
})
}
}

module.exports = TypeScriptPlugin
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2974,7 +2974,7 @@ ret@~0.1.10:
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==

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