forked from FlowFuse/node-red-dashboard-2-migration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 2.26 KB
/
index.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
const generators = require('./generators')
const utils = require('./utils')
const transformerMap = require('./transformers/map.json')
const transformers = require('./transformers')
const MigrateDashboard = {
migrate: function (flow) {
const idMap = {} // map old IDs to new IDs so that we can update references to them
const migratedFlow = []
// generate nodes we know D1.0 doesn't provide
const base = generators.generateUiBase()
migratedFlow.push(base)
const theme = generators.generateUiTheme()
migratedFlow.push(theme)
// loop over all nodes in the flow.json
flow.forEach(node => {
const oldId = node.id
const newId = utils.generateId()
node.id = newId
idMap[oldId] = newId
if (
Object.prototype.hasOwnProperty.call(transformerMap, node.type) &&
Object.prototype.hasOwnProperty.call(transformers, transformerMap[node.type]) &&
typeof transformers[transformerMap[node.type]] === 'function'
) {
const migratedNode = transformers[transformerMap[node.type]](node, base.id, theme.id)
migratedFlow.push(migratedNode)
return
}
if (node.type.startsWith('ui_')) {
// Unsupported UI node types, disable them
console.log('Unable to automatically migrate ' + node.type + ' nodes currently. Disabling the node.')
node.d = true
}
// We don't have any particular migration logic for this node type
// Not a Dashboard 1.0 node, we can just return it as is
migratedFlow.push(node)
})
// work smart, not hard
let strJson = JSON.stringify(migratedFlow)
// loop over idMap
for (const key in idMap) {
// replace all instances of the old ID with the new ID
strJson = strJson.replaceAll(key, idMap[key])
}
return JSON.parse(strJson)
}
}
if (typeof module === 'object' && module.exports) {
module.exports = MigrateDashboard
} else if (typeof window === 'object') {
window.MigrateDashboard = MigrateDashboard
} else {
global.MigrateDashboard = MigrateDashboard
}