-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudcms-export.js
More file actions
executable file
·938 lines (811 loc) · 29.8 KB
/
cloudcms-export.js
File metadata and controls
executable file
·938 lines (811 loc) · 29.8 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
#!/usr/bin/env node
/*jshint -W069 */
/*jshint -W104*/
const Gitana = require("gitana");
const assert = require('assert').strict;
const fs = require("fs");
const path = require("path").posix;
const async = require("async");
const cliArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage')
const util = require("./lib/util");
const writeJsonFile = require('write-json-file');
const _ = require('underscore');
const Logger = require('basic-logger');
const { map } = require("underscore");
const log = new Logger({
showMillis: false,
showTimestamp: true
});
const SC_SEPARATOR = "__";
// debug only when using charles proxy ssl proxy when intercepting cloudcms api calls:
if (process.env.NODE_ENV === "dev" || process.env.NODE_ENV === "development") {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
}
var options = handleOptions();
if (!options) {
return;
}
if (options["verbose"]) {
Logger.setLevel('debug', true);
} else {
Logger.setLevel('info', true);
}
var option_prompt = options["prompt"];
var option_useCredentialsFile = options["use-credentials-file"];
var option_gitanaFilePath = options["gitana-file-path"] || "./gitana.json";
var option_branchId = options["branch"] || "master";
var option_listTypes = options["list-types"];
var option_definitionQNames = options["definition-qname"]; // array
var option_allDefinitions = options["all-definitions"] || false;
var option_includeInstances = options["include-instances"] || false;
var option_includeRelated = options["include-related"] || false;
var option_includeTranslations = options["include-translations"] || false;
// var option_includeTags = options["include-tags"] || false;
var option_dataFolderPath = options["folder-path"] || "./data";
var option_queryFilePath = options["query-file-path"];
var option_traverse = options["traverse"];
// var option_traverseFilePath = options["traverse-query-file-path"];
log.debug("option_queryFilePath " + option_queryFilePath);
//
// load gitana.json config and override credentials
//
var gitanaConfig = JSON.parse("" + fs.readFileSync(option_gitanaFilePath));
if (option_useCredentialsFile) {
// override gitana.json credentials with username and password properties defined in the cloudcms-cli tool local db
var rootCredentials = JSON.parse("" + fs.readFileSync(path.join(util.homeDirectory(), ".cloudcms", "credentials.json")));
gitanaConfig.username = rootCredentials.username;
gitanaConfig.password = rootCredentials.password;
} else if (option_prompt) {
// override gitana.json credentials with username and password properties entered at command prompt
var option_prompt = require('prompt-sync')({
sigint: true
});
gitanaConfig.username = option_prompt('name: ');
gitanaConfig.password = option_prompt.hide('password: ');
} // else don't override credentials
util.parseGitana(gitanaConfig);
// if listing types
if (option_listTypes) {
// print a list of definition qnames on the project/branch
handleListTypes();
} else if (option_queryFilePath) {
// download and store data from project/branch to a local folder
handleQueryBasedExport();
} else if (option_allDefinitions || (option_definitionQNames && Gitana.isArray(option_definitionQNames))) {
// download and store data from project/branch to a local folder
handleExport();
} else {
printHelp(getOptions());
}
return;
//
// functions
//
function handleQueryBasedExport() {
log.debug("handleQueryBasedExport()");
util.getBranch(gitanaConfig, option_branchId, function (err, branch, platform, stack, domain, primaryDomain, project) {
if (err) {
log.debug("Error connecting to Cloud CMS branch: " + err);
return;
}
log.info("connected to project: \"" + project.title + "\" and branch: " + branch.title || branch._doc);
var context = {
branchId: option_branchId,
branch: branch,
gitanaConfig: gitanaConfig,
platform: platform,
queryFilePath: option_queryFilePath,
dataFolderPath: option_dataFolderPath,
includeRelated: option_includeRelated,
includeTranslations: option_includeTranslations,
query: require(path.resolve(path.normalize(option_queryFilePath))),
nodes: [],
relatedIds: [],
relatedNodes: [],
translationNodes: [],
queryPageSize: 100
};
async.waterfall([
async.apply(getNodesFromQuery, context),
async.ensureAsync(async.apply(getNodesFromTraverseQuery)),
async.ensureAsync(async.apply(getRelated, context.nodes)),
// async.ensureAsync(async.apply(getTranslations, context.relatedNodes)),
async.ensureAsync(async.apply(getTranslations, context.nodes)),
async.ensureAsync(async.apply(downloadAttachments, context.nodes, "nodes")),
async.ensureAsync(async.apply(downloadAttachments, context.relatedNodes, "related")),
async.ensureAsync(async.apply(downloadAttachments, context.translationNodes, "nodes")),
async.ensureAsync(async.apply(writeContentInstanceJSONtoDisk, context.nodes, "nodes")),
], function (err, context) {
if (err) {
log.error("Error exporting: " + err);
return;
}
log.info("Export complete");
return;
});
});
}
function handleExport() {
log.debug("handleExport()");
util.getBranch(gitanaConfig, option_branchId, function (err, branch, platform, stack, domain, primaryDomain, project) {
if (err) {
log.debug("Error connecting to Cloud CMS branch: " + err);
return;
}
log.info("connected to project: \"" + project.title + "\" and branch: \"" + (branch.title || branch._doc) + "\"");
var context = {
branchId: option_branchId,
branch: branch,
allDefinitions: option_allDefinitions,
importTypeQNames: option_definitionQNames,
queryFilePath: null,
gitanaConfig: gitanaConfig,
platform: platform,
typeDefinitions: [],
dataFolderPath: option_dataFolderPath,
includeInstances: option_includeInstances,
includeRelated: option_includeRelated,
includeTranslations: option_includeTranslations,
relatedIds: [],
relatedNodes: [],
translationNodes: [],
instanceNodes: {}
};
async.waterfall([
async.apply(getDefinitions, context),
async.ensureAsync(getDefinitionFormAssociations),
async.ensureAsync(getDefinitionForms),
async.ensureAsync(writeDefinitionJSONtoDisk),
async.ensureAsync(getContentInstances),
async.ensureAsync(async.apply(getRelated, context.instanceNodes)),
async.ensureAsync(async.apply(getTranslations, context.instanceNodes)),
async.ensureAsync(async.apply(downloadAttachments, context.instanceNodes, "instances")),
async.ensureAsync(async.apply(downloadAttachments, context.relatedNodes, "related")),
async.ensureAsync(async.apply(downloadAttachments, context.translationNodes, "translations")),
async.ensureAsync(async.apply(writeContentInstanceJSONtoDisk, context.instanceNodes, "instances"))
], function (err, context) {
if (err) {
log.error("Error exporting: " + err);
return;
}
// log.debug(JSON.stringify(context.typeDefinitions, null, 2));
log.info("Export complete");
return;
});
});
}
function getNodesFromQuery(context, callback) {
log.debug("getNodesFromQuery()");
var query = context.query;
var nodes = context.nodes;
var queryPageSize = context.queryPageSize || 100;
context.branch.queryNodes(query, {
limit: queryPageSize,
paths: true
}).each(function () {
var node = this;
util.enhanceNode(node);
nodes.push(node);
}).then(function () {
callback(null, context);
});
}
function getNodesFromTraverseQuery(context, callback) {
log.debug("getNodesFromTraverseQuery()");
if (!option_traverse) {
// no traverse needed
return callback(null, context);
}
// there should already be nodes from the previous query.
// use this list of nodes to then run a traverse on each
var nodes = context.nodes;
if (!context.associations) {
context.associations = [];
}
async.eachOfLimit(nodes, 5, function(node, key, cb) {
// run the traverse
node.traverse({
filter: "ALL_BUT_START_NODE",
associations: {
"a:linked": "ANY",
"mmcx:bloghasauthor": "ANY",
"mmcx:bloghascategory": "ANY",
"mmcx:hastag": "ANY",
"mmcx:has-image": "ANY",
},
depth: 2,
types: [
"n:node"
]
}).then(function () {
var result = this;
context.associations.push(Object.values(result._associations));
Object.values(result._nodes).forEach( node => {
context.nodes.push(util.enhanceNode(node));
});
cb();
});
}, function(err) {
if (err) {
console.log("traverse error: " + err);
}
callback(null, context);
});
}
function downloadAttachments(nodes, pathPart, context, callback) {
log.debug("downloadAttachments() " + pathPart);
if (!_.keys(nodes).length) {
callback(null, context);
return;
}
if (!Gitana.isArray(nodes)) {
// flatten to an array if list is an associative array of sub lists (by type)
var newList = [];
var types = _.keys(nodes);
for (var i = 0; i < types.length; i++) {
for (var j = 0; j < nodes[types[i]].length; j++) {
newList.push(nodes[types[i]][j]);
}
}
nodes = newList;
}
async.eachSeries(nodes, async.apply(downloadNodeAttachments, context, pathPart),
function (err) {
if (err) {
log.error("Error reading attachments: " + err);
callback(err);
return;
}
log.debug("done downloading node attachments");
callback(null, context);
return;
});
}
function downloadNodeAttachments(context, pathPart, node, callback) {
log.debug("downloadNodeAttachments()");
async.eachSeries(_.filter(_.keys(node.attachments), function (k) {
return !k.match(/^_preview_/)
}), async.apply(downloadAttachment, context, node, pathPart),
function (err) {
if (err) {
log.error("Error reading attachments: " + err);
callback(err);
return;
}
log.debug("downloaded node attachments");
callback(null, context);
return;
});
}
function downloadAttachment(context, node, pathPart, attachmentId, callback) {
log.debug("downloadAttachment()");
var attachmentPath = path.normalize(path.resolve(context.dataFolderPath, pathPart, node._type.replace(':', SC_SEPARATOR), node._doc, "attachments"));
if (node.__translationOf) {
// translation nodes go to a sub folder of its master node's location
attachmentPath = path.normalize(path.resolve(context.dataFolderPath, pathPart, node.__translationOf._type.replace(':', SC_SEPARATOR), node.__translationOf._doc, "translations", node.__features()["f:locale"].locale, "attachments"));
}
fs.mkdirSync(path.normalize(attachmentPath), {
recursive: true
});
var filename = attachmentId;
if (node.title) {
var filenameParts = node.title.match(/\..+$/);
if (filenameParts && filenameParts.length >= 1) {
filename += filenameParts[0];
} else {
filename += ".txt"; // assume .txt file extension
}
}
var filePath = attachmentPath + path.sep + filename;
log.debug("file path: " + filePath);
util.downloadNode(context.platform, filePath, context.branch.getRepositoryId(), context.branchId, node._doc, attachmentId, function (err) {
callback();
});
}
function writeContentInstanceJSONtoDisk(nodes, pathPart, context, callback) {
log.debug("writeContentInstanceJSONtoDisk()");
var dataFolderPath = path.normalize(context.dataFolderPath);
if (!Gitana.isArray(nodes)) {
// flatten to an array if list is an associative array of sub lists (by type)
var newList = [];
var types = _.keys(nodes);
for (var i = 0; i < types.length; i++) {
for (var j = 0; j < nodes[types[i]].length; j++) {
newList.push(nodes[types[i]][j]);
}
}
nodes = newList;
}
for (var i = 0; i < nodes.length; i++) {
var node = cleanNode(nodes[i], "");
assert.ok(node._doc);
var filePath = path.normalize(path.resolve(context.dataFolderPath, pathPart, node._type.replace(':', SC_SEPARATOR), node._doc, "node.json"));
writeJsonFile.sync(filePath, node);
}
// write related nodes
var relatedNodes = context.relatedNodes;
_.map(relatedNodes, function (v, k) {
var node = cleanNode(v, "");
writeJsonFile.sync(buildRelatedPath(dataFolderPath, node), node);
});
// write translation nodes
var translationNodes = context.translationNodes;
translationNodes.forEach(translationNode => {
var translationNodeFilePath = path.normalize(path.resolve(context.dataFolderPath, pathPart, translationNode.__translationOf._type.replace(':', SC_SEPARATOR), translationNode.__translationOf._doc, "translations", translationNode.__features()["f:locale"].locale, "node.json"));
var nodeJSON = cleanNode(translationNode, "");
writeJsonFile.sync(translationNodeFilePath, nodeJSON);
});
callback(null, context);
}
function buildInstancePath(dataFolderPath, node) {
return path.normalize(path.resolve(dataFolderPath, "instances", node._type.replace(':', SC_SEPARATOR), node._source_doc, "node.json"));
}
function buildRelatedPath(dataFolderPath, node) {
return path.normalize(path.resolve(dataFolderPath, "related", node._type.replace(':', SC_SEPARATOR), node._source_doc, "node.json"));
}
function getRelated(list, context, callback) {
log.debug("getRelated()");
if (!context.includeRelated) {
callback(null, context);
return;
}
var instanceNodes = list;
var instances = JSON.parse(JSON.stringify(instanceNodes));
var related = util.findKeyValues(instances, "ref", []);
var relatedIds = {};
for (var i = 0; i < related.length; i++) {
var id = related[i].split('/')[5];
relatedIds[id] = id;
}
relatedIds = Object.keys(relatedIds);
log.debug("relatedIds: \n" + JSON.stringify(relatedIds, null, 2));
context.relatedIds = relatedIds || [];
if (!context.relatedIds.length) {
callback(null, context);
return;
}
var query = {
_doc: {
$in: relatedIds
}
};
context.branch.queryNodes(query, {
limit: -1,
paths: true
}).each(function () {
var node = this;
util.enhanceNode(node);
context.relatedNodes.push(node);
}).then(function () {
// log.debug("related nodes: " + JSON.stringify(context.relatedNodes, null, 2));
callback(null, context);
});
}
function getTranslations(nodes, context, callback) {
log.debug("getTranslations()");
if (!context.includeTranslations) {
callback(null, context);
return;
}
if (!nodes.length) {
callback(null, context);
return;
}
// if (!!!context.queryFilePath && !context.includeInstances) {
// // have to include instances to get translations
// callback(null, context);
// return;
// }
var masterNodes = Object.values(nodes).filter(node => {
let features = node.__features();
return !!features["f:multilingual"];
});
async.eachSeries(masterNodes, function(masterNode, callback) {
masterNode.listTranslations().each(function() {
var node = this;
util.enhanceNode(node);
node.__translationOf = masterNode;
context.translationNodes.push(node);
}).then(function(){
callback();
});
},
function (err) {
if (err) {
log.error("Error reading translation nodes: " + err);
callback(err);
return;
}
log.debug("done downloading node translations");
callback(null, context);
return;
});
}
function getContentInstances(context, callback) {
log.debug("getContentInstances()");
if (!context.includeInstances) {
callback(null, context);
return;
}
var typeDefinitions = context.typeDefinitions;
if (!typeDefinitions || !Gitana.isArray(typeDefinitions) || !typeDefinitions.length) {
callback("No type defintions");
return;
}
async.eachSeries(typeDefinitions, async.apply(getDefinitionInstances, context),
function (err) {
if (err) {
log.error("Error reading content instances: " + err);
callback(err);
return;
}
log.debug("loaded definition instances");
callback(null, context);
return;
});
}
function getDefinitionInstances(context, typeDefinitionNode, callback) {
log.debug("getDefinitionInstances()");
var query = {
_type: typeDefinitionNode._qname
}
if (!context.instanceNodes[typeDefinitionNode._qname]) {
context.instanceNodes[typeDefinitionNode._qname] = [];
}
context.branch.queryNodes(query, {
limit: -1,
paths: true
}).each(function () {
var instance = this;
util.enhanceNode(instance);
context.instanceNodes[typeDefinitionNode._qname].push(instance);
}).then(function () {
// log.debug("instances: " + JSON.stringify(context.instanceNodes, null, 2));
log.debug("instances count: " + context.instanceNodes[typeDefinitionNode._qname].length);
callback(null, context);
});
}
function handleListTypes() {
log.debug("handleListTypes()");
util.getBranch(gitanaConfig, option_branchId, function (err, branch, platform, stack, domain, primaryDomain) {
if (err) {
log.debug("Error connecting to Cloud CMS branch: " + err);
return;
}
log.info("connected to branch: " + branch.title || branch._doc);
var context = {
branchId: option_branchId,
branch: branch,
typeDefinitions: []
};
getDefinitions(context, function (err, context) {
if (err) {
log.error("Error listing definition nodes " + err);
return;
}
Object.keys(context.typeDefinitions).forEach(function (type) {
log.debug(JSON.stringify(context.typeDefinitions[type]));
console.log("type: " + context.typeDefinitions[type]._type + "\t_qname: " + context.typeDefinitions[type]._qname + "\tTitle: \"" + context.typeDefinitions[type].title + "\"");
});
// console.log("\ndone");
});
});
}
function writeDefinitionJSONtoDisk(context, callback) {
var dataFolderPath = context.dataFolderPath;
var includeInstances = context.includeInstances;
var typeDefinitions = context.typeDefinitions;
dataFolderPath = path.normalize(dataFolderPath);
fs.mkdirSync(dataFolderPath, {
recursive: true
});
if (fs.existsSync(dataFolderPath)) {
Object.keys(typeDefinitions).forEach(function (typeId) {
log.debug(JSON.stringify(typeDefinitions[typeId]));
writeFormJsontoDisk(dataFolderPath, typeDefinitions[typeId]);
var node = cleanNode(typeDefinitions[typeId]);
writeJsonFile.sync(buildDefinitionPath(dataFolderPath, node), node);
});
console.log('done');
callback(null, context);
return;
} else {
callback("data folder path not found or could not be created: " + dataFolderPath);
return;
}
}
function writeFormJsontoDisk(dataFolderPath, node) {
if (!node.__forms) {
return;
}
for (var i = 0; i < node.__formAssociations.length; i++) {
var formAssociation = node.__formAssociations[i];
var forms = node.__forms;
var formKey = formAssociation["form-key"];
for (var j = 0; j < forms.length; j++) {
var form = node.__forms[j];
if (form._doc == formAssociation.target) {
var formNode = cleanNode(form, formKey);
writeJsonFile.sync(buildDefinitionFormPath(dataFolderPath, node, formKey), formNode);
}
}
}
}
function buildDefinitionPath(dataFolderPath, node) {
return path.normalize(path.resolve(dataFolderPath, "definitions", node._qname.replace(':', SC_SEPARATOR), "node.json"));
}
function buildDefinitionFormPath(dataFolderPath, node, formKey) {
return path.normalize(path.resolve(dataFolderPath, "definitions", node._qname.replace(':', SC_SEPARATOR), "forms", formKey.replace(':', SC_SEPARATOR) + ".json"));
}
function cleanNode(node, qnameMod) {
var n = node;
util.enhanceNode(n);
n = JSON.parse(JSON.stringify(n));
n._source_doc = n._doc;
n._qname += qnameMod || "";
// delete n._doc;
delete n._system;
delete n.attachments;
delete n._attachments;
delete n.__forms;
delete n.__formAssociations;
delete n.__translationOf;
return n;
}
function logContext(context, callback) {
log.debug("logContext() " + JSON.stringify(context.branch, null, 2));
callback(null, context);
}
function getDefinitions(context, callback) {
log.debug("getDefinitions()");
var qnames = context.importTypeQNames;
if (!context.typeDefinitions) {
context.typeDefinitions = [];
}
var query = {
_type: {
"$in": ["d:type", "d:association", "d:feature"]
},
systemBootstrapped: {
$exists: false
}
}
if (qnames && Gitana.isArray(qnames)) {
query._qname = {
"$in": qnames
};
}
context.branch.queryNodes(query, {
limit: -1,
sort: {
_type: 1,
title: 1
}
}).each(function () {
var definition = this;
util.enhanceNode(definition);
context.typeDefinitions.push(definition);
}).then(function () {
log.debug("definitions: " + JSON.stringify(context.typeDefinitions, null, 2));
callback(null, context);
});
}
function readDefinition(context, callback) {
log.debug("readDefinition()");
context.branch.readDefinition().each(function () {
var definition = this;
util.enhanceNode(definition);
context.typeDefinitions.push(definition);
}).then(function () {
log.debug("definitions: " + JSON.stringify(context.typeDefinitions, null, 2));
callback(null, context);
});
}
function getDefinitionForms(context, callback) {
log.debug("getDefinitionForms()");
var typeDefinitions = context.typeDefinitions;
if (!typeDefinitions || !Gitana.isArray(typeDefinitions) || !typeDefinitions.length) {
callback("No type defintions");
return;
}
async.eachSeries(typeDefinitions, getDefinitionFormList, function (err) {
if (err) {
log.error("Error reading definition forms: " + err);
callback(err);
return;
}
log.debug("loaded definition forms");
callback(null, context);
return;
});
}
function getDefinitionFormAssociations(context, callback) {
log.debug("getDefinitionFormAssociations()");
var typeDefinitions = context.typeDefinitions;
if (!typeDefinitions || !Gitana.isArray(typeDefinitions) || !typeDefinitions.length) {
callback("No type defintions");
return;
}
async.eachSeries(typeDefinitions, getDefinitionFormAssociationList, function (err) {
if (err) {
log.error("Error reading definition form associations: " + err);
callback(err);
return;
}
log.debug("loaded definition form associations");
callback(null, context);
return;
});
}
function getDefinitionFormList(typeDefinitionNode, callback) {
log.debug("getDefinitionFormList()");
typeDefinitionNode.listRelatives({
"type": "a:has_form",
"direction": "OUTGOING"
}).then(function () {
log.debug("relatives: " + JSON.stringify(this, null, 2));
typeDefinitionNode.__forms = this.asArray();
callback();
});
}
function getDefinitionFormAssociationList(typeDefinitionNode, callback) {
log.debug("getDefinitionFormList()");
typeDefinitionNode.associations({
"type": "a:has_form",
"direction": "OUTGOING"
}).then(function () {
log.debug("associations: " + JSON.stringify(this, null, 2));
typeDefinitionNode.__formAssociations = this.asArray();
callback();
});
}
function getOptions() {
return [{
name: 'help',
alias: 'h',
type: Boolean
},
{
name: 'verbose',
alias: 'v',
type: Boolean,
description: 'verbose logging'
},
{
name: 'prompt',
alias: 'p',
type: Boolean,
description: 'prompt for username and password. overrides gitana.json credentials'
},
{
name: 'use-credentials-file',
alias: 'c',
type: Boolean,
description: 'use credentials file ~/.cloudcms/credentials.json. overrides gitana.json credentials'
},
{
name: 'gitana-file-path',
alias: 'g',
type: String,
description: 'path to gitana.json file to use when connecting. defaults to ./gitana.json'
},
{
name: 'branch',
alias: 'b',
type: String,
description: 'branch id (not branch name!) to write content to. branch id or "master". Default is "master"'
},
{
name: 'list-types',
alias: 'l',
type: Boolean,
description: 'list type definitions available in the branch'
},
{
name: 'definition-qname',
alias: 'q',
type: String,
multiple: true,
description: '_qname of the type definition. Or use --all-definitions'
},
{
name: 'all-definitions',
alias: 'a',
type: Boolean,
description: 'export all definitions. Or use --definition-qname'
},
{
name: 'include-instances',
alias: 'i',
type: Boolean,
description: 'include instance records for conent type definitions'
},
{
name: 'include-related',
alias: 'r',
type: Boolean,
description: 'include instance records referred to in relators on instance records'
},
{
name: 'include-translations',
alias: 's',
type: Boolean,
description: 'include translation records of selected nodes'
},
// {
// name: 'include-tags',
// type: Boolean,
// description: 'include tags for instances and nodes'
// },
{
name: 'folder-path',
alias: 'f',
type: String,
description: 'folder to store exported files. defaults to ./data'
},
{
name: 'query-file-path',
alias: 'y',
type: String,
description: 'path to a json file defining the query'
},
{
name: 'traverse',
type: Boolean,
description: 'traverse nodes found in initial query to find all related nodes'
},
{
name: 'traverse-query-file-path',
type: String,
description: 'path to a json file defining the traverse query'
}
];
}
function handleOptions() {
var options = cliArgs(getOptions());
if (options.help) {
printHelp(getOptions());
return null;
}
return options;
}
function printHelp(optionsList) {
console.log(commandLineUsage([{
header: 'Cloud CMS Export',
content: 'Export defintions and content instance records from a Cloud CMS project branch.'
},
{
header: 'Options',
optionList: optionsList
},
{
header: 'Examples',
content: [{
desc: '\n1. connect to Cloud CMS and list available definition qnames',
},
{
desc: 'npx cloudcms-util export --list-types'
},
{
desc: '\n2. export definitions and content records by qname:',
},
{
desc: 'npx cloudcms-util export --definition-qname "my:type1" "my:type2" --include-instances --folder-path ./data'
},
{
desc: '\n3. export all definition nodes:',
},
{
desc: 'npx cloudcms-util export --all-definitions --include-instances --folder-path ./data'
},
{
desc: '\n4. export a list of nodes based on a user defined query:',
},
{
desc: 'npx cloudcms-util export -y ./myquery.json --folder-path ./data'
}
]
}
]));
}