Skip to content

Commit a02f3ff

Browse files
committed
fix(shell): use local webda when available
1 parent 6acc1d5 commit a02f3ff

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

packages/shell/bin/webda

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#!/usr/bin/env node
22
"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");
78

89
/**
910
* Get Version of a package (version attribute of package.json)
1011
*
1112
* @param packageName to retrieve version from
1213
*/
1314
function getVersion(packageName) {
14-
return require(packageName + '/package.json').version;
15+
return require(packageName + "/package.json").version;
1516
}
1617

1718
/**
@@ -22,36 +23,55 @@ function getVersion(packageName) {
2223
* - local folder
2324
* - embedded library
2425
*/
26+
let webdaVersion;
2527
if (fs.existsSync("node_modules/webda")) {
2628
// 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);
2934
} else if (fs.existsSync("core.js") && fs.existsSync("services/executor.js")) {
3035
console.log("Using local web" + "da".yellow + "development version");
3136
// We are in a webda development directory
3237
global.__webda = process.cwd();
38+
webdaVersion = "dev";
3339
} else {
3440
// 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);
3746
}
47+
global.__webda = require(global.__webda);
3848

3949
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+
}
4159

4260
/**
4361
* Add a Promise.each ( should add it to core.js )
4462
*/
4563
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
4766
// 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"));
4969
// empty case
50-
if(arr.length === 0) return Promise.resolve();
70+
if (arr.length === 0) return Promise.resolve();
5171
return arr.reduce(function(prev, cur) {
52-
return prev.then(() => fn(cur))
72+
return prev.then(() => fn(cur));
5373
}, Promise.resolve());
54-
}
74+
};
5575
}
5676

5777
/**
@@ -61,37 +81,48 @@ if (!Promise.each) {
6181
// Find first shell override
6282
var files = [];
6383

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");
6888
}
6989
}
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+
70102
var consoleService;
71103
if (files.length) {
72104
let cfg = require(files[0]);
73105
let dir = path.dirname(files[0]);
74-
console.log(cfg.name + ' - v' + getVersion(dir).trim());
106+
console.log(cfg.name + " - v" + getVersion(dir).trim());
75107
consoleService = require(path.join(dir, cfg.handler));
76108
if (consoleService.default) {
77109
consoleService = consoleService.default;
78110
}
79111
} else {
80-
consoleService = require(global.__webda_shell + '/lib/console/webda').default;
112+
consoleService = require(global.__webda_shell + "/lib/console/webda").default;
81113
}
82114

83115
/**
84116
* Commandline parsing
85117
*/
86118
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(() => {
89121
process.exit(0);
90-
}).catch( (err) => {
122+
}).catch(err => {
91123
console.log(err);
92124
process.exit(1);
93125
});
94126
} else {
95127
process.exit(0);
96128
}
97-

packages/shell/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
"mkdirp": "^0.5.1",
5353
"multer": ">=1.1.0",
5454
"nodejs-websocket": "^1.5.0",
55-
"open": ">=6.0.0",
55+
"open": "^6.0.0",
5656
"request": "^2.81.0",
5757
"request-promise": "^4.2.0",
58-
"semver": "^5.5.1",
58+
"semver": "^6.2.0",
5959
"socket.io": "^2.0.3",
6060
"unzip": "^0.1.11",
6161
"yamljs": "^0.3.0",

0 commit comments

Comments
 (0)