-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.js
144 lines (134 loc) · 5.19 KB
/
merge.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
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
/*
© 2024 CVS Health and/or one of its affiliates. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
merge.js
Merges a script and a batch and returns jobs.
*/
// ########## IMPORTS
// Module to keep secrets.
require('dotenv').config();
// Utility module.
const {alphaNumOf, dateOf, getRandomString, getNowStamp} = require('./procs/util');
// ########## CONSTANTS
// Length of the random merger ID.
const mergeIDLength = 3;
// ########## FUNCTIONS
// Merges a script and a batch and returns jobs.
exports.merge = (script, batch, executionTimeStamp) => {
// If an execution time stamp was specified:
if (executionTimeStamp) {
// If it is invalid:
if (! dateOf(executionTimeStamp)) {
// Report this and quit.
console.log('ERROR: Execution time stamp invalid');
return [];
}
}
// Otherwise, i.e. if no time stamp was specified:
else {
// Create one for the job.
executionTimeStamp = getNowStamp();
}
// Initialize a job template as a copy of the script.
const protoJob = JSON.parse(JSON.stringify(script));
// Populate empty properties of the template.
protoJob.creationTimeStamp = getNowStamp();
protoJob.executionTimeStamp = executionTimeStamp;
const {sources} = protoJob;
sources.batch = batch.id;
sources.mergeID = getRandomString(mergeIDLength);
// If isolation was requested:
if (protoJob.isolate) {
// For each act:
let {acts} = protoJob;
let lastPlaceholder = {};
for (const actIndexString in acts) {
// If it is a placeholder:
const actIndex = Number.parseInt(actIndexString);
const act = acts[actIndex];
if (act.type === 'placeholder') {
// Identify it as the current one.
lastPlaceholder = act;
}
// Otherwise, if it is a test act:
else if (act.type === 'test') {
// Change it to an array of itself and the current placeholder.
acts[actIndex] = JSON.parse(JSON.stringify([act, lastPlaceholder]));
}
};
// Flatten the acts, causing insertion of placeholder copies before acts needing them.
protoJob.acts = acts.flat();
}
// Initialize an array of jobs.
const jobs = [];
const {targets} = batch;
const targetIDs = Object.keys(targets);
const targetCount = targetIDs.length;
const targetSuffixWidth = Math.ceil(Math.pow(targetCount, 1 / 36));
// For each target in the batch:
targetIDs.forEach((what, index) => {
const {actGroups, url} = targets[what];
// If the target has the required identifiers:
if (actGroups && url) {
// Initialize a job as a copy of the template.
const job = JSON.parse(JSON.stringify(protoJob));
const {sources, target} = job;
// Make the job ID unique.
const targetSuffix = alphaNumOf(index).padStart(targetSuffixWidth, '0');
job.id = `${executionTimeStamp}-${sources.mergeID}-${targetSuffix}`;
// Populate the target-specific properties of the job.
target.what = what;
target.url = url;
// Replace each placeholder in the job with a copy of the named act group of the target.
let {acts} = job;
for (const actIndex in acts) {
const act = acts[actIndex];
if (act.type === 'placeholder') {
const replacerName = act.which;
const replacerActs = JSON.parse(JSON.stringify(actGroups[replacerName]));
if (replacerName && actGroups && replacerActs) {
// Add any override properties to any launch act in the replacer.
for (const replacerAct of replacerActs) {
if (replacerAct.type === 'launch') {
if (act.browserID) {
replacerAct.browserID = act.browserID;
}
if (act.target) {
replacerAct.target = act.target;
}
}
};
acts[actIndex] = replacerActs;
}
else {
console.log(`ERROR: Placeholder for target ${what} not replaceable`);
}
}
};
// Flatten the acts.
job.acts = acts.flat();
// Append the job to the array of jobs.
jobs.push(job);
}
else {
console.log(`ERROR: Target ${what} in batch missing actGroups or url property`);
}
});
return jobs;
};