-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathApplyUpdates.js
More file actions
84 lines (69 loc) · 2.69 KB
/
ApplyUpdates.js
File metadata and controls
84 lines (69 loc) · 2.69 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
/*globals define*/
/*eslint-env node, browser*/
define([
'deepforge/updates/Updates',
'text!./metadata.json',
'plugin/ImportLibrary/ImportLibrary/ImportLibrary',
], function (
Updates,
pluginMetadata,
PluginBase,
) {
'use strict';
pluginMetadata = JSON.parse(pluginMetadata);
const ApplyUpdates = function () {
PluginBase.call(this);
this.pluginMetadata = pluginMetadata;
};
ApplyUpdates.metadata = pluginMetadata;
// Prototypical inheritance from PluginBase.
ApplyUpdates.prototype = Object.create(PluginBase.prototype);
ApplyUpdates.prototype.constructor = ApplyUpdates;
ApplyUpdates.prototype.main = async function (callback) {
// Retrieve the updates to apply
const config = this.getCurrentConfig();
if (!config.updates.length) {
this.result.setSuccess(true);
return callback(null, this.result);
}
const [libUpdates, migrations] = partition(
config.updates,
update => update.type === Updates.SEED
);
await this.applyLibraryUpdates(libUpdates);
await this.applyMigrations(migrations);
const updateDisplayNames = config.updates
.map(update => update.type === Updates.SEED ? `${update.name} (library)` : update.name)
.join(', ');
await this.save(`Applied project updates: ${updateDisplayNames}`);
this.result.setSuccess(true);
callback(null, this.result);
};
ApplyUpdates.prototype.applyLibraryUpdates = async function (updates) {
for (let i = 0; i < updates.length; i++) {
const {name} = updates[i];
const {branchInfo, rootHash, libraryData} = await this.createGMELibraryFromSeed(name);
await this.core.updateLibrary(this.rootNode, name, rootHash, libraryData);
await this.removeTemporaryBranch(branchInfo);
}
};
ApplyUpdates.prototype.applyMigrations = async function (migrations) {
const updateNames = migrations.map(migration => migration.name);
const updates = Updates.getUpdates(updateNames);
for (let i = 0, len = updates.length; i < len; i++) {
const update = updates[i];
this.logger.info(`Applying update: ${update.name} to ${this.projectId}`);
await update.apply(this.core, this.rootNode, this.META);
}
};
function partition(data, fn) {
const partitioned = [[], []];
data.forEach(datum => {
const partitionIndex = fn(datum) ? 0 : 1;
const partition = partitioned[partitionIndex];
partition.push(datum);
});
return partitioned;
}
return ApplyUpdates;
});