deprecate.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. let deprecationHandler: ElectronInternal.DeprecationHandler | null = null
  2. function warnOnce (oldName: string, newName?: string) {
  3. let warned = false
  4. const msg = newName
  5. ? `'${oldName}' is deprecated and will be removed. Please use '${newName}' instead.`
  6. : `'${oldName}' is deprecated and will be removed.`
  7. return () => {
  8. if (!warned && !process.noDeprecation) {
  9. warned = true
  10. deprecate.log(msg)
  11. }
  12. }
  13. }
  14. const deprecate: ElectronInternal.DeprecationUtil = {
  15. setHandler: (handler) => { deprecationHandler = handler },
  16. getHandler: () => deprecationHandler,
  17. warn: (oldName, newName) => {
  18. if (!process.noDeprecation) {
  19. deprecate.log(`'${oldName}' is deprecated. Use '${newName}' instead.`)
  20. }
  21. },
  22. log: (message) => {
  23. if (typeof deprecationHandler === 'function') {
  24. deprecationHandler(message)
  25. } else if (process.throwDeprecation) {
  26. throw new Error(message)
  27. } else if (process.traceDeprecation) {
  28. return console.trace(message)
  29. } else {
  30. return console.warn(`(electron) ${message}`)
  31. }
  32. },
  33. // remove a function with no replacement
  34. removeFunction: (fn, removedName) => {
  35. if (!fn) { throw Error(`'${removedName} function' is invalid or does not exist.`) }
  36. // wrap the deprecated function to warn user
  37. const warn = warnOnce(`${fn.name} function`)
  38. return function (this: any) {
  39. warn()
  40. fn.apply(this, arguments)
  41. }
  42. },
  43. // change the name of a function
  44. renameFunction: (fn, newName) => {
  45. const warn = warnOnce(`${fn.name} function`, `${newName} function`)
  46. return function (this: any) {
  47. warn()
  48. fn.apply(this, arguments)
  49. }
  50. },
  51. // change the name of an event
  52. event: (emitter, oldName, newName) => {
  53. const warn = newName.startsWith('-') /* internal event */
  54. ? warnOnce(`${oldName} event`)
  55. : warnOnce(`${oldName} event`, `${newName} event`)
  56. return emitter.on(newName, function (this: NodeJS.EventEmitter, ...args) {
  57. if (this.listenerCount(oldName) !== 0) {
  58. warn()
  59. this.emit(oldName, ...args)
  60. }
  61. })
  62. },
  63. // deprecate a getter/setter function pair in favor of a property
  64. fnToProperty: (module: any, prop: string, getter: string, setter: string) => {
  65. const withWarnOnce = (obj: any, key: any, oldName: string, newName: string) => {
  66. const warn = warnOnce(oldName, newName)
  67. return (...args: any) => {
  68. warn()
  69. return obj[key](...args)
  70. }
  71. }
  72. module[getter.substr(1)] = withWarnOnce(module, getter, `${getter.substr(1)} function`, `${prop} property`)
  73. module[setter.substr(1)] = withWarnOnce(module, setter, `${setter.substr(1)} function`, `${prop} property`)
  74. },
  75. // remove a property with no replacement
  76. removeProperty: (o, removedName) => {
  77. // if the property's already been removed, warn about it
  78. if (!(removedName in o)) {
  79. deprecate.log(`Unable to remove property '${removedName}' from an object that lacks it.`)
  80. }
  81. // wrap the deprecated property in an accessor to warn
  82. const warn = warnOnce(removedName)
  83. let val = o[removedName]
  84. return Object.defineProperty(o, removedName, {
  85. configurable: true,
  86. get: () => {
  87. warn()
  88. return val
  89. },
  90. set: newVal => {
  91. warn()
  92. val = newVal
  93. }
  94. })
  95. },
  96. // deprecate a callback-based function in favor of one returning a Promise
  97. promisify: <T extends (...args: any[]) => any>(fn: T): T => {
  98. const fnName = fn.name || 'function'
  99. const oldName = `${fnName} with callbacks`
  100. const newName = `${fnName} with Promises`
  101. const warn = warnOnce(oldName, newName)
  102. return function (this: any, ...params: any[]) {
  103. let cb: Function | undefined
  104. if (params.length > 0 && typeof params[params.length - 1] === 'function') {
  105. cb = params.pop()
  106. }
  107. const promise = fn.apply(this, params)
  108. if (!cb) return promise
  109. if (process.enablePromiseAPIs) warn()
  110. return promise
  111. .then((res: any) => {
  112. process.nextTick(() => {
  113. cb!.length === 2 ? cb!(null, res) : cb!(res)
  114. })
  115. return res
  116. }, (err: Error) => {
  117. process.nextTick(() => {
  118. cb!.length === 2 ? cb!(err) : cb!()
  119. })
  120. throw err
  121. })
  122. } as T
  123. },
  124. // deprecate a callback-based function in favor of one returning a Promise
  125. promisifyMultiArg: <T extends (...args: any[]) => any>(fn: T, convertPromiseValue?: (v: any) => any): T => {
  126. const fnName = fn.name || 'function'
  127. const oldName = `${fnName} with callbacks`
  128. const newName = `${fnName} with Promises`
  129. const warn = warnOnce(oldName, newName)
  130. return function (this: any, ...params) {
  131. let cb: Function | undefined
  132. if (params.length > 0 && typeof params[params.length - 1] === 'function') {
  133. cb = params.pop()
  134. }
  135. const promise = fn.apply(this, params)
  136. if (!cb) return promise
  137. if (process.enablePromiseAPIs) warn()
  138. return promise
  139. .then((res: any) => {
  140. if (convertPromiseValue) { res = convertPromiseValue(res) }
  141. process.nextTick(() => {
  142. // eslint-disable-next-line standard/no-callback-literal
  143. cb!.length > 2 ? cb!(null, ...res) : cb!(...res)
  144. })
  145. }, (err: Error) => {
  146. process.nextTick(() => cb!(err))
  147. })
  148. } as T
  149. },
  150. // change the name of a property
  151. renameProperty: (o, oldName, newName) => {
  152. const warn = warnOnce(oldName, newName)
  153. // if the new property isn't there yet,
  154. // inject it and warn about it
  155. if ((oldName in o) && !(newName in o)) {
  156. warn()
  157. o[newName] = (o as any)[oldName]
  158. }
  159. // wrap the deprecated property in an accessor to warn
  160. // and redirect to the new property
  161. return Object.defineProperty(o, oldName, {
  162. get: () => {
  163. warn()
  164. return o[newName]
  165. },
  166. set: value => {
  167. warn()
  168. o[newName] = value
  169. }
  170. })
  171. }
  172. }
  173. export default deprecate