climb-lookup
Version:
a lookup climbing recursively file like a require.
155 lines (132 loc) • 4.9 kB
HTML
<html>
<head>
<meta charset="utf-8">
<base data-ice="baseUrl" href="../../">
<title data-ice="title">src/index.js | API Document</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
<link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
<script src="script/prettify/prettify.js"></script>
<script src="script/manual.js"></script>
</head>
<body class="layout-container" data-ice="rootContainer">
<header>
<a href="./">Home</a>
<a href="identifiers.html">Reference</a>
<a href="source.html">Source</a>
<a data-ice="repoURL" href="https://github.com/59naga/climb-lookup.git" class="repo-url-github">Repository</a>
<div class="search-box">
<span>
<img src="./image/search.png">
<span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
</span>
<ul class="search-result"></ul>
</div>
</header>
<nav class="navigation" data-ice="nav"><div>
<ul>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-getPaths">getPaths</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-lookup">lookup</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-function">F</span><span data-ice="name"><span><a href="function/index.html#static-function-lookupSync">lookupSync</a></span></span></li>
</ul>
</div>
</nav>
<div class="content" data-ice="content"><h1 data-ice="title">src/index.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">/**
* @module climb-lookup
*/
import fs from 'fs';
import path from 'path';
import objectAssign from 'object-assign';
/**
* Get recursively climbing paths.
*
* @function getPaths
* @param {string} filePath - base path
* @return {array} paths - an abosolute file paths from filePath to root
*/
export function getPaths(filePath) {
const paths = [];
const fileName = path.basename(filePath);
let current = path.resolve(filePath);
while (current.split(path.sep).length > 2) {// unless `/${file}`
paths.push(current);
current = path.join(path.join(current, '..', '..'), fileName);
}
paths.push(current);
return paths;
}
/**
* lookup climbing recursively file like a `require`.
*
* @function lookup
* @param {string} file - lookup file name
* @param {object} [options] -
* @param {object} [options.cwd=process.cwd()] - begin path
* @param {object} [options.mode=null] - pass to fs.accessSync as 2nd argument
* @param {lookedupAbsolutePath} callback -
*/
export function lookup(file, ...params) {
const [options, callback] = params.length === 1 ? [{}, params[0]] : params;
const opts = objectAssign({
cwd: process.cwd(),
}, options);
/**
* Callback for looked up absolute file path.
*
* @callback lookedupAbsolutePath
* @param {error} error - no paths found
* @param {string} lookedupPath - a found absolute file path
*/
const paths = getPaths(path.resolve(opts.cwd, file));
paths.reduceRight((next, filePath) => () => {
fs.access(filePath, opts.mode, (error) => {
if (error) {
return next();
}
return callback(null, filePath);
});
}, () => {
callback(Error(`ENOENT: no such paths, access '${paths.join("', '")}'`));
})();
}
/**
* Synchronous version of lookup
*
* @function lookupSync
* @param {string} file - lookup file name
* @param {object} [options]
* @param {object} [options.cwd=process.cwd()] - begin path
* @param {object} [options.mode=null] - pass to fs.accessSync as 2nd argument
* @return {string} lookedupPath - a found absolute file path
* @throws if no such paths
*/
export function lookupSync(file, options = {}) {
const opts = objectAssign({
cwd: process.cwd(),
}, options);
const paths = getPaths(path.resolve(opts.cwd, file));
for (let i = 0; i < paths.length; i++) {
try {
fs.accessSync(paths[i], opts.mode);
return paths[i];
} catch (error) {
// ignore
}
}
throw new Error(`ENOENT: no such paths, access '${paths.join("', '")}'`);
}
</code></pre>
</div>
<footer class="footer">
Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.5)</span></a>
</footer>
<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>