resolve 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var path = require('path');
  4. var fs = require('fs');
  5. if (
  6. String(process.env.npm_lifecycle_script).slice(0, 8) !== 'resolve '
  7. && (
  8. !process.argv
  9. || process.argv.length < 2
  10. || (process.argv[1] !== __filename && fs.statSync(process.argv[1]).ino !== fs.statSync(__filename).ino)
  11. || (process.env.npm_lifecycle_event !== 'npx' && process.env._ && fs.realpathSync(path.resolve(process.env._)) !== __filename)
  12. )
  13. ) {
  14. console.error('Error: `resolve` must be run directly as an executable');
  15. process.exit(1);
  16. }
  17. var supportsPreserveSymlinkFlag = require('supports-preserve-symlinks-flag');
  18. var preserveSymlinks = false;
  19. for (var i = 2; i < process.argv.length; i += 1) {
  20. if (process.argv[i].slice(0, 2) === '--') {
  21. if (supportsPreserveSymlinkFlag && process.argv[i] === '--preserve-symlinks') {
  22. preserveSymlinks = true;
  23. } else if (process.argv[i].length > 2) {
  24. console.error('Unknown argument ' + process.argv[i].replace(/[=].*$/, ''));
  25. process.exit(2);
  26. }
  27. process.argv.splice(i, 1);
  28. i -= 1;
  29. if (process.argv[i] === '--') { break; } // eslint-disable-line no-restricted-syntax
  30. }
  31. }
  32. if (process.argv.length < 3) {
  33. console.error('Error: `resolve` expects a specifier');
  34. process.exit(2);
  35. }
  36. var resolve = require('../');
  37. var result = resolve.sync(process.argv[2], {
  38. basedir: process.cwd(),
  39. preserveSymlinks: preserveSymlinks
  40. });
  41. console.log(result);