-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixids.js
105 lines (105 loc) · 3.65 KB
/
fixids.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixIds = void 0;
var fs = require("fs");
var _ = require("underscore");
var IdModel = (function () {
function IdModel() {
}
return IdModel;
}());
var FixIds = exports.FixIds = (function () {
function FixIds(bundle) {
if (typeof bundle === 'string') {
this.filePath = bundle;
var fileContent = fs.readFileSync(this.filePath).toString();
this.content = JSON.parse(fileContent);
}
else if (typeof bundle === 'object') {
this.content = bundle;
}
}
FixIds.args = function (args) {
return args
.positional('output', {
description: ''
})
.option('path', {
array: true,
demandOption: true
});
};
FixIds.handler = function (args) {
var fixids = new FixIds(args.file_path);
fixids.fix();
fixids.save();
};
FixIds.prototype.updateReferences = function (obj) {
if (obj instanceof Array) {
var _loop_1 = function (i) {
if (obj[i] instanceof Array || typeof obj[i] === 'object') {
this_1.updateReferences(obj[i]);
}
else if (typeof obj[i] === 'string') {
_.each(this_1.ids, function (id) {
if (obj[i].endsWith(id.resourceType + '/' + id.oldId)) {
obj[i] = id.resourceType + '/' + id.newId;
}
});
}
};
var this_1 = this;
for (var i = 0; i < obj.length; i++) {
_loop_1(i);
}
}
else if (typeof obj === 'object') {
var matchingId = _.find(this.ids, function (id) { return id.resourceType === obj.resourceType && id.oldId === obj.id; });
if (matchingId) {
obj.id = matchingId.newId;
}
var _loop_2 = function (i) {
if (obj[i] instanceof Array || typeof obj[i] === 'object') {
this_2.updateReferences(obj[i]);
}
else if (typeof obj[i] === 'string') {
_.each(this_2.ids, function (id) {
if (obj[i].endsWith(id.resourceType + '/' + id.oldId)) {
obj[i] = id.resourceType + '/' + id.newId;
}
});
}
};
var this_2 = this;
for (var _i = 0, _a = Object.keys(obj); _i < _a.length; _i++) {
var i = _a[_i];
_loop_2(i);
}
}
};
FixIds.prototype.fix = function () {
(this.content.entry || [])
.filter(function (entry) {
entry.resource && entry.resource.id && entry.resource.id.match(/^\d+$/);
})
.map(function (entry) {
return {
resourceType: entry.resource.resourceType,
oldId: entry.resource.id,
newId: 't' + entry.resource.id
};
});
this.updateReferences(this.content);
};
FixIds.prototype.save = function () {
if (!this.filePath) {
throw new Error('No file path specified to save to');
}
var fileContent = JSON.stringify(this.content, null);
fs.writeFileSync(this.filePath, fileContent);
};
FixIds.command = 'fixids <file_path>';
FixIds.description = 'Fix number-only ids of resources in a bundle so they can be imported with HAPI';
return FixIds;
}());
//# sourceMappingURL=fixids.js.map