forked from marklogic-community/slush-marklogic-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslushfile.js
359 lines (292 loc) · 10.6 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
/* jshint node: true */
'use strict';
var gulp = require('gulp'),
colors = require('colors'),
FetchStream = require('fetch').FetchStream,
fs = require('fs'),
inquirer = require('inquirer'),
install = require('gulp-install'),
q = require('q'),
rename = require('gulp-rename'),
pkgSettings = require('./package.json'),
spawn = require('child_process').spawn,
win32 = process.platform === 'win32',
_ = require('underscore.string')
;
var npmVersion = null;
var settings = {
includeEsri: false
};
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');
var proxy = process.env.PROXY || process.env.http_proxy || null;
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 inputs = {appType: 'rest', branch: 'master'};
gulp.args.forEach(function(arg) {
if (isFlag(arg)) {
var splits = arg.split('=');
var flag = splits[0];
var value = splits[1];
if (flag === 'appType') {
inputs.appType = value;
} else if (flag === 'branch') {
inputs.branch = value;
} else {
var message = arg + '\n is not a supported flag and has been ignored. You can specify appType=<appType> to specify Roxy appType or branch=<branch> to specify a Roxy branch\n';
process.stdout.write(message.red);
}
} else {
inputs.appName = 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, appType, branch) {
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/marklogic/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 + '; appType=' + appType + '; branch=' + branch);
d.resolve({
'script': scriptName,
'app': appName,
'mlVersion': mlVersion,
'appType': appType,
'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,
appType = config.appType,
branch = config.branch;
var d = q.defer();
var args = [
'new',
appName,
'--server-version=' + mlVersion,
'--app-type=' + appType,
'--branch=' + branch
];
console.log('Spawning Roxy new command: ' + scriptName + ' ' + args.join(' '));
var child = spawn(scriptName, args);
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(appPort,xccPort) {
console.log('Configuring Roxy');
try {
var properties = fs.readFileSync('deploy/build.properties', { encoding: 'utf8' });
// set the authentication-method property to digestbasic
properties = properties.replace(/^authentication\-method=digest/m, 'authentication-method=digestbasic');
//set the ports
properties = properties.replace(/^app\-port=\b\d*\b/m, 'app-port=' + appPort);
properties = properties.replace(/^xcc\-port=\b\d*\b/m, 'xcc-port=' + xccPort);
fs.writeFileSync('deploy/build.properties', properties);
} catch (e) {
console.log('failed to update properties: ' + e.message);
}
try {
var foo = fs.readFileSync('deploy/ml-config.xml', { encoding: 'utf8' });
// add an index for the default content
foo = foo.replace(/^\s*<range-element-indexes>/m,
' <range-element-indexes>\n' +
' <range-element-index>\n' +
' <scalar-type>string</scalar-type>\n' +
' <namespace-uri/>\n' +
' <localname>eyeColor</localname>\n' +
' <collation>http://marklogic.com/collation/codepoint</collation>\n' +
' <range-value-positions>false</range-value-positions>\n' +
' </range-element-index>\n');
fs.writeFileSync('deploy/ml-config.xml', foo);
} catch (e) {
console.log('failed to update configuration: ' + e.message);
}
}
gulp.task('default', ['init', 'configGulp', 'configNodeExService', 'configEsri'], function(done) {
gulp.src(['./bower.json', './package.json'])
.pipe(install());
});
gulp.task('configEsri', ['init'], function(done) {
if (!settings.includeEsri) {
var indexData, appData;
try {
// Update the index.html file.
indexData = fs.readFileSync('ui/index.html', { encoding: 'utf8' });
indexData = indexData.replace(/^.*arcgis.*$[\r\n]/gm, '');
indexData = indexData.replace(/^.*esri.*$[\r\n]/gm, '');
fs.writeFileSync('ui/index.html', indexData);
} catch (e) {
console.log('failed to update index.html: ' + e.message);
}
try {
// Update the app.js file.
appData = fs.readFileSync('ui/app/app.js', { encoding: 'utf8' });
appData = appData.replace(/^.*esriMap.*$[\r\n]/gm, '');
fs.writeFileSync('ui/app/app.js', appData);
} catch (e) {
console.log('failed to update app.js: ' + e.message);
}
}
done();
});
gulp.task('configGulp', ['init'], function(done) {
try {
var config = fs.readFileSync('gulp.config.js', { encoding: 'utf8' });
//set the ports
config = config.replace(/\bdefaultPort: '\b\d*\b'/m, 'defaultPort: \'' + settings.nodePort + '\'');
config = config.replace(/\bport: '\b\d*\b'/m, 'port: \'' + settings.appPort + '\'');
fs.writeFileSync('gulp.config.js', config);
} catch (e) {
console.log('failed to update gulp.config.js: ' + e.message);
}
done();
});
gulp.task('configNodeExService', ['init'], function(done) {
try {
var nodeService = fs.readFileSync('etc/init.d/node-express-service', { encoding: 'utf8' });
//set the ports
nodeService = nodeService.replace(/\bAPP_PORT=\b\d*\b/m, 'APP_PORT=' + settings.nodePort);
nodeService = nodeService.replace(/\bML_PORT=\b\d*\b/m, 'ML_PORT=' + settings.appPort);
fs.writeFileSync('etc/init.d/node-express-service', nodeService);
} catch (e) {
console.log('failed to update node-express-service: ' + e.message);
}
done();
});
gulp.task('checkForUpdates', function(done) {
checkLatestVersion().then(function() {
printVersionWarning();
done();
});
});
gulp.task('init', ['checkForUpdates'], function (done) {
var clArgs = processInput();
var appName = clArgs.appName;
var appType = clArgs.appType;
var branch = clArgs.branch;
var prompts = [
{type: 'input', name: 'nodePort', message: 'Node app port?', default: 9070},
{type: 'input', name: 'appPort', message: 'MarkLogic App/Rest port?', default: 8040},
{type: 'input', name: 'xccPort', message: 'XCC port?', default:8041},
{type: 'list', name: 'mlVersion', message: 'MarkLogic version?', choices: ['8','7', '6', '5'], default: 0},
{type: 'confirm', name: 'includeEsri', message: 'Include ESRI Maps?', default: false}
];
if (typeof appName === 'undefined') {
prompts.unshift(
{type: 'input', name: 'name', message: 'Name for the app?', default: getNameProposal()});
}
inquirer.prompt(prompts, function (answers) {
if (typeof appName === 'undefined') {
answers.nameDashed = _.slugify(answers.name);
} else {
answers.nameDashed = _.slugify(appName);
}
settings.nodePort = answers.nodePort;
settings.appPort = answers.appPort;
settings.includeEsri = answers.includeEsri;
getRoxyScript(answers.nameDashed, answers.mlVersion, appType, branch)
.then(runRoxy)
.then(function() {
// Copy over the Angular files
var files = [__dirname + '/app/templates/**'];
// Adjust files to copy based on whether ESRI Maps are included
if (!answers.includeEsri) {
files.push('!' + __dirname + '/app/templates/ui/app/esri-map/**');
files.push('!' + __dirname + '/app/templates/ui/app/esri-map');
files.push('!' + __dirname + '/app/templates/ui/app/detail/detail.html');
}
else {
files.push('!' + __dirname + '/app/templates/ui/app/detail/detail-no-esri.html');
}
process.chdir('./' + answers.nameDashed);
configRoxy(answers.appPort, answers.xccPort);
gulp.src(files)
.pipe(rename(function (file) {
// change _foo to .foo
if (file.basename[0] === '_') {
file.basename = '.' + file.basename.slice(1);
}
// Rename detail file when not using ESRI.
else if (!answers.includeEsri && file.basename === 'detail-no-esri' && file.extname === '.html') {
console.log('changed name to detail.html');
file.basename = 'detail';
}
}))
.pipe(gulp.dest('./')) // Relative to cwd
.on('end', function () {
done(); // Finished!
});
},
function(reason) {
console.log('Caught an error: ' + reason);
});
});
});