index.js 766 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const path = require('path');
  3. const pathKey = require('path-key');
  4. module.exports = opts => {
  5. opts = Object.assign({
  6. cwd: process.cwd(),
  7. path: process.env[pathKey()]
  8. }, opts);
  9. let prev;
  10. let pth = path.resolve(opts.cwd);
  11. const ret = [];
  12. while (prev !== pth) {
  13. ret.push(path.join(pth, 'node_modules/.bin'));
  14. prev = pth;
  15. pth = path.resolve(pth, '..');
  16. }
  17. // ensure the running `node` binary is used
  18. ret.push(path.dirname(process.execPath));
  19. return ret.concat(opts.path).join(path.delimiter);
  20. };
  21. module.exports.env = opts => {
  22. opts = Object.assign({
  23. env: process.env
  24. }, opts);
  25. const env = Object.assign({}, opts.env);
  26. const path = pathKey({env});
  27. opts.path = env[path];
  28. env[path] = module.exports(opts);
  29. return env;
  30. };