-
Notifications
You must be signed in to change notification settings - Fork 2
/
mxgit.js
executable file
·779 lines (677 loc) · 21.1 KB
/
mxgit.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
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
#!/usr/bin/env node
/**
MXGIT - utility to manage Mendix projects with git
https://github.com/mweststrate/mxgit
Author: Michel Weststrate <mweststrate@gmail.com>
© 2014, MIT Licensed
*/
//V1.1:
//support in-modeler update so that it is less often required to reopen the repo
//see other known issues.
/* dependencies */
var fs = require('fs-extra'); //https://github.com/jprichardson/node-fs-extra
var child_process = require('child_process');
var path = require('path');
/* state */
var mprName = path.relative(process.cwd(), findMprFile());
var requiresModelerReload = false;
var ignoreMprLock = false;
var verbose = false;
var superverbose = false;
var lastInfoMsg;
var mprBasePath = path.dirname(mprName);
if (mprBasePath.length > 0)
mprBasePath += "/";
var MENDIX_CACHE = mprBasePath + '.mendix-cache/';
var BASE_DATA = MENDIX_CACHE + 'base_data';
var BASE_REV = MENDIX_CACHE + 'base_rev';
var BASE_REV_GIT = MENDIX_CACHE + 'base_rev_git';
var BASE_VER = MENDIX_CACHE + 'base_ver';
var FILE_OPTS = { encoding : 'utf-8' };
var BOGUS_REPO = "https://teamserver.sprintr.com/this_is_not_a_svn_repo_use_git/trunk";
var MERGE_MARKER = mprBasePath + "modeler-merge-marker";
function main() {
var yargs = require('yargs')
.usage("mxgit: Small tool that helps versioning Mendix projects with git\n" +
"See https://github.com/mweststrate/mxgit for the full documentation\n" +
"Copyright by Michel Weststrate<mweststrate@gmail.com> 2014. MIT Licensed\n" +
"\nBasic usage: 'mxgit -> creates or updates a local SVN working copy with the HEAD state of the git repo in this directory." +
"\nBasic usage: 'mxgit --install -> makes sure that mxgit is run automatically after every git command")
.describe('install', 'Creates git hooks so that the mxgit çommand no longer needs to be invoked manually. ')
.describe('reset', 'Removes the local SVN working copy and git hooks.')
.describe('setprojectid', '<projectid> Sets the Mendix projectid. Use this if you want story and cloud integration in the Mendix Business Modeler')
.describe('precommit', 'Determines whether the Mendix model can be commit safely')
.describe('postupdate', 'Same as no arguments, but ignores the model lock')
.describe('merge', 'Prepares the modeler for a three way merged. Takes three (temporarily) files as argument: <base> <mine> <theirs>. Used by the git merge driver.')
.describe('v', 'Verbose')
.describe('debug', 'Extremely verbose')
.describe('help', 'Prints this help')
.boolean(["install", "reset", "precommit", "postcommit", "v", "debug", "help", "merge"])
.string(["setprojectid"]);
var params = yargs.argv;
if (params.help) {
yargs.showHelp();
process.exit(0);
}
superverbose = params.debug;
verbose = superverbose || params.v;
info("using " + mprName + " (project dir: " + mprBasePath + ")");
seq([
makeAsync(checkGitDir),
checkSvnDir,
initializeSvnDir,
makeAsync(createCacheDir),
partial(interpretParams, params)
], function(err) {
if (err) {
console.info(err);
info("[ERROR] aborted.");
process.exit(1);
}
else {
if (requiresModelerReload)
console.info("\n>>> PLEASE REOPEN THE MODEL IN THE MENDIX BUSINESS MODELER <<<\n");
else
info("done.");
}
});
}
function interpretParams(params, callback) {
if (params.reset) {
reset(callback);
}
else if (params.setprojectid) {
updateSprintrProjectId(params.setprojectid, callback);
}
else if (params.install) {
seq([
initializeGitIgnore,
installGitHooks,
installMergeDriver,
updateStatus
], callback);
}
else if (params.precommit) {
checkMergeMarker();
checkMprLock();
callback();
}
else if (params.postupdate) {
ignoreMprLock = true;
updateStatus(callback);
}
else if (params.merge) {
gitMergeDrive(params._);
}
else {
updateStatus(callback);
}
}
function installGitHooks(callback) {
function writeGitHook(name, command) {
var filename = ".git/hooks/" + name;
if (fs.existsSync(filename))
console.info("The git hook '" + filename + "' already exists! Skipping.");
else {
fs.writeFileSync(filename, "#!/bin/sh\n#mxgit-marker-hook\necho 'git -> mxgit: running hook " + name + "'\nexec mxgit --" + command, FILE_OPTS);
if (process.platform != 'win32')
fs.chmodSync(filename, 0755 /*octal!*/) ;
}
}
//http://git-scm.com/book/en/Customizing-Git-Git-Hooks
//http://stackoverflow.com/a/4185449
//TODO: or use filter for .mpr file that as side effect runs update? that is less of a hassle with existing hooks
var hooks = {
"pre-commit" : "precommit",
"post-commit" : "postupdate",
"post-update" : "postupdate",
"post-checkout" : "postupdate",
"post-merge" : "postupdate"
};
info("setting up git hooks...");
for (var hook in hooks) {
writeGitHook(hook, hooks[hook]);
}
done();
callback();
}
function installMergeDriver(callback) {
info("setting up merge driver for .mpr files...");
/*
.git/info/attributes instead of .gitattributes,
so that the attributes are not stored in the repo, since others might not be using mxgit
*/
updateConfigFile(".git/info/attributes",["/*.mpr merge=mxgit"]);
/*
[merge "mxgit"]
name = mxgit merge driver for mpr's
driver = mxgit --merge %O %A %B
recursive = binary
*/
updateConfigFile(".git/config", [
"[merge \"mxgit\"]\n\tname = mxgit merge driver for mpr files\n\tdriver = mxgit --merge %O %A %B"
]);
done();
callback();
}
function reset(callback) {
info("resetting. Removing all traces of mxgit...");
//check which hooks are ours
var toRemove = [
".git/hooks/pre-commit",
".git/hooks/post-update",
".git/hooks/post-commit",
".git/hooks/post-checkout",
".git/hooks/post-merge"
].filter(function(hook) {
return fs.existsSync(hook) && fs.readFileSync(hook, FILE_OPTS).indexOf("#mxgit-marker-hook") != -1;
});
//remove SVN related dirs
toRemove.concat([
MENDIX_CACHE,
mprBasePath + ".svn"
]).map(function(thing) {
fs.removeSync(thing);
});
//clean altered config files
[
".gitignore",
".git/config",
".git/info/attributes"
].map(function(thing) {
if (fs.existsSync(thing))
cleanConfigFile(thing);
});
done();
callback();
}
function updateStatus(callback) {
seq([
makeAsync(checkMergeMarker),
makeAsync(checkMprLock),
updateBase,
when(isMprModified, function(cb) { cb(); }, markMprAsNotModified),
when(isMprConflicted, writeConflictData),
function(callback) {
debug("status updated.");
callback();
}
], callback);
}
function createCacheDir() {
debug("checking Mendix cache directory");
/* cache dir is used internally by the modeler to store version control meta data */
if (!fs.existsSync(MENDIX_CACHE)) {
info("creating Mendix cache directory");
fs.mkdirsSync(MENDIX_CACHE);
}
}
function checkGitDir() {
debug("checking git repository");
if (!fs.existsSync(".git")) {
console.info("[ERROR] Please run mxgit from a git directory");
process.exit(7);
}
}
function initializeGitIgnore(callback) {
debug("updating .gitignore file");
updateConfigFile(".gitignore", [
mprBasePath + ".svn",
mprBasePath + "modeler-merge-marker",
mprBasePath + ".mendix-cache",
mprBasePath + "*.mpr.lock",
mprBasePath + "*.mpr.bak",
mprBasePath + "*.mpr.left*",
mprBasePath + "*.mpr.right*",
mprBasePath + ".settings",
mprBasePath + "deployment",
mprBasePath + "releases",
"proxies",
"*.launch",
".classpath",
".project",
]);
callback();
}
function checkMprLock() {
debug("checking mpr lock");
/* a lock file exists as long as the mpr is opened in a modeler (or if the modeler didn't exit cleanly */
if (fs.existsSync(mprName + ".lock")) {
if (ignoreMprLock) {
console.info("[WARN] The file '" + mprName + "' is currently being edited in the Mendix Business Modeler.");
requiresModelerReload = true;
}
else {
console.info("[ERROR] The file '" + mprName + "' is currently being edited in the Mendix Business Modeler. Please close the project (or remove the lock file)");
process.exit(9);
}
}
}
function checkMergeMarker() {
debug("checking merge marker");
/* merge marker exists if as soon as the modeler has picked up a merge conflict, and created a new mpr from that. It disappears as soon as the model has no conflicts anymore, to indicate that the conflict has been resolved (which should be communicated to git by using a git add command) */
if (fs.existsSync(MERGE_MARKER)) {
console.info("[ERROR] The file '" + mprName + "' is currently being merged by the Mendix Business Modeler. Please resolve any model conflicts first.");
process.exit(10);
}
}
function checkSvnDir(callback) {
debug("checking svn repository");
/* svn dir shouldn't exists, or it should be our dummy repository */
if (fs.existsSync(mprBasePath + ".svn/wc.db")) {
execSvnQuery("select root from REPOSITORY", function(err, results) {
assertNotError(err);
if (results[0].trim() == BOGUS_REPO)
callback();
else {
console.info("[ERROR] This repository is currently managed by SVN / Mendix Teamserver. Please remove the current .svn directory before managing the repo with (mx)git. Repo: " + results[0]);
process.exit(8);
}
});
}
else
callback();
}
function initializeSvnDir(callback) {
if (!fs.existsSync(mprBasePath + ".svn")) {
info("initializing SVN dummy repository...");
/* the dummy repository is there to trick the modeler into thinking this project is properly versioned. We need to alter the actual db though to use the correct name of the mpr file, which might differ per project */
fs.copySync(__dirname + "/dummysvn/", mprBasePath + ".svn");
execSvnQuery(
"update NODES set local_relpath = '" + path.basename(mprName) + "' where local_relpath = 'GitBasedTeamserverRepo.mpr'",
function(err) {
assertNotError(err);
done();
callback();
}
);
}
else
callback();
}
function updateSprintrProjectId(projectid, callback) {
info("updating project id to '" + projectid+ "'...");
if (!/^[a-zA-Z0-9-_]+$/.test(projectid)) {
console.info("[ERROR] '" + projectid + "' doesn't look like a valid project id");
process.exit(11);
}
var needle = "mx:sprintr-project-id 14 dummyprojectid";
var replacement = "mx:sprintr-project-id " + projectid.length + " " + projectid;
//TODO: Note: this doesn't update the project id once it is set. Requires --reset first.
execSvnQuery(
"update NODES set properties = replace(properties, '" + needle + "', '" + replacement + "') where local_relpath = '' and kind = 'dir'",
function(err) {
assertNotError(err);
done();
callback();
}
);
}
function findMprFile() {
var files = [];
findMprFileHelper(process.cwd(), files);
if (files.length == 0) {
console.info("[ERROR] No .mpr file found in current working directory");
process.exit(2);
}
else if (files.length > 1) {
console.info("[ERROR] There should be exactly one .mpr file, but found: " + files.join(", "));
process.exit(2);
}
else
return files[0];
}
function findMprFileHelper(dir, results) {
var files = fs.readdirSync(dir);
for(var i = 0; i < files.length; i++) {
var file = dir + "/" + files[i];
if (fs.lstatSync(file).isDirectory())
findMprFileHelper(file, results);
else if (file.match(/\.mpr$/))
results.push(file);
}
}
function getMprMendixVersion(callback) {
debug("determine Mendix version");
execMprQuery("select _ProductVersion from _MetaData", function(err, lines) {
assertNotError(err);
callback(null, lines[0]);
});
}
function updateBase(callback) {
debug("updating base data");
using([
findLatestMprHash,
getMprMendixVersion
],
function(err, latestHash, version) {
assertNotError(err);
debug("using Mendix version " + version);
fs.writeFileSync(BASE_VER, version, FILE_OPTS);
fs.writeFileSync(BASE_REV, "2", FILE_OPTS);
if(latestHash !== null && fs.existsSync(BASE_REV_GIT) && latestHash == fs.readFileSync(BASE_REV_GIT, FILE_OPTS)) {
debug("already up to date");
callback();
}
else {
requiresModelerReload = true;
fs.writeFileSync(BASE_REV_GIT, latestHash, FILE_OPTS);
if (fs.existsSync(BASE_DATA))
fs.removeSync(BASE_DATA);
if (latestHash === null) {
fs.copySync(mprName, BASE_DATA);
info("initialized new base version of " + mprName);
copyBaseToPristine();
callback();
}
else {
info("wrote new base version of " + mprName);
storeBlobToFile(latestHash, BASE_DATA, function(err) {
assertNotError(err);
copyBaseToPristine();
callback();
});
}
}
});
}
function copyBaseToPristine() {
//Override the cached file for the mpr. This makes sure that if somebody accidentally
//executes an SVN revert, at least the proper base will be used.
//Note that we cannot simply break the revert function, since the modeler somehow internally depends on it.
debug("updating SVN cache..");
fs.copySync(BASE_DATA, mprBasePath + ".svn/pristine/19/190fc40c2d5f1f4ec60919d2db2be93a0053c48a.svn-base");
}
function findLatestMprHash(callback) {
debug("searching base version of " + mprName);
execGitCommand("ls-tree HEAD", function(err, treeFiles) {
if (err) {
console.info("[WARN] failed to find git HEAD: "+ err);
callback(null, null);
return;
}
for(var i = 0; i < treeFiles.length; i++)
if (treeFiles[i].indexOf(mprName) != -1) {
callback(null, treeFiles[i].split(/\s+/)[2]);
return;
}
callback(null, null);
});
}
function isMprModified(callback) {
debug("detecting modified status");
getGitFileStatus(function(status) {
callback(null, "M" == status);
});
}
function isMprConflicted(callback) {
debug("detecting conflict status");
getGitFileStatus(function(status) {
callback(null, "U" == status);
});
}
function getGitFileStatus(callback) {
debug("detecting file status");
execGitCommand("status --porcelain " + quoteFileName(mprName), function(err, lines) {
assertNotError(err);
if (lines.length) {
var status = lines[0].charAt(1);
debug("file status is '" + status + "'");
callback(status ? status : null);
}
else
callback(null);
});
}
function showMergeMessage() {
console.info("");
console.info(">>> MERGE CONFLICT DETECTED. PLEASE SOLVE THE CONFLICTS IN THE MODELER <<<");
console.info(">>> TO MARK RESOLVED, USE 'git add " + mprName +"' <<<");
console.info("");
}
function markMprAsConflicted(callback) {
seq([
markMprAsNotModified,
partial(execSvnQuery, "insert into ACTUAL_NODE (wc_id, local_relpath, conflict_old, conflict_new) values (1,'"+ path.basename(mprName) +"','"+ path.basename(mprName) +".left','"+ path.basename(mprName) +".right')")
], callback);
}
function markMprAsNotModified(callback) {
execSvnQuery("delete from ACTUAL_NODE where local_relpath = '" + path.basename(mprName) + "'", callback);
}
function gitMergeDrive(fileArray) {
// mxgit --merge as merge driver was introduced because git itself has no hook whenever a merge fails,
// which is exactly the case we are interested in. So we introduce our own driver that
// stores the conflict data in svn and after that marks the conflict as unresolved
debug("processing mpr merge " + fileArray);
if (fileArray.length != 3)
throw "Expected exactly three arguments for a --merge command";
fs.copySync(fileArray[0], mprName + ".left");
fs.copySync(fileArray[2], mprName + ".right");
markMprAsConflicted(function(err) {
assertNotError(err);
showMergeMessage();
//https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html#_defining_a_custom_merge_driver
//Exit non-zero, mark the file as conflicted so that the modeler will merge it
//(in fact, it would do the same if we would return zero, but this makes it more clear that
//a conflict should be merged; use git add in the end to mark resolved)
process.exit(1);
});
}
function writeConflictData(callback) {
info("merge conflict detected, writing merge information...");
requiresModelerReload = true;
/* A SVN conflict is stored as follows:
conflict_old column in ACTUAL_NODE table: mpr.merge-left.r# (BASE), conflict_new column: mpr.merge-right.r# (THEIRS)
modeler-merge-marker appears as soon as the file is merged by the modeler, but still has conflicts. Disappears as soon as last conflict is resolved in the modeler. */
execGitCommand("ls-files -u " + quoteFileName(mprName), function(err, mergestatus) {
assertNotError(err);
if (mergestatus.length < 3)
callback("mxgit: cannot handle the current conflict, please use an external tool");
else {
var baseblob = mergestatus[0].split(/\s+/)[1];
var theirsblob = mergestatus[2].split(/\s+/)[1];
seq([
partial(storeBlobToFile, baseblob, mprName + ".left"),
partial(storeBlobToFile, theirsblob, mprName + ".right"),
markMprAsConflicted
], function(err) {
assertNotError(err);
done();
showMergeMessage();
callback();
});
}
});
}
function storeBlobToFile(hash, filename, callback) {
var out = fs.openSync(filename, 'w');
var child = child_process.spawn("git", ["cat-file", "-p", hash], {
stdio: ['pipe', out, 'pipe']
});
child.on('close', function() {
debug("stored " + hash + " -> " + filename);
callback();
});
}
function updateConfigFile(filename, requiredItems) {
var contents = "";
if (fs.existsSync(filename))
contents = fs.readFileSync(filename, FILE_OPTS);
var missing = [];
for(var i = 0; i < requiredItems.length; i++) {
var re = new RegExp("(^|\n)(" + escapeRegExp(requiredItems[i]) + ")($|\r?\n)");
if (!re.test(contents))
missing.push(requiredItems[i]);
}
if (missing.length) {
missing.unshift("\n#mxgit-marker-start");
missing.push("#mxgit-marker-end\n");
fs.appendFileSync(filename, missing.join("\n"), FILE_OPTS);
}
}
function cleanConfigFile(filename) {
if (fs.existsSync) {
var data = fs.readFileSync(filename, FILE_OPTS);
var newdata = data.replace(/(\n#mxgit-marker-start)([\s\S]*?)(#mxgit-marker-end\n)/g,"");
if (newdata != data)
fs.writeFileSync(filename, newdata, FILE_OPTS);
}
}
function escapeRegExp(str) {
//http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
/**
Logging and startup
*/
function info(msg) {
lastInfoMsg = msg;
console.info("mxgit: " + msg);
}
function done() {
console.info("mxgit: " + lastInfoMsg + " DONE");
}
function debug(msg) {
if (verbose)
console.info("\t* " + msg);
}
if ((typeof (module) !== "undefined" && !module.parent))
main();
/**
*
* Section: utility functions to invoke git, svn or mpr updates
*
*/
function execSvnQuery(query, callback) {
execSqliteQuery(process.cwd() + path.sep + mprBasePath + ".svn/wc.db", query, callback);
}
function execMprQuery(query, callback) {
execSqliteQuery(mprName, query, callback);
}
function execSqliteQuery(file, query, callback) {
var sqlite = process.platform == 'win32' ? __dirname + "\\sqlite3" : "sqlite3";
query = query.replace(/[\\\"]/g, function(r) { return "\\" + r; });
execCommand([sqlite, quoteFileName(file), "\"" + query + "\""].join(" "), callback);
}
function execGitCommand(command, callback) {
execCommand("git " + command, callback);
}
function execCommand(command, callback) {
if (superverbose)
console.info("\t\t\t > " + command);
child_process.exec(command, function(error, stdout) {
if (error) {
callback(error);
}
else {
if (superverbose)
console.info("\t\t\t < " + stdout);
callback(null, stdout.split(/\r?\n/));
}
});
}
function quoteFileName(filename) {
return process.platform == 'win32' ? "\"" + filename + "\"" : "'" + filename + "'";
}
/**
*
* Section: utility methods for asynchronous function calls.
* (of course, a lib like 'async' could be used for this,
* but I wrote it as small attempt to remember what those
* libs actually do)
*
*/
function seq(funcs /* [func(callback(err))] */, callback /*optional func(err)) */) {
function next(nextItem) {
if (!nextItem) {
if (callback) {
callback();
}
}
else {
nextItem(function(err) {
if (err) {
if (callback)
callback(err);
else
throw err;
}
else
next(funcs.shift());
});
}
}
next(funcs.shift());
}
function using(paramFuncs /* [func(callback(err,res))] */, callback /* func(err, res) */) {
var params = [];
var left = paramFuncs.length;
var aborted = false;
paramFuncs.map(function(func, idx) {
func(function(err, res) {
if (aborted)
return;
else if (err) {
aborted = true;
callback(err);
}
else {
params[idx] = res;
if (--left === 0)
callback.apply(null, [null].concat(params));
}
});
});
}
function when(condfunc, whenfunc /*or array*/, elsefunc /*optional, or array*/) {
function wrapSequence(func) {
if (Array.isArray(func)) {
return partial(seq, func); //how convenient
}
else
return func;
}
return function(callback) {
condfunc(function(err, conditionResult) {
if (err)
callback(err);
else if (conditionResult)
wrapSequence(whenfunc)(callback);
else {
if (elsefunc)
wrapSequence(elsefunc)(callback);
else
callback();
}
});
};
}
function makeAsync(func) {
return function(callback) {
var res;
try {
res = func();
}
catch(e) {
callback(e);
return;
}
callback(null, res);
};
}
function partial(func/*, args*/) {
var cargs = arguments;
var scope = this;
return function() {
var args = [];
for(var i = 1; i < cargs.length; i++)
args.push(cargs[i]);
for(var j = 0; j < arguments.length; j++)
args.push(arguments[j]);
return func.apply(scope, args);
};
}
function assertNotError(err) {
if (err)
console.trace(err);
}