-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate-entity-boilerplate.js
88 lines (67 loc) · 2.67 KB
/
generate-entity-boilerplate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require('fs');
const entities = {};
const foldersToExclude = ['./node_modules', './ui', './__test', './dist'];
// Grabs all classes derived from DurableEntity and puts them into entities
function collectEntitiesInFolder(folderName) {
for (const fileName of fs.readdirSync(folderName)) {
const fullPath = folderName + '/' + fileName;
if (fs.lstatSync(fullPath).isDirectory() && (!foldersToExclude.includes(fullPath))) {
collectEntitiesInFolder(fullPath);
continue;
}
if (fileName.endsWith('.ts')) {
const code = fs.readFileSync(fullPath, { encoding: 'utf8' });
const match = /class (\w+) extends DurableEntity</i.exec(code);
if (!!match) {
entities[match[1]] = fullPath;
}
}
}
}
collectEntitiesInFolder('.');
for (const entityName in entities) {
var importFileName = entities[entityName];
importFileName = '.' + importFileName.substr(0, importFileName.length - 3);
if (!fs.existsSync(entityName)) {
fs.mkdirSync(entityName);
}
const indexTsFileName = entityName + '/index.ts';
if (fs.existsSync(indexTsFileName)) {
// TODO: validate file contents
console.log(`#durable-mvc: ${indexTsFileName} already exists - skipping`);
} else {
console.log(`#durable-mvc: generating ${indexTsFileName}`);
const code = `
import * as DurableFunctions from "durable-functions"
import { ${entityName} } from '${importFileName}';
export default DurableFunctions.entity((ctx) => new ${entityName}(ctx as any).handleSignal());
`;
fs.writeFileSync(indexTsFileName, code);
}
const functionJsonFileName = entityName + '/function.json';
if (fs.existsSync(functionJsonFileName)) {
// TODO: validate file contents
console.log(`#durable-mvc: ${functionJsonFileName} already exists - skipping`);
} else {
console.log(`#durable-mvc: generating ${functionJsonFileName}`);
const functionJson = {
bindings: [
{
name: 'context',
type: 'entityTrigger',
direction: 'in'
},
{
type: 'signalR',
name: 'signalRMessages',
hubName: '%AzureSignalRHubName%',
connectionStringSetting: 'AzureSignalRConnectionString',
direction:'out'
}
],
disabled: false,
scriptFile: `../dist/${entityName}/index.js`
};
fs.writeFileSync(functionJsonFileName, JSON.stringify(functionJson, null, 4));
}
}