Skip to content

Commit

Permalink
refactor: extract resolveSilent
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Feb 22, 2024
1 parent 77284ea commit 84f7990
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lib/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import picocolors from 'picocolors';
const { dim, red } = picocolors;

import meow from 'meow';
import resolveFrom from 'resolve-from';

import { isBoolean, isNumber, isObject, isPlainObject, isString } from './utils/validateTypes.mjs';
import checkInvalidCLIOptions from './utils/checkInvalidCLIOptions.mjs';
import dynamicImport from './utils/dynamicImport.mjs';
import printConfig from './printConfig.mjs';
import resolveSilent from './utils/resolveSilent.mjs';
import standalone from './standalone.mjs';
import writeOutputFile from './writeOutputFile.mjs';

Expand Down Expand Up @@ -400,7 +400,7 @@ export default async function main(argv) {
// c. relative path relative to `process.cwd()`.
// If none of the above work, we'll try a relative path starting
// in `process.cwd()`.
options.configFile = resolveFrom.silent(cwd, configFile) || join(cwd, configFile);
options.configFile = resolveSilent(cwd, configFile) || join(cwd, configFile);
}

if (isString(configBasedir)) {
Expand Down
38 changes: 8 additions & 30 deletions lib/utils/getModulePath.mjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import path from 'node:path';
import process from 'node:process';

import { fileURLToPath, pathToFileURL } from 'node:url';

import globalModules from 'global-modules';
import { moduleResolve } from 'import-meta-resolve';

import configurationError from './configurationError.mjs';

const suffixes = ['', '.js', '.json', '/index.js', '/index.json'];

/**
* @param {string} parent
* @param {string} lookup
* @return {string | undefined}
*/
const resolveSilent = (parent, lookup) => {
const resolvedParent = pathToFileURL(path.resolve(parent, 'noop.js'));

for (const suffix of suffixes) {
try {
return fileURLToPath(moduleResolve(lookup + suffix, resolvedParent));
} catch {
//
}
}
};
import resolveSilent from './resolveSilent.mjs';

/**
* @param {string} basedir
Expand All @@ -37,21 +15,21 @@ export default function getModulePath(basedir, lookup, cwd = process.cwd()) {
// 1. Try to resolve from the provided directory
// 2. Try to resolve from `cwd` or `process.cwd()`
// 3. Try to resolve from global `node_modules` directory
let modulePath = resolveSilent(basedir, lookup);
let path = resolveSilent(basedir, lookup);

if (!modulePath) {
modulePath = resolveSilent(cwd, lookup);
if (!path) {
path = resolveSilent(cwd, lookup);
}

if (!modulePath) {
modulePath = resolveSilent(globalModules, lookup);
if (!path) {
path = resolveSilent(globalModules, lookup);
}

if (!modulePath) {
if (!path) {
throw configurationError(
`Could not find "${lookup}". Do you need to install the package or use the "configBasedir" option?`,
);
}

return modulePath;
return path;
}
39 changes: 39 additions & 0 deletions lib/utils/resolveSilent.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'node:fs';
import path from 'node:path';

import { fileURLToPath, pathToFileURL } from 'node:url';

import { moduleResolve } from 'import-meta-resolve';

const pathSuffixes = ['', '.js', '.json', `${path.sep}index.js`, `${path.sep}index.json`];

const specifierSuffixes = ['', '.js', '.json', '/index.js', '/index.json'];

/**
* @param {string} parent
* @param {string} lookup
* @return {string | undefined}
*/
export default function resolveSilent(parent, lookup) {
if (path.isAbsolute(lookup)) {
for (const suffix of pathSuffixes) {
const filename = lookup + suffix;

if (fs.existsSync(filename)) {
return filename;
}
}

return;
}

const resolvedParent = pathToFileURL(path.resolve(parent, 'noop.js'));

for (const suffix of specifierSuffixes) {
try {
return fileURLToPath(moduleResolve(lookup + suffix, resolvedParent));
} catch {
//
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
],
"scripts": {
"benchmark-rule": "node scripts/benchmark-rule.mjs",
"build": "rollup -c && git checkout HEAD -- lib/utils/getModulePath.cjs",
"build": "rollup -c && git checkout HEAD -- lib/utils/getModulePath.cjs && rm lib/utils/resolveSilent.cjs",
"build-check": "node scripts/build-check.mjs",
"format": "prettier . --write --cache",
"lint": "npm-run-all --parallel --continue-on-error lint:*",
Expand Down

0 comments on commit 84f7990

Please sign in to comment.