forked from marklogic-community/slush-marklogic-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slushfile.js
681 lines (609 loc) · 22.2 KB
/
slushfile.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
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
/* jshint node: true */
'use strict';
var gulp = require('gulp'),
FetchStream = require('fetch').FetchStream,
fs = require('fs'),
inquirer = require('inquirer'),
install = require('gulp-install'),
q = require('q'),
rename = require('gulp-rename'),
replace = require('gulp-replace'),
pkgSettings = require('./package.json'),
spawn = require('child_process').spawn,
uuid = require('node-uuid'),
win32 = process.platform === 'win32',
xmlpoke = require('gulp-xmlpoke');
/* jshint ignore:start */
var colors = require('colors'),
_ = require('underscore.string');
/* jshint ignore:end */
var npmVersion = null;
var settings = {};
var encoding = {
encoding: 'utf8'
};
var skipBinary = {
skipBinary: true
};
function printUsage() {
process.stdout.write('\n');
process.stdout.write('Usage:\n');
process.stdout.write(' slush marklogic-node <app-name> [args]\n');
process.stdout.write('\n');
process.stdout.write('Arguments:\n');
process.stdout.write(' fork=<..> Github fork to use for Roxy. Defaults to: marklogic\n');
process.stdout.write(' branch=<..> Github branch to use for Roxy. Defaults to: master\n');
process.stdout.write(' theme=<..> Slush theme to use. Defaults to: default\n');
process.stdout.write(' ml-version=<..> MarkLogic version. Defaults to: 8\n');
process.stdout.write(' ml-host=<..> Host on which MarkLogic runs. Defaults to: localhost\n');
process.stdout.write(' ml-admin-user=<..> User for MarkLogic deployments. Defaults to: admin\n');
process.stdout.write(' ml-admin-pass=<..> Pass for MarkLogic deployments. Defaults to: <blank>\n');
process.stdout.write(' ml-app-user=<..> MarkLogic user for guest access. Defaults to: <app-name>-user\n');
process.stdout.write(' ml-app-pass=<..> MarkLogic pass for guest access. Defaults to: <roxy-appuser-password>\n');
process.stdout.write(' ml-http-port=<..> Port at which MarkLogic app-server runs. Defaults to: 8040\n');
process.stdout.write(' ml-xcc-port=<..> Port at which MarkLogic xcc-server runs. Defaults to: <ml-app-port>\n');
process.stdout.write(' node-port=<..> Port at which Node.js middle-tier runs. Defaults to: 9070\n');
process.stdout.write(' guest-access=<..> Whether guests are automatically logged in. Defaults to: false\n');
process.stdout.write(' disallow-updates=<..> Whether updates to MarkLogic should be disallowed. Defaults to: false\n');
process.stdout.write(' appusers-only=<..> Whether front-end users get prefixed with <app-name>-. Defaults to: false\n');
process.stdout.write('\n');
process.stdout.write('Note:\n');
process.stdout.write(' Using command-line arguments will suppress the relevant questions.\n');
process.stdout.write(' To suppress all, provide arguments for all.\n');
process.stdout.write('\n');
}
function printVersionWarning() {
if (npmVersion && npmVersion !== pkgSettings.version.trim()) {
process.stdout.write('\n------------------------------------\n'.red);
process.stdout.write('Slush MarkLogic Node is out of date:\n'.bold.yellow);
process.stdout.write((' * Locally installed version: ' + pkgSettings.version + '\n').yellow);
process.stdout.write((' * Latest version: ' + npmVersion + '\n').yellow);
process.stdout.write(' * Run '.yellow + 'npm install -g slush-marklogic-node'.bold + ' to update\n'.yellow);
process.stdout.write('------------------------------------\n\n'.red);
npmVersion = null;
}
}
function checkLatestVersion() {
var latestVersion = q.defer();
try {
console.log('checking for latest version');
/* jshint camelcase:false */
var proxy = process.env.PROXY || process.env.http_proxy || null;
/* jshint camelcase:true */
var request = require('request');
request({
url: 'http://registry.npmjs.org/slush-marklogic-node/latest',
proxy: proxy
}, function(err, res, body) {
try {
npmVersion = JSON.parse(body).version;
} catch (e) {}
latestVersion.resolve();
});
} catch (e) {
latestVersion.resolve();
}
return latestVersion.promise;
}
function isFlag(arg) {
return (arg.indexOf('=') > -1);
}
function processInput() {
var allowedFlags = [
'app-name',
'fork',
'branch',
'theme',
'ml-version',
'ml-host',
'ml-admin-user',
'ml-admin-pass',
'ml-app-user',
'ml-app-pass',
'ml-http-port',
'ml-xcc-port',
'node-port',
'guest-access',
'disallow-updates',
'appusers-only'
];
var inputs = {
fork: 'marklogic',
branch: 'master'
};
(gulp.args || process.argv).forEach(function(arg) {
if (isFlag(arg)) {
var splits = arg.split('=');
var flag = splits[0].replace(/^-+/,'');
var value = splits[1];
if (flag === 'gulpfile') {
// ignore
} else if (allowedFlags.indexOf(flag) >= 0) {
inputs[flag] = value;
} else {
process.stdout.write(('\nERROR: unsupported argument \'' + arg + '\'.\n').red);
printUsage();
process.exit(1);
}
} else {
inputs['app-name'] = arg;
}
});
return inputs;
}
function getNameProposal() {
var path = require('path');
try {
return require(path.join(process.cwd(), 'package.json')).name;
} catch (e) {
return path.basename(process.cwd());
}
}
// Download the Roxy ml script from GitHub
function getRoxyScript(appName, mlVersion, fork, branch) {
fork = fork || 'marklogic';
branch = branch || 'master';
var d = q.defer(),
out;
var scriptName = (win32 ? '' : './') + 'ml' + (win32 ? '.bat' : '');
console.log('Retrieving Roxy script');
out = fs.createWriteStream(scriptName);
var stream = new FetchStream('https://github.com/' + fork + '/roxy/raw/' + branch + '/' + scriptName);
stream.pipe(out);
stream.on('end', function() {
console.log('Got Roxy script');
out.end();
fs.chmod(scriptName, '755', function(err) {
if (err) {
console.log(err);
d.reject(err);
} else {
console.log('chmod done; appName=' + appName + '; mlVersion=' + mlVersion + '; fork=' + fork + '; branch=' + branch);
d.resolve({
'script': scriptName,
'app': appName,
'mlVersion': mlVersion,
'fork': fork,
'branch': branch
});
}
});
});
return d.promise;
}
// Run the Roxy 'ml new' command for the new project
function runRoxy(config) {
var scriptName = config.script,
appName = config.app,
mlVersion = config.mlVersion,
fork = config.fork,
branch = config.branch;
var d = q.defer();
var args = [
'new',
appName,
'--server-version=' + mlVersion,
'--app-type=rest',
'--fork=' + fork,
'--branch=' + branch
];
console.log('Spawning Roxy new command: ' + scriptName + ' ' + args.join(' '));
var child = spawn(scriptName, args, {
stdio: [
0, // Use parents stdin for child
'pipe', // Pipe child's stdout to parent (default)
'pipe' // Pipe child's stderr to parent (default)
]
});
child.on('close', function() {
console.log('done running ml new');
d.resolve('done');
});
child.stdout.on('data', function(data) {
console.log('' + data);
});
child.stderr.on('data', function(data) {
console.log('' + data);
});
return d.promise;
}
// Make some changes to Roxy's deploy/build.properties file for the out-of-the-box application
function configRoxy() {
console.log('Configuring Roxy');
try {
var properties = fs.readFileSync('deploy/build.properties', encoding);
// Ensure the authentication-method property is set to digest
properties = properties.replace(/^authentication\-method=basic/m, 'authentication-method=digest');
properties = properties.replace(/^authentication\-method=digestbasic/m, 'authentication-method=digest');
// Capture appuser-password from Roxy properties for later use
var passwordMatch = /^appuser\-password=(.*)$/m;
var matches = passwordMatch.exec(properties);
settings.appuserPassword = matches[1];
// Bypass bug in Roxy causing {, } and \ in pwd to get mangled
if (settings.appuserPassword.match(/[\{\}\\]/g)) {
settings.appuserPassword = settings.appuserPassword.replace(/[\{\}\\]/g, '');
properties = properties.replace(passwordMatch, 'appuser-password=' + settings.appuserPassword);
}
fs.writeFileSync('deploy/build.properties', properties);
} catch (e) {
console.log('failed to update properties: ' + e.message);
process.exit(1);
}
try {
var localProperties = '#################################################################\n' +
'# This file contains overrides to values in build.properties\n' +
'# These only affect your local environment and should not be checked in\n' +
'#################################################################\n' +
'\n' +
'server-version=' + settings.mlVersion + '\n' +
'\n' +
'#\n' +
'# The ports used by your application\n' +
'#\n' +
'app-port=' + settings.appPort + '\n';
if (settings.xccPort || (settings.mlVersion < 8)) {
localProperties += 'xcc-port=' + settings.xccPort + '\n';
} else {
localProperties += '# Taking advantage of not needing a XCC Port for ML8\n' +
'xcc-port=${app-port}\n' +
'install-xcc=false\n';
}
localProperties += '\n' +
'#\n' +
'# the uris or IP addresses of your servers\n' +
'# WARNING: if you are running these scripts on WINDOWS you may need to change localhost to 127.0.0.1\n' +
'# There have been reported issues with dns resolution when localhost wasn\'t in the hosts file.\n' +
'#\n' +
'local-server=' + settings.marklogicHost + '\n' +
'content-forests-per-host=3\n' +
'\n' +
'#\n' +
'# Admin username/password that will exist on the local/dev/prod servers\n' +
'#\n' +
'user=' + settings.marklogicAdminUser + '\n' +
'password=' + settings.marklogicAdminPass + '\n';
fs.writeFileSync('deploy/local.properties', localProperties, encoding);
} catch (e) {
console.log('failed to write roxy local.properties');
process.exit(1);
}
try {
var foo = fs.readFileSync('deploy/ml-config.xml', encoding);
// add a geospatial index for the default content
foo = foo.replace(/^\s*<geospatial-element-pair-indexes>/m,
' <geospatial-element-pair-indexes>\n' +
' <geospatial-element-pair-index>\n' +
' <parent-namespace-uri/>\n' +
' <parent-localname>location</parent-localname>\n' +
' <latitude-namespace-uri/>\n' +
' <latitude-localname>latitude</latitude-localname>\n' +
' <longitude-namespace-uri/>\n' +
' <longitude-localname>longitude</longitude-localname>\n' +
' <coordinate-system>wgs84</coordinate-system>\n' +
' <range-value-positions>false</range-value-positions>\n' +
' </geospatial-element-pair-index>\n');
// add range path index for the default content
foo = foo.replace(/^\s*<range-path-indexes>/m,
' <range-path-indexes>\n' +
' <range-path-index>\n' +
' <scalar-type>string</scalar-type>\n' +
' <collation>http://marklogic.com/collation/codepoint</collation>\n' +
' <path-expression>docFormat</path-expression>\n' +
' <range-value-positions>false</range-value-positions>\n' +
' <invalid-values>reject</invalid-values>\n' +
' </range-path-index>\n' +
' <range-path-index>\n' +
' <scalar-type>string</scalar-type>\n' +
' <collation>http://marklogic.com/collation/codepoint</collation>\n' +
' <path-expression>eyeColor</path-expression>\n' +
' <range-value-positions>false</range-value-positions>\n' +
' <invalid-values>reject</invalid-values>\n' +
' </range-path-index>\n' +
' <range-path-index>\n' +
' <scalar-type>string</scalar-type>\n' +
' <collation>http://marklogic.com/collation/codepoint</collation>\n' +
' <path-expression>gender</path-expression>\n' +
' <range-value-positions>false</range-value-positions>\n' +
' <invalid-values>reject</invalid-values>\n' +
' </range-path-index>\n' +
' <range-path-index>\n' +
' <scalar-type>unsignedInt</scalar-type>\n' +
' <path-expression>age</path-expression>\n' +
' <range-value-positions>false</range-value-positions>\n' +
' <invalid-values>reject</invalid-values>\n' +
' </range-path-index>\n');
// fix default app-role privileges to match rest-style applications
foo = foo.replace(/<privileges>[^]*?<\/privileges>/,
'<privileges>\n' +
' <privilege>\n' +
' <privilege-name>rest-reader</privilege-name>\n' +
' </privilege>\n' +
' <!-- remove the rest-writer privilege if read-only access is required -->\n' +
' <privilege>\n' +
' <privilege-name>rest-writer</privilege-name>\n' +
' </privilege>\n' +
' </privileges>\n');
fs.writeFileSync('deploy/ml-config.xml', foo);
console.log('Applying replacements..');
gulp.src(['deploy/ml-config.xml'])
.pipe(xmlpoke({
replacements: [{
namespaces: {
db: 'http://marklogic.com/xdmp/database'
},
xpath: ['//db:database[1]/db:stemmed-searches','//db:database[1]/db:word-searches','//db:database[1]/db:trailing-wildcard-searches'],
valueType: 'remove'
},{
namespaces: {
db: 'http://marklogic.com/xdmp/database'
},
xpath: '//db:database[1]',
valueType: 'append',
value: ' <stemmed-searches>basic</stemmed-searches>\n <word-searches>true</word-searches>\n <trailing-wildcard-searches>true</trailing-wildcard-searches>\n '
}]
}))
.pipe(gulp.dest('deploy'));
} catch (e) {
console.log('failed to update configuration: ' + e.message);
process.exit(1);
}
}
gulp.task('npmInstall', ['init', 'generateSecret', 'configGulp'], function(done) {
return gulp.src(['./package.json'])
.pipe(install({
args: ['--msvs_version=2013'] // npm install --msvs_version=2013 // node-gyph depends on Visual C++ on Win
}));
});
gulp.task('default', ['npmInstall'], function(done) {
return gulp.src(['./bower.json'])
.pipe(install());
});
gulp.task('generateSecret', ['init'], function(done) {
try {
var nodeApp = fs.readFileSync('node-server/node-app.js', encoding);
//generate new uuid
var secret = uuid.v4();
nodeApp = nodeApp.replace(/\bsecret: '\b.*\b'/m, 'secret: \'' + secret + '\'');
fs.writeFileSync('node-server/node-app.js', nodeApp);
} catch (e) {
console.log('failed to update SECRET in node-server/node-app.js: ' + e.message);
process.exit(1);
}
done();
});
gulp.task('configGulp', ['init'], function(done) {
try {
var configJSON = {};
configJSON['app-name'] = settings.appName;
configJSON['ml-version'] = settings.mlVersion;
configJSON['ml-host'] = settings.marklogicHost;
configJSON['ml-admin-user'] = settings.marklogicAdminUser;
configJSON['ml-admin-pass'] = settings.marklogicAdminPass;
configJSON['ml-app-user'] = settings.appName + '-user';
configJSON['ml-app-pass'] = settings.appuserPassword;
configJSON['ml-http-port'] = settings.appPort;
if (settings.xccPort || (settings.mlVersion < 8)) {
configJSON['ml-xcc-port'] = settings.xccPort;
}
configJSON['node-port'] = settings.nodePort;
configJSON['guest-access'] = settings.guestAccess;
configJSON['disallow-updates'] = settings.disallowUpdates;
configJSON['appusers-only'] = settings.appUsersOnly;
var configString = JSON.stringify(configJSON, null, 2) + '\n';
fs.writeFileSync('local.json', configString, encoding);
} catch (e) {
console.log('failed to write local.json: ' + e.message);
process.exit(1);
}
done();
});
gulp.task('checkForUpdates', function(done) {
checkLatestVersion().then(function() {
printVersionWarning();
done();
});
});
gulp.task('init', ['checkForUpdates'], function(done) {
var clArgs = processInput();
var appName = clArgs['app-name'];
var prompts = [];
if (!clArgs['ml-version']) {
prompts.push({
type: 'list',
name: 'mlVersion',
message: 'MarkLogic version?',
choices: ['8', '7', '6', '5'],
default: 0
});
}
if (!clArgs['ml-host']) {
prompts.push({
type: 'input',
name: 'marklogicHost',
message: 'MarkLogic Host?',
default: 'localhost'
});
}
if (!clArgs['ml-admin-user']) {
prompts.push({
type: 'input',
name: 'marklogicAdminUser',
message: 'MarkLogic Admin User?',
default: 'admin'
});
}
if (!clArgs['ml-admin-pass']) {
prompts.push({
type: 'input',
name: 'marklogicAdminPass',
message: 'Note: consider keeping the following blank, ' +
'you will be asked to enter it at appropriate commands.\n? MarkLogic Admin Password?',
default: ''
});
}
if (!clArgs['ml-http-port']) {
prompts.push({
type: 'input',
name: 'appPort',
message: 'MarkLogic App/Rest port?',
default: 8040
});
}
if (!clArgs['ml-xcc-port']) {
prompts.push({
type: 'input',
name: 'xccPort',
message: 'XCC port?',
default: 8041,
when: function(answers) {
return (answers.mlVersion || clArgs['ml-version']) < 8;
}
});
}
if (!clArgs['node-port']) {
prompts.push({
type: 'input',
name: 'nodePort',
message: 'Node app port?',
default: 9070
});
}
if (!clArgs.theme) {
prompts.push({
type: 'list',
name: 'template',
message: 'Select Template',
choices: [{
name: 'default',
value: 'default'
}, {
name: '3-columns',
value: '3column'
}, {
name: 'Cards',
value: 'cards'
}, {
name: 'Dashboard',
value: 'dashboard'
}, {
name: 'Full-screen map',
value: 'map'
}, {
name: 'I don\'t know',
value: 'unsure'
}]
});
prompts.push({
type: 'list',
name: 'theme',
message: 'What is the main focus?',
when: function(ans) {
return ans.template === 'unsure';
},
choices: [{
name: 'Semantics',
value: '3column'
}, {
name: 'Charts',
value: 'dashboard'
}, {
name: 'Map/Graph',
value: 'map'
}, {
name: 'Data records',
value: 'cards'
}, {
name: 'Documents',
value: '3column'
}, {
name: 'Other',
value: 'default'
}]
});
}
if (!clArgs['guest-access']) {
prompts.push({
type: 'list',
name: 'guestAccess',
message: 'Allow anonymous users to search data?',
choices: ['false', 'true'],
default: 0
});
}
if (!clArgs['disallow-updates']) {
prompts.push({
type: 'list',
name: 'disallowUpdates',
message: 'Disallow proxying update requests?',
choices: ['false', 'true'],
default: 0
});
}
if (!clArgs['appusers-only']) {
prompts.push({
type: 'list',
name: 'appUsersOnly',
message: 'Only allow access to users created for this app? Note: disallows admin users.',
choices: ['false', 'true'],
default: 0
});
}
if (typeof appName === 'undefined') {
prompts.unshift({
type: 'input',
name: 'name',
message: 'Name for the app?',
default: getNameProposal()
});
}
inquirer.prompt(prompts, function(answers) {
// consolidate answers, clArgs, and defaults
if (typeof appName === 'undefined') {
settings.appName = _.slugify(answers.name);
} else {
settings.appName = _.slugify(appName);
}
settings.mlVersion = answers.mlVersion || clArgs['ml-version'];
settings.marklogicHost = answers.marklogicHost || clArgs['ml-host'];
settings.marklogicAdminUser = answers.marklogicAdminUser || clArgs['ml-admin-user'];
settings.marklogicAdminPass = answers.marklogicAdminPass || clArgs['ml-admin-pass'] || '';
settings.nodePort = answers.nodePort || clArgs['node-port'];
settings.appPort = answers.appPort || clArgs['ml-http-port'];
settings.xccPort = answers.xccPort || clArgs['ml-xcc-port'] || null;
settings.guestAccess = answers.guestAccess || clArgs['guest-access'];
settings.disallowUpdates = answers.disallowUpdates || clArgs['disallowed-updates'];
settings.appUsersOnly = answers.appUsersOnly || clArgs['appusers-only'];
settings.theme = answers.theme || answers.template || clArgs.theme || 'default';
getRoxyScript(settings.appName, settings.mlVersion, clArgs.fork, clArgs.branch)
.then(runRoxy)
.then(function() {
// Copy over the Angular files
var files = [__dirname + '/app/templates/**'];
if (settings.theme !== 'default') { // overlay the theme if not the default theme chosen
files.push(__dirname + '/app/themes/' + settings.theme + '/**');
}
process.chdir('./' + settings.appName);
configRoxy();
gulp.src(files)
.pipe(rename(function(file) {
// change _foo to .foo
if (file.basename[0] === '_') {
file.basename = '.' + file.basename.slice(1);
}
}))
.pipe(replace('@slush-version', pkgSettings.version.trim(), skipBinary))
.pipe(replace('@sample-app-name', settings.appName, skipBinary))
.pipe(replace('@node-port', settings.nodePort, skipBinary))
.pipe(replace('@ml-http-port', settings.appPort, skipBinary))
.pipe(replace('@ml-xcc-port', settings.xccPort || settings.appPort, skipBinary))
.pipe(replace('@ml-host', settings.marklogicHost, skipBinary))
.pipe(gulp.dest('./')) // Relative to cwd
.on('end', function() {
done(); // Finished!
});
},
function(reason) {
console.log('Caught an error: ' + reason);
});
});
});