-
Notifications
You must be signed in to change notification settings - Fork 161
feat: Func plugin to add/remove functions within function app #151
Conversation
src/index.ts
Outdated
import { AzureLoginPlugin } from './plugins/login/loginPlugin'; | ||
import { AzureApimServicePlugin } from './plugins/apim/apimServicePlugin'; | ||
import { AzureApimFunctionPlugin } from './plugins/apim/apimFunctionPlugin'; | ||
import Serverless from "serverless"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use double quotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some feedback. Let's discuss.
} | ||
|
||
public static getFunctionHandler(name: string) { | ||
return `"use strict"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this inline script into a separate template file. We can then use JS string interpolation (similar to vott resources) to include any dynamic string content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I've spent some time trying to make this work. My problem has been what kind of file to put the template in. Here are the options I've tried:
Template file options:
- TypeScript file
- Gets transpiled to JavaScript in build - doesn't keep template structure
- JavaScript file
- Can't be included in build
- Tried setting 'allowJs' to be true, but that conflicts with 'declarations' being true
- Text file
- Could make it work, but it would mean an extra step post-build of copying the text file over. Not sure that's warranted just to avoid the inline script
Other options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah, the ideal solution would be to be able to include a js file of the handler since that's technically what we're packaging, but if that isn't really an option, then inline seems reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a work item to re-review this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[AB#18775]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure where it should go and if it's out of scope of this pr, but we need some tests to enforce the right file structure for function zip folder.
src/plugins/func/azureFunc.test.ts
Outdated
expect(sls.cli.log).toBeCalledWith("Need to provide a name of function to remove") | ||
}); | ||
|
||
it("returns with pre-existing function", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy/pasta: should be returns with non-existing function
src/plugins/func/azureFunc.ts
Outdated
|
||
private createFunctionDir(name: string) { | ||
this.serverless.cli.log("Creating function dir"); | ||
fs.mkdirSync(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we worry about error here? in cases of permission issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll put a try catch here
src/plugins/func/funcUtils.ts
Outdated
|
||
private static defaultFunctionSlsObject(name: string) { | ||
return { | ||
handler: `${name}/index.handler`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't want ${name}/index.handler
, this will generate incorrect binding and function won't work.
just index.handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the structure that serverless.yml
needs to find the handler since it will live inside the directory {functionName
and inside the file index.js
, with the function name handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but the plugin will generate incorrect scriptFilePath
since function.json
now resides at the same level as index.json
src/test/mockFactory.ts
Outdated
|
||
public static createTestFunctionMetadata(name: string) { | ||
return { | ||
"handler": `${name}/index.handler`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above no ${name}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above :p
src/plugins/func/funcUtils.ts
Outdated
|
||
private static defaultFunctionSlsObject(name: string) { | ||
return { | ||
handler: `${name}/index.handler`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but the plugin will generate incorrect scriptFilePath
since function.json
now resides at the same level as index.json
src/test/mockFactory.ts
Outdated
|
||
public static createTestFunctionMetadata(name: string) { | ||
return { | ||
"handler": `${name}/index.handler`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above :p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm! only have one question about a use of string interpolation.
src/plugins/func/funcUtils.ts
Outdated
|
||
private static defaultFunctionSlsObject(name: string) { | ||
return { | ||
handler: `index.handler`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can change back to double quote since we're no longer interpolating a variable
const template = sls.utils.readFileSync(path); | ||
const names = params.keys(); | ||
const vals = params.values(); | ||
return new Function(...names, `return \`${template}\`;`)(...vals); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: can
`return \`${template}\`;`
be replace with
`return ${template};`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a string interpolation that won't happen here. We actually have the same function (kind of) in VoTT. This is expecting a file with templated variables. I'm actually not using this right now... I was using it when I had a javascript template file. But I might keep it in just in case we go back to that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just had a small question but looks good!
} | ||
|
||
public static getFunctionHandler(name: string) { | ||
return `"use strict"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah, the ideal solution would be to be able to include a js file of the handler since that's technically what we're packaging, but if that isn't really an option, then inline seems reasonable.
|
||
it("returns with missing name", async () => { | ||
const sls = MockFactory.createTestServerless(); | ||
const options = MockFactory.createTestServerlessOptions(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Would it make sense to add a default name to the options and then remove/override as needed? Just thinking if we'll need it for other function tests if we add more functionality, like the option to include the extension bundles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure. Let's add that in another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing earlier feedback.
} | ||
|
||
public static getFunctionHandler(name: string) { | ||
return `"use strict"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a work item to re-review this later.
sls func add -n {functionName}
andsls func remove -n {functionName}
func
,func:add
,func:remove
)serverless.yml
appropriately