UNPKG

ninjs-lodash

Version:
32 lines (25 loc) 990 B
'use strict' const _ = require('lodash') const path = require('path') const fs = require('fs') module.exports = pkgup _.mixin({ pkgup: pkgup }) // returns nearest parent package.json object function pkgup() { let main_dir = path.parse(require.main.filename).dir let main_pkg = path.join(main_dir, 'package.json') let srcs = main_dir.split(path.sep) let mexists = !_.isError(_.attempt(fs.accessSync, main_pkg, fs.constants.R_OK)) ? true : false let found = mexists ? main_pkg : null // keep truncating path until at drive root or package.json found while(srcs.pop() && !found) { let src = path.join(_.join(srcs, path.sep), 'package.json') let exists = !_.isError(_.attempt(fs.accessSync, src, fs.constants.R_OK)) ? true : false if(exists) found = src } if(!found) return let pkg = _.attempt(require, found) if(!pkg || _.isError(pkg)) return return _.assign(pkg, { "_root": path.parse(found).dir, "_path": found }) }