package.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. module.exports = exports = _package;
  3. exports.usage = 'Packs binary (and enclosing directory) into locally staged tarball';
  4. const fs = require('fs');
  5. const path = require('path');
  6. const log = require('npmlog');
  7. const versioning = require('./util/versioning.js');
  8. const napi = require('./util/napi.js');
  9. const existsAsync = fs.exists || path.exists;
  10. const makeDir = require('make-dir');
  11. const tar = require('tar');
  12. function readdirSync(dir) {
  13. let list = [];
  14. const files = fs.readdirSync(dir);
  15. files.forEach((file) => {
  16. const stats = fs.lstatSync(path.join(dir, file));
  17. if (stats.isDirectory()) {
  18. list = list.concat(readdirSync(path.join(dir, file)));
  19. } else {
  20. list.push(path.join(dir, file));
  21. }
  22. });
  23. return list;
  24. }
  25. function _package(gyp, argv, callback) {
  26. const package_json = gyp.package_json;
  27. const napi_build_version = napi.get_napi_build_version_from_command_args(argv);
  28. const opts = versioning.evaluate(package_json, gyp.opts, napi_build_version);
  29. const from = opts.module_path;
  30. const binary_module = path.join(from, opts.module_name + '.node');
  31. existsAsync(binary_module, (found) => {
  32. if (!found) {
  33. return callback(new Error('Cannot package because ' + binary_module + ' missing: run `node-pre-gyp rebuild` first'));
  34. }
  35. const tarball = opts.staged_tarball;
  36. const filter_func = function(entry) {
  37. const basename = path.basename(entry);
  38. if (basename.length && basename[0] !== '.') {
  39. console.log('packing ' + entry);
  40. return true;
  41. } else {
  42. console.log('skipping ' + entry);
  43. }
  44. return false;
  45. };
  46. makeDir(path.dirname(tarball)).then(() => {
  47. let files = readdirSync(from);
  48. const base = path.basename(from);
  49. files = files.map((file) => {
  50. return path.join(base, path.relative(from, file));
  51. });
  52. tar.create({
  53. portable: false,
  54. gzip: true,
  55. filter: filter_func,
  56. file: tarball,
  57. cwd: path.dirname(from)
  58. }, files, (err2) => {
  59. if (err2) console.error('[' + package_json.name + '] ' + err2.message);
  60. else log.info('package', 'Binary staged at "' + tarball + '"');
  61. return callback(err2);
  62. });
  63. }).catch((err) => {
  64. return callback(err);
  65. });
  66. });
  67. }