Esempio n. 1
0
module.exports = function (name, opts, cb) {
  if (typeof opts === "function") {
    cb = opts;
    opts = {};
  }
  var executable = opts.executable || name;

  var mod;
  try {
    mod = require.resolve(name);
  } catch (err) {
    return cb(err);
  }

  findParentDir(mod, 'package.json', function (err, dir) {
    if (err) return cb(err);

    var pack = require(path.join(dir, 'package.json'));
    var binfield = pack.bin;

    var binpath = typeof binfield === 'object' ? binfield[executable] : binfield;
    var bin = path.join(dir, binpath);
    cb(null, bin);
  });
}
Esempio n. 2
0
module.exports = function(filename){
  var def = Q.defer();
  findParentDir(path.dirname(filename), 'package.json', function(err, dir){
     if(err) {
       def.reject(err);
     }
    var fullDir = dir === '.' ? path.dirname(filename) : dir;
    var pack = require(fullDir + '/package');
    def.resolve({pack: pack, dir: fullDir});
  });
  return def.promise;
};
Esempio n. 3
0
function resolveCommand(file, cb) {
  if (exports.cmd) return cb(null, { dir: exports.dir, cmd: exports.cmd });

  findParent(file, 'package.json', function (err, dir) {
    if (err) return cb(err);
    if (!dir) return cb(null, null);

    var packfile = path.join(dir, 'package.json');
    var pack = require(packfile);

    cb(null, {dir: dir, cmd: pack.commandify });
  });
}
Esempio n. 4
0
  (function findShip (root) {
    findParentDir(root, 'package.json', function (err, packageDir) {
      if (err) return cb(err);
      if (!packageDir) return cb();

      var pack;
      try {
        pack = require(path.join(packageDir, 'package.json'));
        if (ismothership(pack)) return cb(null, { path: path.join(packageDir, 'package.json'), pack: pack });
        findShip(path.resolve(root, '..'));
      } catch (e) {
        cb(e);
      }
    });

  })(start);
Esempio n. 5
0
function setIndentRule(rootPath, callback) {
    findParentDir(rootPath, '.editorconfig', onEditorConfigDir);

    function onEditorConfigDir(err, editorConfigDir) {
        if (err || !editorConfigDir) {
            // If no editorconfig found, swallow the error and default to 4.
            return callback();
        }
        var editorConfigPath = path.join(editorConfigDir, '.editorconfig');
        editorConfigParse(editorConfigPath, onEditorConfigParse);
    }

    function onEditorConfigParse(parseErr, parsed) {
        if (parseErr) {
            // If we can't parse .editorconfig, throw an error since there may
            // be a *.js indent rule we want to respect.
            return callback(parseErr);
        }
        var rules = getRuleset(parsed, '*.js') || getRuleset(parsed, '*');

        if (!rules || rules[0].indent_size) {
            // There is no ruleset that applies to *.js files or there is no
            // indent_size defined, don't change indent rule.
            return callback();
        }

        var jscsrcPath = path.resolve(__dirname, './rc/.jscsrc');
        var indent = parseInt(rules[1].indent_size, 10);
        if (!indent) {
            var indentError = new Error('Invalid indent from editorconfig');
            return callback(indentError);
        }
        var diff = {validateIndentation: indent};
        updateJSON(jscsrcPath, diff);
        callback();
    }

    function getRuleset(parsed, id) {
        return parsed.filter(function getJsRuleSet(ruleset) {
            return ruleset[0] === id;
        })[0];
    }
}
Esempio n. 6
0
function getAssertFilePath(filePath, callback) {
  assertFunction('callback', callback);

  if (isExplicitFilePath(filePath)) {
    callback(null, filePath);
  }

  var cwd = process.cwd();

  findParentDir(cwd, filePath, function (error, directory) {
    if (error || directory === null) {
      throw new Error(filePath + ' cannot be found.');
    }

    var assertFilePath = directory + '/' + filePath;

    callback(null, assertFilePath);
  });
}
Esempio n. 7
0
function info (name, cb) {
  parentDir(require.resolve(name), 'package.json', function (err, dir) {
    if (err) return cb(err);
    cb(null, getPackinfo(dir));
  });
}
Esempio n. 8
0
  (function resolve(rootdir, lookedUp) {
    parentDir(rootdir, 'package.json', function (err, packageDir) {
      if (err) return cb(err);

      var pack;

      var packFile = path.join(packageDir, 'package.json');
      // we cached this before which means it was also grouped by file
      var cached = shimsCache[packageDir];
      if (cached) { 
        pack = cached.pack;
        return cb(null, { 
            package_json       :  packFile
          , packageDir         :  packageDir
          , resolvedPreviously :  true
          , shim               :  shimsByPath[file]
          , exposeGlobals      :  cached.exposeGlobals
          , browser            :  pack.browser
          , 'browserify-shim'  :  pack['browserify-shim']
          , dependencies       :  pack.dependencies
          , lookedUp           :  lookedUp
        });
      }

      try {
        pack = require(packFile);

        var shimField = pack['browserify-shim'];

        if (!shimField) { 
          if (lookedUp) return cb(null, { package_json: packFile, shim: undefined }); 

          // some files are inside invalid packages, i.e. bower components
          // in this case the package.json we find first is not the one that has the shims definitions
          // therefore we'll give it one more chance and look inside the package.json one level higher
          var root = path.resolve(packageDir, '..');
          return resolve(root, true);
        }

        var resolved = typeof shimField === 'string'
          ? resolveFromShimFile(packageDir, pack, shimField, messages)
          : resolveInlineShims(packageDir, pack, shimField, messages);

        messages.push({ resolved: resolved.shims });
        updateCache(packageDir, pack, resolved.shims, resolved.exposeGlobals);

        cb(null, { 
            package_json      :  packFile
          , packageDir        :  packageDir 
          , shim              :  shimsByPath[file]
          , exposeGlobals     :  resolved.exposeGlobals
          , browser           :  pack.browser
          , 'browserify-shim' :  pack['browserify-shim']
          , dependencies      :  pack.dependencies
          , lookedUp          :  lookedUp
        });

      } catch (err) {
        console.trace();
        return cb(err);
      }
    });
  })(path.dirname(file), false);