deprecate.js 879 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const _ = require('lodash');
  3. const chalk = require('chalk');
  4. const deprecate = (message, fn) => {
  5. return function () {
  6. deprecate.log(message);
  7. return fn.apply(this, arguments);
  8. };
  9. };
  10. deprecate.log = message => {
  11. console.log(chalk.yellow('(!) ') + message);
  12. };
  13. deprecate.object = (message, object) => {
  14. const msgTpl = _.template(message);
  15. const mirror = [];
  16. for (const name of Object.keys(object)) {
  17. const func = object[name];
  18. if (typeof func !== 'function') {
  19. mirror[name] = func;
  20. continue;
  21. }
  22. mirror[name] = deprecate(msgTpl({name}), func);
  23. }
  24. return mirror;
  25. };
  26. deprecate.property = (message, object, property) => {
  27. const original = object[property];
  28. Object.defineProperty(object, property, {
  29. get() {
  30. deprecate.log(message);
  31. return original;
  32. }
  33. });
  34. };
  35. module.exports = deprecate;