Skip to content

Commit 1329556

Browse files
committed
fix(pencil): Copy on failed symlink for Windows
Closes #89, #23, #103, #24.
1 parent 6afc256 commit 1329556

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

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

0 commit comments

Comments
 (0)