feature: tests

This commit is contained in:
Joel Male
2022-11-10 23:06:47 +10:00
parent ee20d8c79e
commit 80541e6fb8
52 changed files with 6109 additions and 832 deletions
+15 -15
View File
@@ -3,9 +3,9 @@ const path = require('path');
const Module = require('module');
const fs = require('fs');
const resolveFrom = (fromDir, moduleId, silent) => {
if (typeof fromDir !== 'string') {
throw new TypeError(`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDir}\``);
const resolveFrom = (fromDirectory, moduleId, silent) => {
if (typeof fromDirectory !== 'string') {
throw new TypeError(`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDirectory}\``);
}
if (typeof moduleId !== 'string') {
@@ -13,35 +13,35 @@ const resolveFrom = (fromDir, moduleId, silent) => {
}
try {
fromDir = fs.realpathSync(fromDir);
} catch (err) {
if (err.code === 'ENOENT') {
fromDir = path.resolve(fromDir);
fromDirectory = fs.realpathSync(fromDirectory);
} catch (error) {
if (error.code === 'ENOENT') {
fromDirectory = path.resolve(fromDirectory);
} else if (silent) {
return null;
return;
} else {
throw err;
throw error;
}
}
const fromFile = path.join(fromDir, 'noop.js');
const fromFile = path.join(fromDirectory, 'noop.js');
const resolveFileName = () => Module._resolveFilename(moduleId, {
id: fromFile,
filename: fromFile,
paths: Module._nodeModulePaths(fromDir)
paths: Module._nodeModulePaths(fromDirectory)
});
if (silent) {
try {
return resolveFileName();
} catch (err) {
return null;
} catch (error) {
return;
}
}
return resolveFileName();
};
module.exports = (fromDir, moduleId) => resolveFrom(fromDir, moduleId);
module.exports.silent = (fromDir, moduleId) => resolveFrom(fromDir, moduleId, true);
module.exports = (fromDirectory, moduleId) => resolveFrom(fromDirectory, moduleId);
module.exports.silent = (fromDirectory, moduleId) => resolveFrom(fromDirectory, moduleId, true);
+8 -6
View File
@@ -1,6 +1,6 @@
{
"name": "resolve-from",
"version": "4.0.0",
"version": "5.0.0",
"description": "Resolve the path of a module like `require.resolve()` but from a given path",
"license": "MIT",
"repository": "sindresorhus/resolve-from",
@@ -10,13 +10,14 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"require",
@@ -28,7 +29,8 @@
"import"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
+5 -5
View File
@@ -24,15 +24,15 @@ resolveFrom('foo', './bar');
## API
### resolveFrom(fromDir, moduleId)
### resolveFrom(fromDirectory, moduleId)
Like `require()`, throws when the module can't be found.
### resolveFrom.silent(fromDir, moduleId)
### resolveFrom.silent(fromDirectory, moduleId)
Returns `null` instead of throwing when the module can't be found.
Returns `undefined` instead of throwing when the module can't be found.
#### fromDir
#### fromDirectory
Type: `string`
@@ -47,7 +47,7 @@ What you would use in `require()`.
## Tip
Create a partial using a bound function if you want to resolve from the same `fromDir` multiple times:
Create a partial using a bound function if you want to resolve from the same `fromDirectory` multiple times:
```js
const resolveFromFoo = resolveFrom.bind(null, 'foo');