Skip to content

Commit

Permalink
Fix issue #30 in resolving local modules with relative paths and work…
Browse files Browse the repository at this point in the history
…around for #28 (#31)
  • Loading branch information
deepal authored Mar 15, 2020
1 parent cd55205 commit 764b4ea
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 19 deletions.
61 changes: 57 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
},
"author": "Deepal Jayasekara <dpjayasekara@gmail.com>",
"license": "ISC",
"engines": {
"node": "<=12.15.0"
},
"dependencies": {
"colors": "1.4.0",
"mkdirp": "0.5.1",
"rimraf": "3.0.0"
"rimraf": "3.0.0",
"semver": "7.1.3"
},
"repository": {
"type": "git",
Expand Down
27 changes: 19 additions & 8 deletions src/baapan.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Module } from 'module';
import path from 'path';
import 'colors';
import repl from 'repl';
import compatibility from './versionCheck';

export default class BaapanREPLServer {
constructor(options = {}) {
Expand Down Expand Up @@ -48,8 +49,14 @@ export default class BaapanREPLServer {

installModule(moduleName) {
console.info(`Fetching and installing module '${moduleName}' from npm...`.grey.italic);
execSync(`npm install --silent ${moduleName}`, { cwd: this.options.workspacePath });
console.info('Done!'.grey.italic);
try {
execSync(`npm install --silent ${moduleName}`, { cwd: this.options.workspacePath });
console.info('Done!'.grey.italic);
} catch (err) {
const e = new Error(`Could not install module! ${err.message || 'Unknown Error'}`.red);
e.stack = null;
throw e;
}
}

/**
Expand All @@ -70,6 +77,7 @@ export default class BaapanREPLServer {
execSync('npm init -y --scope baapan', { cwd: this.options.workspacePath });
}
}
mkdirp.sync(path.join(this.options.workspacePath, 'node_modules'));
}

initializeReplHistory() {
Expand Down Expand Up @@ -113,18 +121,21 @@ export default class BaapanREPLServer {
const workspaceModulesDir = path.join(this.options.workspacePath, 'node_modules');
const originalRequire = Module.prototype.require;
Module.prototype.require = function (moduleName) {
// Inject workspace node_modules directory
this.paths.unshift(workspaceModulesDir);
// This is required in node versions >= 12.3.0
if (!this.paths.includes(process.cwd())) this.paths.unshift(process.cwd());
// The following behaviour is broken in latest node versions.
// Skip it if the node version is not supported
if (compatibility.isSupportedNodeVersion() && !this.paths.includes(workspaceModulesDir)) {
this.paths.unshift(workspaceModulesDir);
}
try {
return originalRequire.apply(this, [moduleName]);
return originalRequire.call(this, moduleName);
} catch (err) {
const moduleInfo = BaapanREPLServer.getModuleInfo(moduleName);
if (!moduleInfo.isThirdPartyModule) throw err;

self.installModule(moduleInfo.path);
return originalRequire.apply(this, [
path.join(self.options.workspacePath, 'node_modules', moduleName),
]);
return originalRequire.call(this, path.join(self.options.workspacePath, 'node_modules', moduleName));
}
};
}
Expand Down
16 changes: 10 additions & 6 deletions src/entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ const mergedNodeOpts = (
.filter((opt) => !!opt)
);

require('child_process').spawn(nodePath, [...mergedNodeOpts, libPath], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit',
windowsHide: true,
});
if (mergedNodeOpts.length) {
require('child_process').spawn(nodePath, [...mergedNodeOpts, libPath], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit',
windowsHide: true,
});
} else {
require(libPath);
}
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#!/usr/bin/env node
import os from 'os';
import path from 'path';
import compatibility from './versionCheck';
import BaapanREPLServer from './baapan';
import 'colors';

// Temporary warning for https://github.com/deepal/baapan/issues/28
if (!compatibility.isSupportedNodeVersion()) {
console.error(
`Your current Node version (${compatibility.currentNodeVersion}) is not fully supported yet, but we are working on it. Currently we only support Node ${compatibility.requiredNodeVersion}`.yellow,
);
}

let workspacePath = process.env.BAAPAN_WS_PATH;
let replHistoryPath = null;
let persistWorkspace = true;
Expand Down
15 changes: 15 additions & 0 deletions src/versionCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import semver from 'semver';
import pkg from '../package.json';

const { engines } = pkg;

export default {
currentNodeVersion: process.version,
requiredNodeVersion: (engines && engines.node) || 'none',
isSupportedNodeVersion() {
if (engines && engines.node) {
return semver.satisfies(process.version, engines.node);
}
return true;
},
};

0 comments on commit 764b4ea

Please sign in to comment.