-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPatch.js
More file actions
executable file
·180 lines (160 loc) · 6.38 KB
/
Patch.js
File metadata and controls
executable file
·180 lines (160 loc) · 6.38 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
#!/usr/bin/env node
/*jshint esversion: 8 */
/*jshint -W069 */
const BaseScript = require('./lib/BaseScript.js');
const objectPath = require("object-path");
const chalk = require('chalk');
const Logger = require('basic-logger');
const log = new Logger({
showMillis: false,
showTimestamp: true
});
module.exports =
class Patch extends BaseScript {
constructor() {
super(defaultOptions, defaultHelpMessage);
}
/** exec()
* entry point
*/
async exec() {
await this.connect();
log.info(chalk.yellow("Connected to project: \"" + this.project.title + "\" and branch: " + this.branch.title || this.branch._doc));
this.option_queryFilePath = this.options["query-file-path"] || null;
this.option_reportOnly = this.options["report-only"] || false;
this.option_overwrite = this.options["overwrite"] || false;
this.option_propertyPath = this.options["property-path"];
this.option_newPropertyPath = this.options["new-property-path"];
this.option_propertyValue = this.options["property-value"];
this.option_opMove = this.options["move"] || false;
this.option_opReplace = this.options["replace"] || false;
this.option_opCopy = this.options["copy"] || false;
this.option_opRemove = this.options["remove"] || false;
this.option_opAdd = this.options["add"] || false;
let query = require(this.option_queryFilePath);
if ((this.option_opMove || this.option_opReplace || this.option_opCopy || this.option_opRemove) && !query[this.option_propertyPath]) {
// if doing a move, copy, replace or remove then only need to query for nodes that have the property now
if (!this.option_propertyPath) {
log.error("Operation: " + op + " requires --property-path and --new-property-path");
printHelp(getOptions());
return;
}
query[this.option_propertyPath.split('/').join('.')] = {
'$exists': true
};
}
query._fields = {};
query._fields[this.option_propertyPath.split('/').join('.')] = 1;
let result = await this.session.queryNodes(this.repository, this.branch, query, {metadata: true, full: true, limit: -1});
if (this.option_reportOnly) {
result.rows.forEach(element => {
log.info(`${element._doc} ${this.option_propertyPath} : ${objectPath.get(element, this.option_propertyPath)}`);
});
}
else if (this.option_opMove || this.option_opReplace || this.option_opCopy || this.option_opRemove || this.option_opAdd)
{
this.handlePatch(result.rows, this.option_opMove ? "move" : this.option_opReplace ? "replace" : this.option_opCopy ? "copy" : this.option_opRemove ? "remove" : this.option_opAdd ? "add" : null);
}
else
{
printHelp(getOptions());
}
}
async handlePatch(nodes, op) {
log.debug("handlePatch()");
log.info("Operation: " + op);
if (!op) {
throw `Patch operation (op): ${op} not valid`;
}
let patches = nodes.map(node => {
let patch = {
node: node._doc,
patch: {
op: op,
path: '/' + this.option_propertyPath.split('.').join('/').split('/').join('/'), // ensure property path is formatted
}
};
switch (op) {
case 'add':
case 'replace':
patch.patch.value = option_propertyValue;
break;
case 'copy':
case 'move':
patch.patch.from = '/' + this.option_propertyPath.split('.').join('/').split('/').join('/'); // ensure property path is formatted
patch.patch.path = '/' + this.option_newPropertyPath.split('.').join('/').split('/').join('/'); // ensure property path is formatted
break;
}
return patch;
});
log.debug("Patches: " + JSON.stringify(patches, null, 2));
patches.forEach(patch => {
this.session.patchNode(this.repository, this.branch, patch.node, [patch.patch]);
});
}
};
const defaultOptions = [
{
name: 'report-only',
type: Boolean,
default: false,
description: 'read nodes listed in the query resullts and report the current value of the property. No node updates are made.'
},
{
name: 'query-file-path',
alias: 'y',
type: String,
required: true,
description: 'path to a json file defining the query'
},
{
name: 'move',
type: Boolean,
default: false,
description: 'perform a patch "move" operation. This will rename the property in --property-path to --new-property-path'
},
{
name: 'property-path',
type: String,
description: 'name of the JSON property to update when patching.'
},
{
name: 'new-property-path',
type: String,
description: 'name of the JSON property to update when patching.'
},
{
name: 'property-value',
type: String,
description: 'the new value to set for the patched property.'
},
{
name: 'overwrite',
alias: 'o',
type: Boolean,
default: false,
description: 'overwrite properties that already exist. by default only missing properties will be set'
}
];
const defaultHelpMessage = [
{
header: 'Cloud CMS Patch Nodes',
content: 'Update nodes in a branch by applying an HTTP PATCH method API call. The nodes to update are identified by a provided query.'
},
{
header: 'Examples',
content: [{
desc: '\n1. Report current property values for nodes found in query results:',
},
{
desc: 'npx cloudcms-util patch --report-only --property-path "/body" --query-file-path ./patch-test1.json'
},
{
desc: '\n2. Apply updates for nodes found in query results:',
},
{
desc: 'npx cloudcms-util patch --move --branch "master" --property-path "/body" --new-property-path "/bodyContent" --query-file-path ./patch-test1.json'
}
]
}
];