-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path88.js
More file actions
1021 lines (881 loc) · 26.2 KB
/
Copy path88.js
File metadata and controls
1021 lines (881 loc) · 26.2 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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global __dirname process require */
// This script doesn't use JS syntax, packages, or APIs *un*supported by any
// node version >=4.0.0, so unsupported versions from v4.0.0+ will get the
// intended error messages. A future version of this script could instead rely
// on babel to achieve the same goal.
// See: https://node.green/
// KEY ASSUMPTION: for a DApp to be valid, from embark's perspective, it must
// have a parsable embark.json file in its top-level directory; if that
// requirement changes in the future then this script must be revised.
// Hypothetical example of such a change: embark config info may be included in
// package.json under `{"embark": {...}}` -or- stored in an embark.json file.
function main() {
if (whenNoShim()) return;
var invoked = thisEmbark();
var embarkJson = findEmbarkJson();
var dappPath = embarkJson.dirname;
process.chdir(dappPath);
process.env.DAPP_PATH = dappPath;
process.env.PWD = dappPath;
/* attempt to find a "local" embark in or above but not below dappPath
let `dappPath/(([../])*)bin/embark` be a "containing" embark
let `dappPath/(([../])*)node_modules/embark/bin/embark` be an "installed"
embark
if containing and installed embarks are both found, and if containing
embark is higher in the dir structure than installed embark, then
containing embark will be selected
according to the rule above: if an installed embark is found within a
containing embark's own node_modules (that would be odd), installed embark
will be selected
invoked embark may find itself as local embark, but that is detected by
comparing `binrealpath` props to avoid double-checking and infinite loops
if no local embark is found then cmd execution will use invoked embark */
var containing = findBinContaining(dappPath, invoked);
var installed = findBinInstalled(dappPath, invoked);
var local = selectLocal(containing, installed, invoked);
var pkgJson = findPkgJson(dappPath, embarkJson, local);
process.env.PKG_PATH = pkgJson.dirname;
var embark = select(invoked, local);
process.env.EMBARK_PATH = embark.pkgDir;
embark.exec();
}
// -----------------------------------------------------------------------------
var checkDeps = require('../lib/utils/checkDependencies');
var npmlog = require('npmlog');
var findUp = require('find-up');
var fs = require('fs');
var path = require('path');
var parseJsonWithErrors = require('json-parse-better-errors');
var pkgUp = require('pkg-up');
var semver = require('semver');
var subdir = function (pdir_, dir_) {
var pdir = path.resolve(path.normalize(pdir_)) + (path.sep || '/');
var dir = path.resolve(pdir, path.normalize(dir_));
if (pdir === '//') pdir = '/';
if (pdir === dir) return false;
return dir.slice(0, pdir.length) === pdir;
};
// -- embark bins --------------------------------------------------------------
function EmbarkBin(binpath, kind) {
this.binpath = binpath;
this.binrealpath = undefined;
this.kind = kind || 'invoked';
this.pkgDir = undefined;
this.pkgJson = undefined;
}
EmbarkBin.prototype.exec = function () {
var Cmd = require('../cmd/cmd');
var cli = new Cmd();
if(_logged) { console[this.loglevel](); }
cli.process(process.argv);
};
EmbarkBin.prototype.handle = function () {
this.setup();
this.log();
return this;
};
EmbarkBin.prototype.log = function () {
this.logMissingBin();
this.pkgJson.log();
};
EmbarkBin.prototype.loglevel = 'info';
EmbarkBin.prototype.logMissingBin = function () {
var oldlevel = this.loglevel;
this.loglevel = 'error';
if (!this.binrealpath) {
reportMissingFile_EmbarkBin(this.binpath, this.kind, this.loglevel);
exitWithError();
}
this.loglevel = oldlevel;
};
EmbarkBin.prototype.setBinrealpath = function () {
if (this.binpath) {
this.binrealpath = realpath(this.binpath);
}
};
EmbarkBin.prototype.setPkgDir = function () {
if (this.binpath) {
this.pkgDir = path.join(path.dirname(this.binpath), '..');
}
};
EmbarkBin.prototype.setPkgJson = function () {
if (this.binrealpath) {
this.pkgJson = (
new PkgJsonEmbark(
path.join(this.pkgDir, 'package.json'),
this.kind
)
);
var upNodeModules = findUp.sync(
'node_modules',
{cwd: path.join(path.dirname(this.binrealpath), '../..')}
);
this.pkgJson.noCheck = (
upNodeModules ? subdir(upNodeModules, this.binrealpath) : false
);
this.pkgJson.setup();
}
};
EmbarkBin.prototype.setup = function () {
this.setBinrealpath();
this.setPkgDir();
this.setPkgJson();
return this;
};
// -- bin/embark :: local ------------------------------------------------------
function EmbarkBinLocal(binpath, invokedEmbark) {
EmbarkBin.call(this, binpath, 'local');
this.invokedEmbark = invokedEmbark;
}
setupProto(EmbarkBinLocal, EmbarkBin);
EmbarkBinLocal.prototype.exec = function () {
process.argv[1] = this.binpath;
process.env.EMBARK_NO_SHIM = true;
console[this.loglevel]();
require(this.binpath);
};
EmbarkBinLocal.prototype.log = function () {
if (this.binrealpath !== this.invokedEmbark.binrealpath) {
this.logSwitching();
EmbarkBin.prototype.log.call(this);
}
};
EmbarkBinLocal.prototype.logSwitching = function () {
reportSwitching(
this.invokedEmbark.binpath,
this.binpath,
this.loglevel,
this.invokedEmbark.pkgJson.pkg,
this.pkgJson.pkg
);
};
EmbarkBinLocal.prototype.setup = function () {
this.setBinrealpath();
this.setPkgDir();
if (this.binrealpath !== this.invokedEmbark.binrealpath) {
this.setPkgJson();
}
return this;
};
// -- bin/embark :: local containing -------------------------------------------
function EmbarkBinLocalContaining(binpath, invokedEmbark) {
EmbarkBinLocal.call(this, binpath, invokedEmbark);
}
setupProto(EmbarkBinLocalContaining, EmbarkBinLocal);
EmbarkBinLocalContaining.prototype.setPkgJson = function () {
if (this.binrealpath) {
this.pkgJson = (
new PkgJsonEmbark(
path.join(this.pkgDir, 'package.json'),
this.kind
)
);
this.pkgJson.noCheck = false;
this.pkgJson.setup();
}
};
// -- bin/embark :: local installed --------------------------------------------
function EmbarkBinLocalInstalled(binpath, invokedEmbark) {
EmbarkBinLocal.call(this, binpath, invokedEmbark);
}
setupProto(EmbarkBinLocalInstalled, EmbarkBinLocal);
EmbarkBinLocalInstalled.prototype.log = function () {
EmbarkBinLocal.prototype.log.call(this);
this.pkgJsonLocalExpected.log();
};
EmbarkBinLocalInstalled.prototype.setPkgJson = function () {
if (this.binrealpath) {
this.pkgJson = (
new PkgJsonEmbark(
path.join(this.pkgDir, 'package.json'),
this.kind
)
).setup();
}
};
EmbarkBinLocalInstalled.prototype.setPkgJsonLocalExpected = function () {
if (this.binrealpath) {
this.pkgJsonLocalExpected = (
new PkgJsonLocalExpected(path.join(this.pkgDir, '../../package.json'))
).setup();
}
};
EmbarkBinLocalInstalled.prototype.setup = function () {
EmbarkBinLocal.prototype.setup.call(this);
this.setPkgJsonLocalExpected();
return this;
};
// -- finders ------------------------------------------------------------------
function findBin(dappPath, find, invoked, Kind) {
return (
new Kind(
findUp.sync(find, {cwd: dappPath}),
invoked
)
).setup();
}
function findBinContaining(dappPath, invoked) {
return findBin(
dappPath,
'bin/embark',
invoked,
EmbarkBinLocalContaining
);
}
function findBinInstalled(dappPath, invoked) {
return findBin(
dappPath,
'node_modules/embark/bin/embark',
invoked,
EmbarkBinLocalInstalled
);
}
function findEmbarkJson() {
// findUp search begins in process.cwd() by default, but embark.json could
// be in a subdir if embark was invoked via `npm run` (which changes cwd to
// package.json's dir) and the package.json is in a dir above the top-level
// DApp dir; so start at INIT_CWD if that has been set (by npm, presumably)
// See: https://docs.npmjs.com/cli/run-script
var startDir = initCwd();
return (new EmbarkJson(
findUp.sync('embark.json', {cwd: startDir}) ||
path.join(startDir, 'embark.json'),
process.argv[2]
)).handle();
}
function findPkgJson(dappPath, embarkJson, local) {
var skipDirs = [];
if (local) {
if (local instanceof EmbarkBinLocalContaining) {
skipDirs.push(local.pkgDir);
}
if (local instanceof EmbarkBinLocalInstalled) {
skipDirs.push(local.pkgJsonLocalExpected.dirname);
}
}
var closest, dir, found;
var startDir = dappPath;
/* let `dappPath/(([../])*)package.json` be a "local" package.json */
// look for local package.json files starting from dappPath
function stop() {
found = pkgUp.sync(startDir);
if (found && !closest) {
closest = found;
}
dir = found ? path.dirname(found) : found;
var stop = !dir || !isDappCmd(embarkJson.cmd);
if (!stop) {
startDir = path.join(dir, '..');
}
return stop;
}
while (!stop()) {
if (skipDirs.indexOf(dir) === -1) {
(new PkgJsonLocal(found)).handle();
}
}
if (isDappCmd(embarkJson.cmd) && !closest) {
var loglevel = 'error';
reportMissingFile(path.join(dappPath, 'package.json'), loglevel);
reportMissingFile_DappJson(embarkJson.cmd, loglevel, 'package', 'in or above');
exitWithError();
}
return (
closest || (new PkgJsonLocal(path.join(startDir, 'package.json'))).setup()
);
}
// -- json files ---------------------------------------------------------------
function Json(filepath) {
this.filepath = filepath;
this.dirname = undefined;
this.json = undefined;
this.realpath = undefined;
}
Json.prototype.handle = function () {
this.setup();
this.log();
return this;
};
Json.prototype.log = function () {
this.logMissingFile();
this.logUnparsable();
};
Json.prototype.loglevel = 'warn';
Json.prototype.logMissingFile = function () {
var missing;
if (!this.realpath) {
missing = true;
reportMissingFile(this.filepath, this.loglevel);
}
return missing;
};
Json.prototype.logUnparsable = function () {
var unparsable;
if (this.realpath && !this.json) {
unparsable = true;
reportUnparsable(this.filepath, this.loglevel);
}
return unparsable;
};
Json.prototype.setDirname = function () {
if (this.filepath) {
this.dirname = path.dirname(this.filepath);
}
};
Json.prototype.setJson = function () {
if (this.realpath) {
this.json = parseJson(this.filepath);
}
};
Json.prototype.setRealpath = function () {
if (this.filepath) {
this.realpath = realpath(this.filepath);
}
};
Json.prototype.setup = function () {
this.setDirname();
this.setRealpath();
this.setJson();
return this;
};
// -- embark.json --------------------------------------------------------------
function EmbarkJson(filepath, cmd) {
Json.call(this, filepath);
this.cmd = cmd;
}
setupProto(EmbarkJson, Json);
EmbarkJson.prototype.loglevel = 'error';
EmbarkJson.prototype.log = function () {
this.logMissingFile();
this.logUnparsable();
};
EmbarkJson.prototype.logMissingFile = function () {
if (isDappCmd(this.cmd) && Json.prototype.logMissingFile.call(this)) {
reportMissingFile_DappJson(this.cmd, this.loglevel, 'embark', 'in');
exitWithError();
}
};
EmbarkJson.prototype.logUnparsable = function () {
if (isDappCmd(this.cmd) && Json.prototype.logUnparsable.call(this)) {
reportUnparsable_EmbarkJson(this.loglevel);
exitWithError();
}
};
// -- package.json -------------------------------------------------------------
function PkgJson(filepath) {
Json.call(this, filepath);
}
setupProto(PkgJson, Json);
PkgJson.prototype.log = function () {
Json.prototype.log.call(this);
this.logPkgErrors();
};
PkgJson.prototype.logPkgErrors = function () {
var pkgErrors;
if (this.json && !this.noCheck) {
pkgErrors = checkPkg(this.dirname);
}
if (pkgErrors) {
reportPkgErrors(pkgErrors, this.filepath, this.loglevel);
}
return !!pkgErrors;
};
PkgJson.prototype.noCheck = false;
// -- package.json :: of an embark pkg -----------------------------------------
function PkgJsonEmbark(filepath, kind) {
PkgJson.call(this, filepath);
this.kind = kind || 'invoked';
this.nodeRange = undefined;
this.pkg = undefined;
this.version = undefined;
}
setupProto(PkgJsonEmbark, PkgJson);
PkgJsonEmbark.prototype.log = function () {
PkgJson.prototype.log.call(this);
this.logMissingVersion();
this.logUnsupportedNode();
};
PkgJsonEmbark.prototype.loglevel = 'error';
PkgJsonEmbark.prototype.logMissingFile = function () {
if (PkgJson.prototype.logMissingFile.call(this)) {
reportMissingFile_PkgJsonEmbark(this.kind, this.loglevel);
exitWithError();
}
};
PkgJsonEmbark.prototype.logMissingVersion = function () {
var missing;
var oldlevel = this.loglevel;
this.loglevel = 'warn';
if (this.json && this.version === '???') {
missing = true;
reportMissingVersion(this.filepath, this.kind, this.loglevel);
}
this.loglevel = oldlevel;
return missing;
};
PkgJsonEmbark.prototype.logPkgErrors = function () {
if (PkgJson.prototype.logPkgErrors.call(this)) {
reportPkgErrors_PkgJsonEmbark(this.dirname, this.kind, this.loglevel);
exitWithError();
}
};
PkgJsonEmbark.prototype.logUnparsable = function () {
if (PkgJson.prototype.logUnparsable.call(this)) {
reportUnparsable_PkgJsonEmbark(this.kind, this.loglevel);
exitWithError();
}
};
PkgJsonEmbark.prototype.logUnsupportedNode = function () {
var missing;
var range = this.nodeRange;
if (typeof range === 'undefined') {
missing = true;
range = this.nodeRangeDefault;
}
var bad;
range = parseRange(range);
if (!range) {
bad = true;
range = this.nodeRangeDefault;
}
var procVer = semver.clean(process.version);
this.loglevel = 'error';
if (!semver.satisfies(procVer, range)) {
reportUnsupportedNode(
bad,
this.filepath,
this.kind,
this.loglevel,
missing,
this.nodeRangeDefault,
this.nodeRange,
this.pkg,
procVer,
range
);
exitWithError();
}
};
PkgJsonEmbark.prototype.noCheck = true;
// if changing to the `nodeRangeDefault` value, make sure to manually check
// that it's a valid semver range, otherwise fallback logic in the prototype
// methods won't be reliable
PkgJsonEmbark.prototype.nodeRangeDefault = semver.Range('>=8.11.3').range;
PkgJsonEmbark.prototype.setNodeRange = function () {
if (isObject(this.json) &&
this.json.hasOwnProperty('runtime') &&
this.json.runtime.hasOwnProperty('engines') &&
this.json.runtime.engines.hasOwnProperty('node')) {
this.nodeRange = this.json.runtime.engines.node;
}
};
PkgJsonEmbark.prototype.setPkg = function () {
this.pkg = `embark@${this.version}`;
};
PkgJsonEmbark.prototype.setVersion = function () {
if (isObject(this.json) && this.json.version) {
this.version = this.json.version;
} else {
this.version = '???';
}
};
PkgJsonEmbark.prototype.setup = function () {
PkgJson.prototype.setup.call(this);
this.setVersion();
this.setPkg();
this.setNodeRange();
return this;
};
// -- package.json :: local to DApp --------------------------------------------
function PkgJsonLocal(filepath) {
PkgJson.call(this, filepath);
}
setupProto(PkgJsonLocal, PkgJson);
PkgJsonLocal.prototype.loglevel = 'error';
PkgJsonLocal.prototype.logMissingFile = function () {
if (PkgJson.prototype.logMissingFile.call(this)) {
reportMissingFile_PkgJsonLocal(this.loglevel);
exitWithError();
}
};
PkgJsonLocal.prototype.logPkgErrors = function () {
if (PkgJson.prototype.logPkgErrors.call(this)) {
reportPkgErrors_PkgJsonLocal(this.dirname, this.loglevel);
exitWithError();
}
};
PkgJsonLocal.prototype.logUnparsable = function () {
if (PkgJson.prototype.logUnparsable.call(this)) {
reportUnparsable_PkgJsonLocal(this.loglevel);
exitWithError();
}
};
// -- package.json :: local to DApp, expected by local installed embark --------
function PkgJsonLocalExpected(filepath) {
PkgJsonLocal.call(this, filepath);
this.embarkDep = undefined;
}
setupProto(PkgJsonLocalExpected, PkgJsonLocal);
PkgJsonLocalExpected.prototype.log = function () {
PkgJsonLocal.prototype.log.call(this);
this.logMissingEmbarkDep();
};
PkgJsonLocalExpected.prototype.logMissingEmbarkDep = function () {
if (this.json && !this.embarkDep) {
reportMissingEmbarkDep(this.filepath, this.dirname, this.loglevel);
exitWithError();
}
};
PkgJsonLocalExpected.prototype.logMissingFile = function () {
// PkgJson.prototype NOT PkgJsonLocal.prototype
if (PkgJson.prototype.logMissingFile.call(this)) {
reportMissingFile_PkgJsonLocalExpected(this.dirname, this.loglevel);
exitWithError();
}
};
PkgJsonLocalExpected.prototype.setEmbarkDep = function () {
if (isObject(this.json)) {
if (this.json.dependencies && this.json.dependencies.embark) {
this.embarkDep = this.json.dependencies.embark;
} else if (this.json.devDependencies && this.json.devDependencies.embark) {
this.embarkDep = this.json.devDependencies.embark;
}
}
};
PkgJsonLocalExpected.prototype.setup = function () {
PkgJsonLocal.prototype.setup.call(this);
this.setEmbarkDep();
return this;
};
// -- loggers ------------------------------------------------------------------
var embarklog = npmlog;
embarklog.heading = 'embark';
var _logged = false;
function logged(which) {
var embarklog_which = embarklog[which];
return function () {
_logged = true;
embarklog_which.apply(embarklog, arguments);
};
}
embarklog.error = logged('error');
embarklog.info = logged('info');
embarklog.warn = logged('warn');
function blankLineMaybe(which) {
if (_logged) {
console[which]();
}
}
var isNpmRun = process.env.hasOwnProperty('npm_lifecycle_script');
function blankLineTrailingMaybe(which) {
if (isNpmRun) {
console[which]();
}
}
// -- processors ---------------------------------------------------------------
function checkPkg(pkgDir, scopes) {
var errors;
try {
var config = {packageDir: pkgDir};
if (scopes) {
config.scopeList = scopes;
}
var checked = checkDeps.sync(config);
if (checked.error.length) {
errors = checked.error;
}
} finally {
// eslint-disable-next-line no-unsafe-finally
return errors;
}
}
function parseJson(filepath) {
var parsed;
try {
parsed = require(filepath);
} finally {
// eslint-disable-next-line no-unsafe-finally
return parsed;
}
}
function parseRange(range) {
var parsed;
try {
parsed = semver.Range(range).range;
} finally {
// eslint-disable-next-line no-unsafe-finally
return parsed;
}
}
function realpath(filepath) {
var resolved;
try {
resolved = fs.realpathSync(filepath);
} finally {
// eslint-disable-next-line no-unsafe-finally
return resolved;
}
}
function thisEmbark() {
return (new EmbarkBin(path.join(__dirname, '../../bin/embark'))).handle();
}
function whenNoShim() {
var noShim = !!process.env.EMBARK_NO_SHIM;
if (noShim) EmbarkBin.prototype.exec();
return noShim;
}
// -- reporters ----------------------------------------------------------------
function reportMissingEmbarkDep(filepath, dirname, loglevel) {
blankLineMaybe(loglevel);
embarklog[loglevel]('file', filepath);
embarklog[loglevel](
'',
[
`Could not find embark specified in "dependencies" or "devDependencies" of local package.json file`,
`But embark was found in node_modules relative to that file:`,
`${dirname}/node_modules/embark/`
].join('\n')
);
}
function reportMissingFile(filepath, loglevel) {
try {
// force the exception
fs.realpathSync(filepath);
} catch (e) {
blankLineMaybe(loglevel);
embarklog[loglevel]('path', e.path);
embarklog[loglevel]('code', e.code);
embarklog[loglevel]('errno', e.errno);
embarklog[loglevel]('syscall', e.syscall);
embarklog[loglevel](e.code.toLowerCase(), e.message);
}
}
function reportMissingFile_DappJson(cmd, loglevel, kind, where) {
blankLineMaybe(loglevel);
embarklog[loglevel](
'',
`Could not locate your DApp's ${kind}.json file`
);
embarklog[loglevel](
'',
`Make sure a valid ${kind}.json file exists ${where} your DApp's top-level directory`
);
embarklog[loglevel](
'',
`Embark command '${cmd}' can only be used inside a valid DApp directory structure`
);
}
function reportMissingFile_EmbarkBin(binpath, kind, loglevel) {
reportMissingFile(binpath, loglevel);
console[loglevel]();
embarklog[loglevel](
'',
[
`Could not resolve ${kind} embark command path with require('fs').realpathSync`,
`Maybe a broken symbolic link?`
].join('\n')
);
}
function reportMissingFile_PkgJsonEmbark(kind, loglevel) {
console[loglevel]();
embarklog[loglevel](
'',
`Could not locate ${kind} embark's package.json file`
);
}
function reportMissingFile_PkgJsonLocal(loglevel) {
console[loglevel]();
embarklog[loglevel](
'',
[
`Could not resolve local package.json path with require('fs').realpathSync`,
`Maybe a broken symbolic link?`
].join('\n')
);
}
function reportMissingFile_PkgJsonLocalExpected(dirname, loglevel) {
console[loglevel]();
embarklog[loglevel](
'',
[
`Could not find expected local package.json relative to embark found in:`,
`${dirname}/node_modules/embark/`
].join('\n')
);
}
function reportMissingVersion(filepath, kind, loglevel) {
blankLineMaybe(loglevel);
embarklog[loglevel]('file', filepath);
embarklog[loglevel](
'',
`No version is specified in ${kind} embark's package.json file`
);
}
function reportPkgErrors(errors, filepath, loglevel) {
blankLineMaybe(loglevel);
embarklog[loglevel]('file', filepath);
embarklog[loglevel]('code', `EPKGCHK`);
embarklog[loglevel]('package', errors.join('\n'));
}
function reportPkgErrors_PkgJsonEmbark(dirname, kind, loglevel) {
console[loglevel]();
embarklog[loglevel](
'',
[
`Dependencies are missing relative to ${kind} embark's package.json in:`,
`${dirname}/`
].join('\n')
);
}
function reportPkgErrors_PkgJsonLocal(dirname, loglevel) {
console[loglevel]();
embarklog[loglevel](
'',
[
`Dependencies are missing relative to local package.json in:`,
`${dirname}/`
].join('\n')
);
}
function reportSwitching(binpathFrom, binpathTo, loglevel, pkgFrom, pkgTo) {
blankLineMaybe(loglevel);
embarklog[loglevel]('invoked', binpathFrom);
embarklog[loglevel]('located', binpathTo);
embarklog[loglevel](
'',
`Switching from ${pkgFrom} to ${pkgTo}`
);
}
function reportUnparsable(filepath, loglevel) {
try {
// force the exception
parseJsonWithErrors(stripBOM(fs.readFileSync(filepath)));
} catch (e) {
var basename = path.basename(filepath);
blankLineMaybe(loglevel);
embarklog[loglevel]('file', filepath);
embarklog[loglevel]('code', `EJSONPARSE`);
embarklog[loglevel]('JSON parse', `Failed to parse json`);
embarklog[loglevel]('JSON parse', e.message);
embarklog[loglevel]('JSON parse', `Failed to parse ${basename} data.`);
embarklog[loglevel]('JSON parse', `${basename} must be actual JSON, not just JavaScript.`);
}
}
function reportUnparsable_EmbarkJson(loglevel) {
console[loglevel]();
embarklog[loglevel]('', `Could not parse your DApp's embark.json file`);
}
function reportUnparsable_PkgJsonEmbark(kind, loglevel) {
console[loglevel]();
embarklog[loglevel](
`Could not parse ${kind} embark's package.json file`
);
}
function reportUnparsable_PkgJsonLocal(loglevel) {
embarklog[loglevel]('', `Could not parse a local package.json file`);
}
function reportUnsupportedNode(
bad, filepath, kind, loglevel, missing, rangeDefault, rangeSupplied, pkg,
procVer, range) {
blankLineMaybe(loglevel);
function report(qual, invalid) {
embarklog[loglevel]('file', filepath);
embarklog[loglevel](
'engine',
`package.json of ${kind} ${pkg} does not specify ${qual ? qual : ''}%j`,
{engines: {node: '[semver]'}}
);
if (invalid) {
embarklog[loglevel](
'engine',
`Specified: %j`, {engines: {node: rangeSupplied}}
);
}
embarklog[loglevel](
'engine',
`Defaulting to: %j`, {engines: {node: rangeDefault}}
);
console[loglevel]();
}
if (missing) { report(); }
if (bad) { report('a valid ', true); }
embarklog[loglevel]('notsup', `Unsupported runtime`);
embarklog[loglevel](
'notsup',
`${kind} ${pkg} is not compatible with your version of node`
);
embarklog[loglevel]('notsup', `Required:`, range);
embarklog[loglevel]('notsup', `Actual:`, procVer);
}
// -- selectors ----------------------------------------------------------------
function select(invoked, local) {
var embark;
if (local) {
embark = local;
} else {
embark = invoked;
}
return embark;
}
function selectLocal(containing, installed, invoked) {
var local;
if (containing.binrealpath &&
containing.binrealpath !== invoked.binrealpath &&
(!installed.binrealpath ||
subdir(
installed.pkgJsonLocalExpected.dirname,
containing.pkgDir
))) {
local = containing;
}
if (installed.binrealpath &&
installed.binrealpath !== invoked.binrealpath &&
(!containing.binrealpath ||
subdir(containing.pkgDir, installed.pkgDir))) {
local = installed;
}
if (local) { local.log(); }
return local;
}
// -- utils --------------------------------------------------------------------
function exitWithError(code) {
blankLineTrailingMaybe('error');
process.exit(code || 1);
}
function initCwd() {
var initCwd = process.env.INIT_CWD || process.cwd();
// allow for env override
initCwd = process.env.DAPP_PATH || initCwd;
return initCwd;
}
function isDappCmd(cmd) {
return [
undefined,
'-V',
'--version',
'-h',
'--help',
'new',
'demo',
'version',
'help'
].indexOf(cmd) === -1;
}
function isObject(val) {