-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpatch_template_variables.js
More file actions
175 lines (140 loc) · 7.42 KB
/
patch_template_variables.js
File metadata and controls
175 lines (140 loc) · 7.42 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { readFileSync, writeFile } from 'fs';
import path from 'path';
const workflowSuffix = '_#workflowname#';
const foldersPath = process.argv.slice(2);
const patchTemplateVariables = async (workflowFolderPath, workflowManifest) => {
const manifestFilePath = `./${workflowFolderPath}/manifest.json`;
const workflowFilePath = `./${workflowFolderPath}/workflow.json`;
const workflow = getFileContentInJSON(workflowFilePath);
console.log(`[${workflowFolderPath}] Patching parameter and connection names...`);
const { manifest: updatedManifest, workflow: updatedWorkflow } = await updateConnections(workflowManifest, JSON.stringify(workflow));
const { manifest: finalUpdatedManifest, workflow: finalUpdatedWorkflow} = await updateParameters(updatedManifest, updatedWorkflow);
writeFile(manifestFilePath, JSON.stringify(finalUpdatedManifest, null, 4), () => {});
writeFile(workflowFilePath, JSON.stringify(JSON.parse(finalUpdatedWorkflow), null, 4), () => {});
console.log(`[${workflowFolderPath}] Successfully patched parameter and connection names.`);
};
const verifyMultiWorkflowConnections = (combinedConnections, workflowConnections, workflowFolderPath) => {
for (const [workflowConnectionKey, workflowConnection] of Object.entries(workflowConnections)) {
const existingConnectionWithKey = combinedConnections[workflowConnectionKey];
if (existingConnectionWithKey) {
const isConnectorIdDifferent = existingConnectionWithKey.connectorId !== workflowConnection.connectorId;
const isKindDifferent = existingConnectionWithKey.kind !== workflowConnection.kind;
if (isConnectorIdDifferent || isKindDifferent) {
console.error(`[${workflowFolderPath}] *Connection validation failed. Connection with key ${workflowConnectionKey} exists with different values:
${
isConnectorIdDifferent ? `connectorId: ${existingConnectionWithKey.connectorId}, ${workflowConnection.connectorId}. ` : ''
}${
isKindDifferent ? `kind: ${existingConnectionWithKey.kind}, ${workflowConnection.kind}.` : ''}\nPlease make sure the connection keys are unique.`);
}
}
combinedConnections[workflowConnectionKey] = workflowConnection;
}
}
const verifyMultiWorkflowParameters = (combinedParameters, workflowParameters, workflowFolderPath) => {
for (const workflowParameter of workflowParameters) {
const existingParameterWithName = combinedParameters[workflowParameter.name];
if (existingParameterWithName) {
const isDisplayNameDifferent = existingParameterWithName.displayName !== workflowParameter.displayName;
const isTypeDifferent = existingParameterWithName.type !== workflowParameter.type;
const isDescriptionDifferent = existingParameterWithName.description !== workflowParameter.description;
const isRequiredDifferent = existingParameterWithName.required !== workflowParameter.required;
if (isDisplayNameDifferent || isTypeDifferent || isDescriptionDifferent || isRequiredDifferent) {
console.error(`[${workflowFolderPath}] *Parameter validation failed. Parameters with name ${workflowParameter.name} exists with different values:
${
isDisplayNameDifferent ? `displayName: ${existingParameterWithName.displayName}, ${workflowParameter.displayName}. ` : ''
}${
isTypeDifferent ? `type: ${existingParameterWithName.type}, ${workflowParameter.type}. ` : ''}${
isDescriptionDifferent ? `description: ${existingParameterWithName.description}, ${workflowParameter.description}. ` : ''}${
isRequiredDifferent ? `required: ${existingParameterWithName.required}, ${workflowParameter.required}. ` : ''}\nPlease make sure the parameter names are unique.`);
}
}
combinedParameters[workflowParameter.name] = workflowParameter;
}
}
const run = async () => {
if (foldersPath.length === 0) {
console.error('There are no template folders specified. Please provide the folders with spaces for multiple template folders, example `npm run patchTemplate folder1 folder2 folder3`');
return;
}
console.log("------------Starting patching templates------------");
// 1) Patching parameter and connection names with suffix for all workflows
for (const templateId of foldersPath) {
const workflowManifests = {};
const templateManifestFilePath = `./${templateId}/manifest.json`;
const templateManifest = getFileContentInJSON(templateManifestFilePath);
const combinedConnections = {};
const combinedParameters = {};
const templateWorkflowKeys = Object.keys(templateManifest.workflows);
const isMultiWorkflow = templateWorkflowKeys.length > 1;
for (const workflowId of templateWorkflowKeys) {
const workflowManifestFilePath = `./${templateId}/${workflowId}/manifest.json`;
const workflowManifest = getFileContentInJSON(workflowManifestFilePath);
await patchTemplateVariables(`${templateId}/${workflowId}`, workflowManifest);
workflowManifests[workflowId] = workflowManifest;
}
if (isMultiWorkflow) {
for (const workflowId of templateWorkflowKeys) {
verifyMultiWorkflowConnections(combinedConnections, workflowManifests[workflowId]?.connections, `${templateId}/${workflowId}`);
verifyMultiWorkflowParameters(combinedParameters, workflowManifests[workflowId]?.parameters, `${templateId}/${workflowId}`);
}
}
}
console.log("------------Completed patching templates with suffix------------");
};
const updateConnections = async (manifest, workflow) => {
const updatedConnections = { ...manifest.connections };
for (const name of Object.keys(manifest.connections)) {
const connection = manifest.connections[name];
if (connection.kind?.toLowerCase() === 'shared') {
connection.connectorId = sanitizeConnectorId(connection.connectorId);
}
if (name.endsWith(workflowSuffix)) {
const oldName = name?.split(workflowSuffix)?.[0];
workflow = workflow.replaceAll(`"${oldName}"`, `"${name}"`);
} else {
const newName = `${name}${workflowSuffix}`;
workflow = workflow.replaceAll(`"${name}"`, `"${newName}"`);
updatedConnections[newName] = connection;
delete updatedConnections[name];
}
}
manifest.connections = updatedConnections;
return { manifest, workflow };
};
const updateParameters = async (manifest, workflow) => {
const updatedParameters = [];
for (const parameter of manifest.parameters) {
const name = parameter.name;
if (name.endsWith(workflowSuffix)) {
const oldName = name?.split(workflowSuffix)?.[0];
workflow = workflow.replaceAll(`parameters('${oldName}')`, `parameters('${name}')`);
} else {
const newName = `${name}${workflowSuffix}`;
workflow = workflow.replaceAll(`parameters('${name}')`, `parameters('${newName}')`);
parameter.name = newName;
}
updatedParameters.push(parameter);
}
manifest.parameters = updatedParameters;
return { manifest, workflow };
};
const sanitizeConnectorId = (connectorId) => {
connectorId = (connectorId ?? '').startsWith('/') ? connectorId : `/${connectorId}`;
const fields = connectorId.split('/');
if (fields.length !== 9) {
return connectorId;
}
if (fields[1] === 'subscriptions') {
fields[2] = '#subscription#'
}
if (fields[5] === 'locations') {
fields[6] = '#location#';
}
return fields.join('/');
};
const getFileContentInJSON = (filePath) => {
return JSON.parse(readFileSync(path.resolve(filePath), {
encoding: 'utf-8'
}));
};
run();