Skip to content

Commit

Permalink
refactor: Rely on require.resolve to detect wether module exist
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 27, 2022
1 parent d18efc2 commit 040be5f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
43 changes: 27 additions & 16 deletions lib/classes/PluginManager.js
Expand Up @@ -3,7 +3,6 @@
const path = require('path');
const config = require('@serverless/utils/config');
const _ = require('lodash');
const isModuleNotFoundError = require('ncjsm/is-module-not-found-error');
const ServerlessError = require('../serverless-error');
const resolveCliInput = require('../cli/resolve-input');
const getCommandSuggestion = require('../utils/getCommandSuggestion');
Expand Down Expand Up @@ -145,30 +144,42 @@ class PluginManager {
return this.asyncPluginInit();
}

requireServicePlugin(serviceDir, pluginPath, localPluginsPath) {
if (localPluginsPath && !pluginPath.startsWith('./')) {
requireServicePlugin(serviceDir, pluginPath, legacyLocalPluginsPath) {
if (legacyLocalPluginsPath && !pluginPath.startsWith('./')) {
// TODO (BREAKING): Consider removing support for localPluginsPath with next major
const absoluteLocalPluginPath = path.resolve(localPluginsPath, pluginPath);

const absolutePluginPath = path.resolve(legacyLocalPluginsPath, pluginPath);

const isLocatedAtLegacyPluginsPath = (() => {
try {
require.resolve(absolutePluginPath);
return true;
} catch {
return false;
}
})();
if (isLocatedAtLegacyPluginsPath) return require(absolutePluginPath);
}
const serviceDirRequire = getRequire(serviceDir);
const entryFilePath = (() => {
try {
return require(absoluteLocalPluginPath);
} catch (error) {
if (!isModuleNotFoundError(error, absoluteLocalPluginPath)) throw error;
return serviceDirRequire.resolve(pluginPath);
} catch {
return null;
}
}
try {
const entryFilePath = getRequire(serviceDir).resolve(pluginPath);
})();
if (entryFilePath) {
if (pluginPath.startsWith('./')) {
this.localPluginsPaths.push({ resolvedPath: entryFilePath, inputPath: pluginPath });
}
return require(entryFilePath);
} catch (error) {
if (!isModuleNotFoundError(error, pluginPath) || pluginPath.startsWith('.')) {
throw error;
}
}

// Search in "node_modules" in which framework is placed
try {
require.resolve(pluginPath);
} catch {
throw Object.assign(new Error('Plugin not found'), { code: 'PLUGIN_NOT_FOUND' });
}
return require(pluginPath);
}

Expand All @@ -185,7 +196,7 @@ class PluginManager {
try {
Plugin = this.requireServicePlugin(serviceDir, name, pluginsObject.localPath);
} catch (error) {
if (!isModuleNotFoundError(error, name)) throw error;
if (error.code !== 'PLUGIN_NOT_FOUND') throw error;

// Plugin not installed
if (
Expand Down
15 changes: 8 additions & 7 deletions lib/configuration/read.js
Expand Up @@ -5,7 +5,6 @@ const isPlainObject = require('type/plain-object/is');
const path = require('path');
const fsp = require('fs').promises;
const yaml = require('js-yaml');
const isModuleNotFoundError = require('ncjsm/is-module-not-found-error');
const getRequire = require('../utils/get-require');
const spawn = require('child-process-ext/spawn');
const cloudformationSchema = require('@serverless/utils/cloudformation-schema');
Expand Down Expand Up @@ -137,15 +136,17 @@ const parseConfigurationFile = async (configurationPath) => {
// fallthrough
case '.js': {
const configurationEventuallyDeferred = (() => {
try {
require.resolve(configurationPath);
} catch {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": File not found`,
'CONFIGURATION_NOT_FOUND'
);
}
try {
return require(configurationPath);
} catch (error) {
if (isModuleNotFoundError(error, configurationPath)) {
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": File not found`,
'CONFIGURATION_NOT_FOUND'
);
}
throw new ServerlessError(
`Cannot load "${path.basename(configurationPath)}": Initialization error: ${
error && error.stack ? error.stack : error
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -57,7 +57,6 @@
"lodash": "^4.17.21",
"memoizee": "^0.4.15",
"micromatch": "^4.0.4",
"ncjsm": "^4.3.0",
"node-fetch": "^2.6.7",
"object-hash": "^2.2.0",
"open": "^7.4.2",
Expand Down Expand Up @@ -95,6 +94,7 @@
"log-node": "^8.0.3",
"mocha": "^8.4.0",
"mock-require": "^3.0.3",
"ncjsm": "^4.3.0",
"nyc": "^15.1.0",
"pkg": "^5.5.2",
"prettier": "^2.5.1",
Expand Down

0 comments on commit 040be5f

Please sign in to comment.