@@ -77,7 +77,7 @@ export class TypeScriptPlugin {
77
77
await this . copyDependencies ( )
78
78
if ( this . isWatching ) {
79
79
emitedFiles . forEach ( filename => {
80
- const module = require . resolve ( path . resolve ( this . originalServicePath , filename ) )
80
+ const module = require . resolve ( path . resolve ( this . getRealServicePath ( ) , filename ) )
81
81
delete require . cache [ module ]
82
82
} )
83
83
}
@@ -114,7 +114,7 @@ export class TypeScriptPlugin {
114
114
115
115
get rootFileNames ( ) {
116
116
return typescript . extractFileNames (
117
- this . originalServicePath ,
117
+ this . getRealServicePath ( ) ,
118
118
this . serverless . service . provider . name ,
119
119
this . functions
120
120
)
@@ -145,7 +145,7 @@ export class TypeScriptPlugin {
145
145
146
146
this . isWatching = true
147
147
await new Promise ( ( resolve , reject ) => {
148
- watchFiles ( this . rootFileNames , this . originalServicePath , ( ) => {
148
+ watchFiles ( this . rootFileNames , this . getRealServicePath ( ) , ( ) => {
149
149
this . serverless . pluginManager . spawn ( 'invoke:local' ) . catch ( reject )
150
150
} )
151
151
} )
@@ -159,7 +159,7 @@ export class TypeScriptPlugin {
159
159
this . serverless . cli . log ( `Watching typescript files...` )
160
160
161
161
this . isWatching = true
162
- watchFiles ( this . rootFileNames , this . originalServicePath , this . compileTs . bind ( this ) )
162
+ watchFiles ( this . rootFileNames , this . getRealServicePath ( ) , this . compileTs . bind ( this ) )
163
163
}
164
164
165
165
async compileTs ( ) : Promise < string [ ] > {
@@ -168,9 +168,10 @@ export class TypeScriptPlugin {
168
168
169
169
if ( ! this . originalServicePath ) {
170
170
// Save original service path and functions
171
- this . originalServicePath = this . serverless . config . servicePath
171
+ // console.error('HEYO! ' + JSON.stringify({v: this.serverless['version'], serverlessPath: this.serverless.config['serverlessPath'], serviceDir: this.serverless.config['serviceDir'], slsServiceDir: this.serverless['serviceDir'], top: Object.keys(this.serverless), conf: Object.keys(this.serverless.config)}))
172
+ this . originalServicePath = this . getServicePath ( )
172
173
// Fake service path so that serverless will know what to zip
173
- this . serverless . config . servicePath = path . join ( this . originalServicePath , BUILD_FOLDER )
174
+ this . setServicePath ( path . join ( this . originalServicePath , BUILD_FOLDER ) )
174
175
}
175
176
let tsConfigFileLocation : string | undefined
176
177
if (
@@ -180,7 +181,7 @@ export class TypeScriptPlugin {
180
181
tsConfigFileLocation = this . serverless . service . custom . serverlessPluginTypescript . tsConfigFileLocation
181
182
}
182
183
const tsconfig = typescript . getTypescriptConfig (
183
- this . originalServicePath ,
184
+ this . getRealServicePath ( ) ,
184
185
tsConfigFileLocation ,
185
186
this . isWatching ? null : this . serverless . cli
186
187
)
@@ -189,14 +190,45 @@ export class TypeScriptPlugin {
189
190
190
191
const emitedFiles = await typescript . run ( this . rootFileNames , tsconfig )
191
192
this . serverless . cli . log ( 'Typescript compiled.' )
193
+ // this.setServicePath(this.originalServicePath)
194
+ // this.originalServicePath = null
192
195
return emitedFiles
193
196
}
194
197
195
- /** Link or copy extras such as node_modules or package.patterns definitions */
198
+ private getRealServicePath ( ) {
199
+ return this . originalServicePath || this . getServicePath ( )
200
+ }
201
+
202
+ private getServicePath ( ) : string {
203
+ const path = this . serverless . config . servicePath || this . serverless . config . serviceDir
204
+ // const path = 'servicePath' in this.serverless.config ? this.serverless.config.servicePath : this.serverless.config.serviceDir
205
+ if ( ! path ) {
206
+ throw new Error ( 'Could not find serverless serviceDir or servicePath' )
207
+ }
208
+ return path
209
+ }
210
+
211
+ private setServicePath ( value : string ) {
212
+ this . serverless . config . serviceDir = value
213
+ this . serverless . config . servicePath = value
214
+ // if ('serviceDir' in this.serverless.config) {
215
+ // // serverless V3 config
216
+ // }
217
+ // else {
218
+ // // Serverless <V3 config
219
+ // }
220
+ }
221
+
222
+ /**
223
+ * ~Link or~ copy extras such as node_modules or package.patterns definitions
224
+ * TODO: this says link or copy, it does not attempt to link. Should it? Or is the comment out of date?
225
+ **/
196
226
async copyExtras ( ) {
227
+
197
228
const { service } = this . serverless
198
229
199
230
const patterns = [ ...( service . package . include || [ ] ) , ...( service . package . patterns || [ ] ) ]
231
+ this . serverless . cli . log ( `TS: copyExtras: ${ JSON . stringify ( patterns ) } ` )
200
232
// include any "extras" from the "include" section
201
233
if ( patterns . length > 0 ) {
202
234
const files = await globby ( patterns )
@@ -222,6 +254,7 @@ export class TypeScriptPlugin {
222
254
* @param isPackaging Provided if serverless is packaging the service for deployment
223
255
*/
224
256
async copyDependencies ( isPackaging = false ) {
257
+ this . serverless . cli . log ( 'TS: copyDependencies' )
225
258
const outPkgPath = path . resolve ( path . join ( BUILD_FOLDER , 'package.json' ) )
226
259
const outModulesPath = path . resolve ( path . join ( BUILD_FOLDER , 'node_modules' ) )
227
260
@@ -252,24 +285,27 @@ export class TypeScriptPlugin {
252
285
async moveArtifacts ( ) : Promise < void > {
253
286
const { service } = this . serverless
254
287
288
+ this . serverless . cli . log ( 'TS: moveArtifacts .serverless' )
255
289
await fs . copy (
256
- path . join ( this . originalServicePath , BUILD_FOLDER , SERVERLESS_FOLDER ) ,
257
- path . join ( this . originalServicePath , SERVERLESS_FOLDER )
290
+ path . join ( this . getRealServicePath ( ) , BUILD_FOLDER , SERVERLESS_FOLDER ) ,
291
+ path . join ( this . getRealServicePath ( ) , SERVERLESS_FOLDER )
258
292
)
293
+ this . serverless . cli . log ( 'TS: post-copy' )
259
294
260
295
const layerNames = service . getAllLayers ( )
261
296
layerNames . forEach ( name => {
262
297
service . layers [ name ] . package . artifact = path . join (
263
- this . originalServicePath ,
298
+ this . getRealServicePath ( ) ,
264
299
SERVERLESS_FOLDER ,
265
300
path . basename ( service . layers [ name ] . package . artifact )
266
301
)
302
+ this . serverless . cli . log ( 'TS: ' + name + ' ' + service . layers [ name ] . package . artifact )
267
303
} )
268
304
269
305
if ( this . options . function ) {
270
306
const fn = service . functions [ this . options . function ]
271
307
fn . package . artifact = path . join (
272
- this . originalServicePath ,
308
+ this . getRealServicePath ( ) ,
273
309
SERVERLESS_FOLDER ,
274
310
path . basename ( fn . package . artifact )
275
311
)
@@ -280,27 +316,30 @@ export class TypeScriptPlugin {
280
316
const functionNames = service . getAllFunctions ( )
281
317
functionNames . forEach ( name => {
282
318
service . functions [ name ] . package . artifact = path . join (
283
- this . originalServicePath ,
319
+ this . getRealServicePath ( ) ,
284
320
SERVERLESS_FOLDER ,
285
321
path . basename ( service . functions [ name ] . package . artifact )
286
322
)
323
+ this . serverless . cli . log ( `TS: ${ name } - ${ service . functions [ name ] . package . artifact } ` )
287
324
} )
288
325
return
289
326
}
290
327
291
328
service . package . artifact = path . join (
292
- this . originalServicePath ,
329
+ this . getRealServicePath ( ) ,
293
330
SERVERLESS_FOLDER ,
294
331
path . basename ( service . package . artifact )
295
332
)
333
+ this . serverless . cli . log ( `TS: service.package.artifact: ${ service . package . artifact } ` )
296
334
}
297
335
298
336
async cleanup ( ) : Promise < void > {
337
+ this . serverless . cli . log ( 'TS: cleanup' )
299
338
await this . moveArtifacts ( )
300
339
// Restore service path
301
- this . serverless . config . servicePath = this . originalServicePath
340
+ this . setServicePath ( this . originalServicePath )
302
341
// Remove temp build folder
303
- fs . removeSync ( path . join ( this . originalServicePath , BUILD_FOLDER ) )
342
+ fs . removeSync ( path . join ( this . getServicePath ( ) , BUILD_FOLDER ) )
304
343
}
305
344
306
345
/**
0 commit comments