1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 'use strict';
- var path = require('path')
- , fs = require('fs');
- function parse(data) {
- data = data.toString('utf-8');
-
-
-
-
- if (data.charCodeAt(0) === 0xFEFF) data = data.slice(1);
- try { return JSON.parse(data); }
- catch (e) { return false; }
- }
- module.exports = function find(root) {
- root = root || process.cwd();
- if (typeof root !== "string") {
- if (typeof root === "object" && typeof root.filename === 'string') {
- root = root.filename;
- } else {
- throw new Error("Must pass a filename string or a module object to finder");
- }
- }
- return {
-
- next: function next() {
- if (root.match(/^(\w:\\|\/)$/)) return {
- value: undefined,
- filename: undefined,
- done: true
- };
- var file = path.join(root, 'package.json')
- , data;
- root = path.resolve(root, '..');
- if (fs.existsSync(file) && (data = parse(fs.readFileSync(file)))) {
- data.__path = file;
- return {
- value: data,
- filename: file,
- done: false
- };
- }
- return next();
- }
- };
- };
|