-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrestructure_templates.js
More file actions
183 lines (165 loc) · 7.05 KB
/
restructure_templates.js
File metadata and controls
183 lines (165 loc) · 7.05 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
176
177
178
179
180
181
182
183
import { mkdir, readFileSync, rename, writeFile } from 'fs';
import path from 'path';
const manifestNamesList = JSON.parse(readFileSync(path.resolve('./manifest.json'), {
encoding: 'utf-8'
}));
const getNormWorkflowName = (workflowName) => {
return workflowName
.replace(/-([a-z])/g, (_, letter) => `_${letter.toUpperCase()}`)
.replace(/^([a-z])/, (_, letter) => letter.toUpperCase());
};
const connectorIdForBuiltInOperations = {
chunktext: "connectionProviders/dataOperationNew",
scope: "connectionProviders/control",
xslt: "connectionProviders/xmlOperations",
recurrence: "connectionProviders/schedule",
request: "connectionProviders/request",
}
const featuredOperationsToConnectors = (featuredOperations) => {
return featuredOperations?.map((featuredOperation) => {
const connectorIdForOperation = connectorIdForBuiltInOperations[featuredOperation.type.toLowerCase()];
if (!connectorIdForOperation) {
console.error('Connector Id is unknown for this operation :', featuredOperation.type);
return;
}
return {
id: connectorIdForOperation,
kind: "builtin",
};
}) ?? [];
}
const restructureSingleWorkflow = (folderName, manifestFile) => {
const nonWorkflowArtifacts = manifestFile.artifacts?.filter((artifact) => artifact.type !== 'workflow') ?? undefined;
const updatedTemplateManifest = {
id: folderName,
title: manifestFile.title,
summary: manifestFile.description,
artifacts: (nonWorkflowArtifacts?.length > 0) ? nonWorkflowArtifacts : undefined,
skus: manifestFile.skus ?? ["standard"],
workflows: {
default: {name: getNormWorkflowName(folderName)},
},
featuredConnectors: [
...featuredOperationsToConnectors(manifestFile?.featuredOperations),
...(Object.values(manifestFile?.connections)?.map((connection) => ({
id: connection.connectorId,
kind: connection.kind,
})) ?? [])
],
details: {
...(manifestFile.details ?? {}),
Type: "Workflow",
},
}
if (manifestFile?.tags) {
updatedTemplateManifest.tags = manifestFile.tags;
}
const workflowArtifact = manifestFile.artifacts?.find((artifact) => artifact.type === 'workflow');
const workflowManifest = {
id: "default",
title: manifestFile.title,
summary: manifestFile.description,
description: manifestFile.detailsDescription,
prerequisites: manifestFile.prerequisites,
kinds: (manifestFile.kinds?.length > 0) ? manifestFile.kinds : undefined,
artifacts: [workflowArtifact],
images: {
light: manifestFile.images.light,
dark: manifestFile.images.dark
},
parameters: manifestFile.parameters,
connections: manifestFile.connections
};
// Overwrite template manifest
writeFile(`./${folderName}/manifest.json`, JSON.stringify(updatedTemplateManifest, null, 4), () => {});
// Create subfolder for workflow
mkdir(`./${folderName}/default`, { recursive: true }, () => {});
// Create workflow manifest
writeFile(`./${folderName}/default/manifest.json`, JSON.stringify(workflowManifest, null, 4), () => {});
// Move images to subfolder
for (const imageFileName of Object.values(manifestFile.images)) {
rename(`./${folderName}/${imageFileName}.png`, `./${folderName}/default/${imageFileName}.png`, (err) => {
if (err) {
console.error('Error moving file:', err);
return;
}
})
}
// Move workflow to subfolder
rename(`./${folderName}/${workflowArtifact.file}`, `./${folderName}/default/${workflowArtifact.file}`, (err) => {
if (err) {
console.error('Error moving file:', err);
return;
}
});
}
const restructureMultiWorkflow = (folderName, templateManifest) => {
const updatedTemplateManifest = {
id: folderName,
title: templateManifest.title,
summary: templateManifest.description,
description: templateManifest.detailsDescription,
artifacts: (templateManifest.artifacts?.length > 0) ? templateManifest.artifacts : undefined,
skus: ["standard"],
workflows: templateManifest.workflows,
featuredConnectors: [
...featuredOperationsToConnectors(templateManifest?.featuredOperations),
...(templateManifest?.featuredConnectors ?? Object.values(templateManifest?.connections)?.map((connection) => ({
id: connection.connectorId,
kind: connection.kind,
})) ?? [])
],
details: {
...(templateManifest.details ?? {}),
Type: "Accelerator",
},
};
const allTagsCombined = templateManifest?.tags ?? [];
for (const workflowFolder of Object.keys(templateManifest.workflows)) {
const workflowManifest = JSON.parse(readFileSync(path.resolve(`./${folderName}/${workflowFolder}/manifest.json`), {
encoding: 'utf-8'
}));
const updatedWorkflowManifest = {
id: workflowFolder,
title: workflowManifest.title,
summary: workflowManifest.description,
description: workflowManifest.detailsDescription,
prerequisites: workflowManifest.prerequisites,
kinds: (workflowManifest.kinds?.length > 0) ? workflowManifest.kinds : undefined,
artifacts: workflowManifest.artifacts,
images: {
light: workflowManifest.images.light,
dark: workflowManifest.images.dark
},
parameters: workflowManifest.parameters,
connections: workflowManifest.connections
};
if (workflowManifest?.tags) {
allTagsCombined.push(...workflowManifest.tags);
}
// Overwrite workflow manifest
writeFile(`./${folderName}/${workflowFolder}/manifest.json`, JSON.stringify(updatedWorkflowManifest, null, 4), () => {});
}
const allUniqueTags = [...new Set(allTagsCombined)];
if (allUniqueTags.length > 0) {
updatedTemplateManifest.tags = allUniqueTags;
}
// Overwrite template manifest
writeFile(`./${folderName}/manifest.json`, JSON.stringify(updatedTemplateManifest, null, 4), () => {});
}
const run = async () => {
for (const folderName of manifestNamesList) {
const manifestFile = JSON.parse(readFileSync(path.resolve(`./${folderName}/manifest.json`), {
encoding: 'utf-8'
}));
const manifestFileWorkflowsCount = Object.keys(manifestFile?.workflows ?? {}).length;
// NOTE: restructureMultiWorkflow is idempotent
if (manifestFileWorkflowsCount > 1) {
restructureMultiWorkflow(folderName, manifestFile);
// NOTE: restructureSingleWorkflow is not idempotent - checking if it has been run before
} else if (manifestFileWorkflowsCount === 0) {
restructureSingleWorkflow(folderName, manifestFile);
}
}
}
run();