Skip to content

Commit 59bcdc6

Browse files
authored
Merge pull request #9 from BeyerleinDigital/5-create-function-generator
feat(generators): Implement add-function
2 parents 7ed39f4 + 705448b commit 59bcdc6

File tree

10 files changed

+148
-0
lines changed

10 files changed

+148
-0
lines changed

packages/azure-functions/generators.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"factory": "./src/generators/create-project/generator",
88
"schema": "./src/generators/create-project/schema.json",
99
"description": "Generate an Azure Functions project."
10+
},
11+
"add-function": {
12+
"factory": "./src/generators/add-function/generator",
13+
"schema": "./src/generators/add-function/schema.json",
14+
"description": "Add an Azure Function to an Azure Function project."
1015
}
1116
}
1217
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get"
10+
]
11+
},
12+
{
13+
"type": "http",
14+
"direction": "out",
15+
"name": "res"
16+
}
17+
],
18+
"scriptFile": "<%= offsetFromRoot %>dist/apps/<%= projectName %>/<%= functionName %>/index.mjs"
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { AzureFunction, Context, HttpRequest } from '@azure/functions';
2+
3+
const httpTrigger: AzureFunction = async function (ctx: Context, req: HttpRequest): Promise<void> {
4+
// do something awesome here
5+
};
6+
7+
export default httpTrigger;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "queueTrigger",
5+
"direction": "in",
6+
"name": "myQueueItem",
7+
"queueName": "myqueue-items",
8+
"connection": "MyStorageConnectionAppSetting"
9+
}
10+
],
11+
"scriptFile": "<%= offsetFromRoot %>dist/apps/<%= projectName %>/<%= functionName %>/index.mjs"
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { AzureFunction, Context } from '@azure/functions';
2+
3+
const queueTrigger: AzureFunction = async function (ctx: Context, queueMessage: string): Promise<void> {
4+
// do something awesome here
5+
};
6+
7+
export default queueTrigger;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"bindings": [
3+
{
4+
"schedule": "0 */5 * * * *",
5+
"name": "timer",
6+
"type": "timerTrigger",
7+
"direction": "in"
8+
}
9+
],
10+
"scriptFile": "<%= offsetFromRoot %>dist/apps/<%= projectName %>/<%= functionName %>/index.mjs"
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { AzureFunction, Context } from '@azure/functions';
2+
3+
const timerTrigger: AzureFunction = async function (ctx: Context, timer: unknown): Promise<void> {
4+
// do something awesome here
5+
};
6+
7+
export default timerTrigger;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
generateFiles,
3+
names,
4+
offsetFromRoot,
5+
readProjectConfiguration,
6+
Tree,
7+
} from '@nrwl/devkit';
8+
import * as path from 'path';
9+
import { AddAzureFunctionGeneratorSchema, NormalizedSchema } from './schema';
10+
11+
function normalizeOptions(
12+
tree: Tree,
13+
options: AddAzureFunctionGeneratorSchema
14+
): NormalizedSchema {
15+
const projectRoot = readProjectConfiguration(tree, options.project).root;
16+
const name = names(options.name).fileName;
17+
const functionRoot = `${projectRoot}/${name}`;
18+
19+
return {
20+
...options,
21+
name,
22+
functionRoot,
23+
type: options.type || 'http',
24+
};
25+
}
26+
27+
export default async function (
28+
tree: Tree,
29+
options: AddAzureFunctionGeneratorSchema
30+
) {
31+
const { name, project, functionRoot, type } = normalizeOptions(tree, options);
32+
33+
const srcFolder = path.join(__dirname, 'files', type);
34+
35+
generateFiles(tree, srcFolder, functionRoot, {
36+
tmpl: '',
37+
projectName: project,
38+
functionName: name,
39+
offsetFromRoot: offsetFromRoot(functionRoot),
40+
});
41+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type AzureFunctionType = 'http' | 'timer' | 'queue';
2+
3+
export interface AddAzureFunctionGeneratorSchema {
4+
name: string;
5+
project: string;
6+
type?: AzureFunctionType;
7+
}
8+
9+
interface NormalizedSchema extends AddAzureFunctionGeneratorSchema {
10+
type: AzureFunctionType;
11+
functionRoot: string;
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"cli": "nx",
4+
"$id": "AddFunction",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string",
9+
"description": "Function App name",
10+
"$default": {
11+
"$source": "argv",
12+
"index": 0
13+
}
14+
},
15+
"project": {
16+
"type": "string",
17+
"description": "Project"
18+
},
19+
"type": {
20+
"type": "string",
21+
"description": "Trigger type",
22+
"enum": ["http", "timer", "queue"],
23+
"$default": "http"
24+
}
25+
},
26+
"required": ["project", "name"]
27+
}

0 commit comments

Comments
 (0)