@@ -136,14 +136,17 @@ export class TypeScriptPlugin {
136
136
}
137
137
138
138
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' )
142
146
}
143
147
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' )
147
150
}
148
151
149
152
// include any "extras" from the "include" section
@@ -209,6 +212,23 @@ export class TypeScriptPlugin {
209
212
fs . removeSync ( path . join ( this . originalServicePath , buildFolder ) )
210
213
}
211
214
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
+ }
212
232
}
213
233
214
234
module . exports = TypeScriptPlugin
0 commit comments