Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

enhanced-resolve-cli

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enhanced-resolve-cli - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

104

index.js
#!/usr/bin/env node
const VERSION = require('./package.json').version;
/**
* @typedef ProgramOptions
* @property {object} options
* @property {string} options.basepath=process.cwd()
* @property {boolean} options.debug=false
* @property {boolean} options.suppress=false
* @property {string} [options.webpackConfig]
*/
const package = require('./package.json');
const [ VERSION, DESCRIPTION ] = [ package.version, package.description ];
const fs = require('fs');

@@ -12,10 +22,10 @@ const path = require('path');

const WEBPACK_PATH = path.join('node_modules', 'webpack', 'lib', 'webpack.js');
const WEBPACK_CONFIGFILE = 'webpack.config.js'
const WEBPACK_CONFIG_FILENAME = 'webpack.config.js'
/**
* @return {string[]} array of paths from startdir upwards
* @return {string[]} array of paths from startDir upwards
*/
const getTree = (startdir) => {
let current = startdir;
const root = path.parse(startdir).root;
const getTree = (startDir) => {
let current = startDir;
const root = path.parse(startDir).root;
const paths = [];

@@ -32,4 +42,4 @@ while (current !== root) {

*/
const findLocal = (startdir = process.cwd(), needle) => {
const pathTree = getTree(startdir);
const findLocal = (startDir = process.cwd(), needle) => {
const pathTree = getTree(startDir);
const result = pathTree.findIndex((p) => fs.existsSync(path.join(p, needle)));

@@ -40,7 +50,9 @@ if (result === -1) { return; }

/**
* @param {string} request file
* @param {ProgramOptions} options
*/
const formatter = (request, options) => (err, filepath) => {
if (err) {
if (!options.suppress) {
console.error(`Could not resolve ${request} from ${options.basepath}`);
}
if (!options.suppress) { console.error(`[ERROR] Could not resolve ${request} from ${options.basepath}`); }
process.exit(1);

@@ -51,15 +63,26 @@ }

const getWebpackOptions = (webpackConfigPath, webpackPath) => {
/**
* @param {string} webpackConfigFile full path to a webpack.config.js
* @param {string} webpackModule full path to node_modules/webpack
* @return {?object} webpack options object
*/
const getWebpackOptions = (webpackConfigFile, webpackModule) => {
let webpack;
let webpackCompiler;
let webpackConfig;
try {
webpackConfig = require(require.resolve(webpackConfigPath));
webpackConfig = typeof webpackConfig === 'function' ? webpackConfig() : webpackConfig;
webpackConfig = typeof webpackConfig === 'function'
? webpackConfig()
: webpackConfig;
webpack = require(require.resolve(webpackPath));
webpack = require(require.resolve(webpackModule));
webpackCompiler = webpack(webpackConfig);
webpackCompiler = webpackCompiler.compilers ? webpackCompiler.compilers[0] : webpackCompiler;
// @TODO support options.multiConfigIndex
webpackCompiler = webpackCompiler.compilers
? webpackCompiler.compilers[0]
: webpackCompiler;
return webpackCompiler.options.resolve;

@@ -70,7 +93,22 @@ } catch (e) { /**/ }

const getResolverOptions = (request, options = {}) => {
const localWebpackConfig = findLocal(options.basepath, WEBPACK_CONFIGFILE);
const localWebpackPath = findLocal(options.basepath, WEBPACK_PATH);
const resolverOptions = localWebpackConfig && localWebpackPath
? getWebpackOptions(localWebpackConfig, localWebpackPath) || {}
/**
* @param {string} request file
* @param {ProgramOptions} options
* @return {object} webpack resolver options for use in enhanced-resolve
*/
const getResolverOptions = (request, options) => {
const webpackConfigFile = options.webpackConfig
|| findLocal(options.basepath, WEBPACK_CONFIG_FILENAME);
if (options.debug) { console.info('[DEBUG] Webpack config file: ', webpackConfigFile); }
const webpackModule = findLocal(options.basepath, WEBPACK_PATH);
if (options.debug) { console.info('[DEBUG] Webpack module: ', webpackModule); }
if (webpackConfigFile && !webpackModule) {
if( !options.suppress) { console.error('[ERROR] Found a webpack config file but could not find local webpack module'); }
process.exit(1);
}
const resolverOptions = webpackConfigFile && webpackModule
? getWebpackOptions(webpackConfigFile, webpackModule) || {}
: {};

@@ -81,7 +119,13 @@

const outputResult = (request, options = {}) => {
/**
* @param {string} request file
* @param {ProgramOptions} options
*/
const outputResult = (request, options) => {
const startDir = path.resolve(options.basepath);
if (options.debug) { console.info('[DEBUG] Basepath: ', startDir); }
const resolverOptions = getResolverOptions(request, options);
const resolver = makeResolver(resolverOptions);
//console.log({}, options.basepath, request, formatter(request, options));
resolver({}, options.basepath, request, formatter(request, options));
resolver({}, startDir, request, formatter(request, options));
};

@@ -91,7 +135,11 @@

.version(VERSION)
.description(DESCRIPTION)
.arguments('<request>', 'Thing to resolve using enhanced-resolve')
.option('-s, --suppress', 'Suppress error output')
.option('-b, --basepath <path>', `Path to resolve from <${process.cwd()}>`, process.cwd())
.action(outputResult);
.option('-b, --basepath <basepath>', `Path to resolve from <${process.cwd()}>`, process.cwd())
.option('-d, --debug', 'Output debugging info', false)
.option('-s, --suppress', 'Suppress error output', false)
.option('-w, --webpackConfig <webpackConfig>', `Path to a webpack.config.js file`)
.action(outputResult)
.parse(process.argv);
program.parse(process.argv);
if (!program.args.length) { program.help(); }
{
"name": "enhanced-resolve-cli",
"version": "0.1.0",
"version": "0.2.0",
"description": "Provides `enhancedresolve` CLI tool to output a resolved path to a requested import/module with support for Node.js and webpack resolve options.",

@@ -5,0 +5,0 @@ "main": "index.js",

# enhanced-resolve-cli
<https://github.com/davidosomething/enhanced-resolve-cli>
Get a resolved module path. Reads resolve settings from your
`webpack.config.js` if found.
`webpack.config.js` if available, otherwise falls back to node resolution.

@@ -26,2 +28,8 @@ ## Installation

## Vim plugin
[vim-enhanced-resolver](https://github.com/davidosomething/vim-enhanced-resolver)
is a vim plugin using this cli tool. It provides a mappable `<Plug>` to jump
to the resolved file.
## Examples

@@ -50,1 +58,5 @@

## License
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc