pre-binding.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const npg = require('..');
  3. const versioning = require('../lib/util/versioning.js');
  4. const napi = require('../lib/util/napi.js');
  5. const existsSync = require('fs').existsSync || require('path').existsSync;
  6. const path = require('path');
  7. module.exports = exports;
  8. exports.usage = 'Finds the require path for the node-pre-gyp installed module';
  9. exports.validate = function(package_json, opts) {
  10. versioning.validate_config(package_json, opts);
  11. };
  12. exports.find = function(package_json_path, opts) {
  13. if (!existsSync(package_json_path)) {
  14. throw new Error(package_json_path + 'does not exist');
  15. }
  16. const prog = new npg.Run({ package_json_path, argv: process.argv });
  17. prog.setBinaryHostProperty();
  18. const package_json = prog.package_json;
  19. versioning.validate_config(package_json, opts);
  20. let napi_build_version;
  21. if (napi.get_napi_build_versions(package_json, opts)) {
  22. napi_build_version = napi.get_best_napi_build_version(package_json, opts);
  23. }
  24. opts = opts || {};
  25. if (!opts.module_root) opts.module_root = path.dirname(package_json_path);
  26. const meta = versioning.evaluate(package_json, opts, napi_build_version);
  27. return meta.module;
  28. };