clean.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. module.exports = exports = clean;
  3. exports.usage = 'Removes the entire folder containing the compiled .node module';
  4. const rm = require('rimraf');
  5. const exists = require('fs').exists || require('path').exists;
  6. const versioning = require('./util/versioning.js');
  7. const napi = require('./util/napi.js');
  8. const path = require('path');
  9. function clean(gyp, argv, callback) {
  10. const package_json = gyp.package_json;
  11. const napi_build_version = napi.get_napi_build_version_from_command_args(argv);
  12. const opts = versioning.evaluate(package_json, gyp.opts, napi_build_version);
  13. const to_delete = opts.module_path;
  14. if (!to_delete) {
  15. return callback(new Error('module_path is empty, refusing to delete'));
  16. } else if (path.normalize(to_delete) === path.normalize(process.cwd())) {
  17. return callback(new Error('module_path is not set, refusing to delete'));
  18. } else {
  19. exists(to_delete, (found) => {
  20. if (found) {
  21. if (!gyp.opts.silent_clean) console.log('[' + package_json.name + '] Removing "%s"', to_delete);
  22. return rm(to_delete, callback);
  23. }
  24. return callback();
  25. });
  26. }
  27. }