1
1
#!/usr/bin/env node
2
2
"use strict" ;
3
- var fs = require ( 'fs' ) ;
4
- const colors = require ( 'colors' ) ; // Used for console output: DO NOT REMOVE
5
- const path = require ( 'path' ) ;
6
-
3
+ var fs = require ( "fs" ) ;
4
+ const colors = require ( "colors" ) ; // Used for console output: DO NOT REMOVE
5
+ const path = require ( "path" ) ;
6
+ var Module = require ( "module" ) ;
7
+ const semver = require ( "semver" ) ;
7
8
8
9
/**
9
10
* Get Version of a package (version attribute of package.json)
10
11
*
11
12
* @param packageName to retrieve version from
12
13
*/
13
14
function getVersion ( packageName ) {
14
- return require ( packageName + ' /package.json' ) . version ;
15
+ return require ( packageName + " /package.json" ) . version ;
15
16
}
16
17
17
18
/**
@@ -22,36 +23,55 @@ function getVersion(packageName) {
22
23
* - local folder
23
24
* - embedded library
24
25
*/
26
+ let webdaVersion ;
25
27
if ( fs . existsSync ( "node_modules/webda" ) ) {
26
28
// Local module of webda exists use it
27
- global . __webda = process . cwd ( ) + '/node_modules/webda' ;
28
- console . log ( "Using local web" + "da" . yellow + " - v" + getVersion ( global . __webda ) ) ;
29
+ global . __webda = process . cwd ( ) + "/node_modules/webda" ;
30
+ console . log (
31
+ "Using local web" + "da" . yellow + " - v" + getVersion ( global . __webda )
32
+ ) ;
33
+ webdaVersion = getVersion ( global . __webda ) ;
29
34
} else if ( fs . existsSync ( "core.js" ) && fs . existsSync ( "services/executor.js" ) ) {
30
35
console . log ( "Using local web" + "da" . yellow + "development version" ) ;
31
36
// We are in a webda development directory
32
37
global . __webda = process . cwd ( ) ;
38
+ webdaVersion = "dev" ;
33
39
} else {
34
40
// Use the webda-shell default webda
35
- global . __webda = 'webda' ;
36
- console . log ( "Using embedded web" + "da" . yellow + " - v" + getVersion ( global . __webda ) ) ;
41
+ global . __webda = "webda" ;
42
+ console . log (
43
+ "Using embedded web" + "da" . yellow + " - v" + getVersion ( global . __webda )
44
+ ) ;
45
+ webdaVersion = getVersion ( global . __webda ) ;
37
46
}
47
+ global . __webda = require ( global . __webda ) ;
38
48
39
49
global . __webda_shell = __dirname + "/.." ;
40
- console . log ( 'web' + "da" . yellow + "-shell - v" + getVersion ( __webda_shell ) ) ;
50
+ console . log ( "web" + "da" . yellow + "-shell - v" + getVersion ( __webda_shell ) ) ;
51
+
52
+ // Display warning for versions mismatch
53
+ if ( ! semver . satisfies ( webdaVersion , "^" + getVersion ( __webda_shell ) ) ) {
54
+ console . log (
55
+ "Versions mismatch: @webda/core and @webda/shell are not within patch versions"
56
+ . yellow
57
+ ) ;
58
+ }
41
59
42
60
/**
43
61
* Add a Promise.each ( should add it to core.js )
44
62
*/
45
63
if ( ! Promise . each ) {
46
- Promise . each = function ( arr , fn ) { // take an array and a function
64
+ Promise . each = function ( arr , fn ) {
65
+ // take an array and a function
47
66
// invalid input
48
- if ( ! Array . isArray ( arr ) ) return Promise . reject ( new Error ( "Non array passed to each" ) ) ;
67
+ if ( ! Array . isArray ( arr ) )
68
+ return Promise . reject ( new Error ( "Non array passed to each" ) ) ;
49
69
// empty case
50
- if ( arr . length === 0 ) return Promise . resolve ( ) ;
70
+ if ( arr . length === 0 ) return Promise . resolve ( ) ;
51
71
return arr . reduce ( function ( prev , cur ) {
52
- return prev . then ( ( ) => fn ( cur ) )
72
+ return prev . then ( ( ) => fn ( cur ) ) ;
53
73
} , Promise . resolve ( ) ) ;
54
- }
74
+ } ;
55
75
}
56
76
57
77
/**
@@ -61,37 +81,48 @@ if (!Promise.each) {
61
81
// Find first shell override
62
82
var files = [ ] ;
63
83
64
- if ( process . argv . indexOf ( ' --no-override' ) < 0 ) {
65
- const Finder = require ( ' fs-finder' ) ;
66
- if ( fs . existsSync ( ' ./node_modules' ) ) {
67
- files = Finder . from ( ' ./node_modules' ) . findFiles ( ' webda.shell.json' ) ;
84
+ if ( process . argv . indexOf ( " --no-override" ) < 0 ) {
85
+ const Finder = require ( " fs-finder" ) ;
86
+ if ( fs . existsSync ( " ./node_modules" ) ) {
87
+ files = Finder . from ( " ./node_modules" ) . findFiles ( " webda.shell.json" ) ;
68
88
}
69
89
}
90
+
91
+ // Capture require() to use local webda if exists
92
+ function intercept ( __require ) {
93
+ return function ( moduleId ) {
94
+ if ( moduleId === "webda" ) {
95
+ return global . __webda ;
96
+ }
97
+ return __require . apply ( this , arguments ) ;
98
+ } ;
99
+ }
100
+ Module . prototype . require = intercept ( Module . prototype . require ) ;
101
+
70
102
var consoleService ;
71
103
if ( files . length ) {
72
104
let cfg = require ( files [ 0 ] ) ;
73
105
let dir = path . dirname ( files [ 0 ] ) ;
74
- console . log ( cfg . name + ' - v' + getVersion ( dir ) . trim ( ) ) ;
106
+ console . log ( cfg . name + " - v" + getVersion ( dir ) . trim ( ) ) ;
75
107
consoleService = require ( path . join ( dir , cfg . handler ) ) ;
76
108
if ( consoleService . default ) {
77
109
consoleService = consoleService . default ;
78
110
}
79
111
} else {
80
- consoleService = require ( global . __webda_shell + ' /lib/console/webda' ) . default ;
112
+ consoleService = require ( global . __webda_shell + " /lib/console/webda" ) . default ;
81
113
}
82
114
83
115
/**
84
116
* Commandline parsing
85
117
*/
86
118
let p = consoleService . handleCommand ( process . argv . slice ( 2 ) ) ;
87
- if ( p instanceof Promise || p && p . then ) {
88
- p . then ( ( ) => {
119
+ if ( p instanceof Promise || ( p && p . then ) ) {
120
+ p . then ( ( ) => {
89
121
process . exit ( 0 ) ;
90
- } ) . catch ( ( err ) => {
122
+ } ) . catch ( err => {
91
123
console . log ( err ) ;
92
124
process . exit ( 1 ) ;
93
125
} ) ;
94
126
} else {
95
127
process . exit ( 0 ) ;
96
128
}
97
-
0 commit comments