Skip to content

Commit 8d98a51

Browse files
author
John Reeves
committedOct 26, 2022
fix(serverless#281): add serviceDir to Serverless config
serviceDir and servicePath both are optional so we have to check. In index.ts, I'm setting both, because according to comments throughout Serverless code, they should actually be linked. Due to (I think) a bug, they're not. But there may be code that uses either/or, so they should both be set. Added setter and getter for this, to put that logic in one place. This doesn't fix serverless#281 yet, but it's a necessary part.
1 parent ac330c3 commit 8d98a51

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed
 

‎src/Serverless.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ declare namespace Serverless {
55
}
66

77
config: {
8-
servicePath: string
8+
servicePath?: string
9+
serviceDir?: string
910
}
1011

1112
service: {

‎src/index.ts

+55-16
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class TypeScriptPlugin {
7777
await this.copyDependencies()
7878
if (this.isWatching) {
7979
emitedFiles.forEach(filename => {
80-
const module = require.resolve(path.resolve(this.originalServicePath, filename))
80+
const module = require.resolve(path.resolve(this.getRealServicePath(), filename))
8181
delete require.cache[module]
8282
})
8383
}
@@ -114,7 +114,7 @@ export class TypeScriptPlugin {
114114

115115
get rootFileNames() {
116116
return typescript.extractFileNames(
117-
this.originalServicePath,
117+
this.getRealServicePath(),
118118
this.serverless.service.provider.name,
119119
this.functions
120120
)
@@ -145,7 +145,7 @@ export class TypeScriptPlugin {
145145

146146
this.isWatching = true
147147
await new Promise((resolve, reject) => {
148-
watchFiles(this.rootFileNames, this.originalServicePath, () => {
148+
watchFiles(this.rootFileNames, this.getRealServicePath(), () => {
149149
this.serverless.pluginManager.spawn('invoke:local').catch(reject)
150150
})
151151
})
@@ -159,7 +159,7 @@ export class TypeScriptPlugin {
159159
this.serverless.cli.log(`Watching typescript files...`)
160160

161161
this.isWatching = true
162-
watchFiles(this.rootFileNames, this.originalServicePath, this.compileTs.bind(this))
162+
watchFiles(this.rootFileNames, this.getRealServicePath(), this.compileTs.bind(this))
163163
}
164164

165165
async compileTs(): Promise<string[]> {
@@ -168,9 +168,10 @@ export class TypeScriptPlugin {
168168

169169
if (!this.originalServicePath) {
170170
// 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()
172173
// 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))
174175
}
175176
let tsConfigFileLocation: string | undefined
176177
if (
@@ -180,7 +181,7 @@ export class TypeScriptPlugin {
180181
tsConfigFileLocation = this.serverless.service.custom.serverlessPluginTypescript.tsConfigFileLocation
181182
}
182183
const tsconfig = typescript.getTypescriptConfig(
183-
this.originalServicePath,
184+
this.getRealServicePath(),
184185
tsConfigFileLocation,
185186
this.isWatching ? null : this.serverless.cli
186187
)
@@ -189,14 +190,45 @@ export class TypeScriptPlugin {
189190

190191
const emitedFiles = await typescript.run(this.rootFileNames, tsconfig)
191192
this.serverless.cli.log('Typescript compiled.')
193+
// this.setServicePath(this.originalServicePath)
194+
// this.originalServicePath = null
192195
return emitedFiles
193196
}
194197

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+
**/
196226
async copyExtras() {
227+
197228
const { service } = this.serverless
198229

199230
const patterns = [...(service.package.include || []), ...(service.package.patterns || [])]
231+
this.serverless.cli.log(`TS: copyExtras: ${JSON.stringify(patterns)}`)
200232
// include any "extras" from the "include" section
201233
if (patterns.length > 0) {
202234
const files = await globby(patterns)
@@ -222,6 +254,7 @@ export class TypeScriptPlugin {
222254
* @param isPackaging Provided if serverless is packaging the service for deployment
223255
*/
224256
async copyDependencies(isPackaging = false) {
257+
this.serverless.cli.log('TS: copyDependencies')
225258
const outPkgPath = path.resolve(path.join(BUILD_FOLDER, 'package.json'))
226259
const outModulesPath = path.resolve(path.join(BUILD_FOLDER, 'node_modules'))
227260

@@ -252,24 +285,27 @@ export class TypeScriptPlugin {
252285
async moveArtifacts(): Promise<void> {
253286
const { service } = this.serverless
254287

288+
this.serverless.cli.log('TS: moveArtifacts .serverless')
255289
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)
258292
)
293+
this.serverless.cli.log('TS: post-copy')
259294

260295
const layerNames = service.getAllLayers()
261296
layerNames.forEach(name => {
262297
service.layers[name].package.artifact = path.join(
263-
this.originalServicePath,
298+
this.getRealServicePath(),
264299
SERVERLESS_FOLDER,
265300
path.basename(service.layers[name].package.artifact)
266301
)
302+
this.serverless.cli.log('TS: ' + name + ' ' + service.layers[name].package.artifact)
267303
})
268304

269305
if (this.options.function) {
270306
const fn = service.functions[this.options.function]
271307
fn.package.artifact = path.join(
272-
this.originalServicePath,
308+
this.getRealServicePath(),
273309
SERVERLESS_FOLDER,
274310
path.basename(fn.package.artifact)
275311
)
@@ -280,27 +316,30 @@ export class TypeScriptPlugin {
280316
const functionNames = service.getAllFunctions()
281317
functionNames.forEach(name => {
282318
service.functions[name].package.artifact = path.join(
283-
this.originalServicePath,
319+
this.getRealServicePath(),
284320
SERVERLESS_FOLDER,
285321
path.basename(service.functions[name].package.artifact)
286322
)
323+
this.serverless.cli.log(`TS: ${name} - ${service.functions[name].package.artifact}`)
287324
})
288325
return
289326
}
290327

291328
service.package.artifact = path.join(
292-
this.originalServicePath,
329+
this.getRealServicePath(),
293330
SERVERLESS_FOLDER,
294331
path.basename(service.package.artifact)
295332
)
333+
this.serverless.cli.log(`TS: service.package.artifact: ${service.package.artifact}`)
296334
}
297335

298336
async cleanup(): Promise<void> {
337+
this.serverless.cli.log('TS: cleanup')
299338
await this.moveArtifacts()
300339
// Restore service path
301-
this.serverless.config.servicePath = this.originalServicePath
340+
this.setServicePath(this.originalServicePath)
302341
// Remove temp build folder
303-
fs.removeSync(path.join(this.originalServicePath, BUILD_FOLDER))
342+
fs.removeSync(path.join(this.getServicePath(), BUILD_FOLDER))
304343
}
305344

306345
/**

0 commit comments

Comments
 (0)
Please sign in to comment.