async.js (3047B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var resolve = require( 'path' ).resolve; 24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 25 var isFunction = require( '@stdlib/assert/is-function' ); 26 var cwd = require( '@stdlib/process/cwd' ); 27 var exists = require( './../../exists' ); 28 var validate = require( './validate.js' ); 29 30 31 // MAIN // 32 33 /** 34 * Asynchronously resolves a path by walking parent directories. 35 * 36 * @param {string} path - path to resolve 37 * @param {Options} [options] - function options 38 * @param {string} [options.dir] - base directory 39 * @param {Callback} clbk - callback to invoke after resolving a path 40 * @throws {TypeError} first argument must be a string 41 * @throws {TypeError} callback argument must be a function 42 * @throws {TypeError} options argument must be an object 43 * @throws {TypeError} must provide valid options 44 * 45 * @example 46 * resolveParentPath( 'package.json', onPath ); 47 * 48 * function onPath( error, path ) { 49 * if ( error ) { 50 * throw error; 51 * } 52 * console.log( path ); 53 * } 54 */ 55 function resolveParentPath( path, options, clbk ) { 56 var spath; 57 var child; 58 var opts; 59 var done; 60 var dir; 61 var err; 62 if ( !isString( path ) ) { 63 throw new TypeError( 'invalid argument. First argument must be a string primitive. Value: `' + path + '`.' ); 64 } 65 opts = {}; 66 if ( arguments.length > 2 ) { 67 done = clbk; 68 err = validate( opts, options ); 69 if ( err ) { 70 throw err; 71 } 72 } else { 73 done = options; 74 } 75 if ( !isFunction( done ) ) { 76 throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + done + '`.' ); 77 } 78 if ( opts.dir ) { 79 dir = resolve( cwd(), opts.dir ); 80 } else { 81 dir = cwd(); 82 } 83 spath = resolve( dir, path ); 84 exists( spath, onExists ); 85 86 /** 87 * Callback invoked after checking for path existence. 88 * 89 * @private 90 * @param {(Error|null)} error - error object 91 * @param {boolean} bool - boolean indicating if a path exists 92 * @returns {void} 93 */ 94 function onExists( error, bool ) { // eslint-disable-line handle-callback-err 95 if ( bool ) { 96 return done( null, spath ); 97 } 98 // Resolve a parent directory: 99 child = dir; 100 dir = resolve( dir, '..' ); 101 102 // If we have already reached root, we cannot resolve any higher directories... 103 if ( child === dir ) { 104 return done( null, null ); 105 } 106 // Resolve the next search path: 107 spath = resolve( dir, path ); 108 exists( spath, onExists ); 109 } 110 } 111 112 113 // EXPORTS // 114 115 module.exports = resolveParentPath;