index.d.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /// <reference types="node" />
  2. /**
  3. * function to log in case require module was not found
  4. *
  5. * @params message - message to log
  6. * @params path - path of the module that user tried to require
  7. */
  8. export declare type LogFunction = (message: string, path: string) => void;
  9. /**
  10. * Options for calling optionalRequire
  11. */
  12. export declare type OptionalRequireOpts = {
  13. /**
  14. * `notFound` is a function. If error from require was `MODULE_NOT_FOUND`, then:
  15. *
  16. * - call `notFound` if it's provided
  17. * - else return the `default` value.
  18. *
  19. * @remark in case the error was not `MODULE_NOT_FOUND`, will instead call the `fail` function
  20. * if it's provided.
  21. *
  22. * @param err - the error from require
  23. */
  24. notFound?: (err: Error) => unknown;
  25. /**
  26. * `fail` is a function. If error from require was something other than `MODULE_NOT_FOUND`,
  27. * for example, the module contains syntax error, then:
  28. *
  29. * - call `fail` if it's provided
  30. * - else rethrow the error
  31. *
  32. * @remark This is a separate callback from `notFound` so user can handle
  33. * real `MODULE_NOT_FOUND` exception separately, or let the `default` be returned.
  34. *
  35. * @param err - the error from require
  36. */
  37. fail?: (err: Error) => unknown;
  38. /**
  39. * The value to return if error was `MODULE_NOT_FOUND` but `notFound` is not provided.
  40. */
  41. default?: unknown;
  42. /**
  43. * Tell optional require to log a message if the module is not found.
  44. * - note: it doesn't log if the error is not due to the module not found
  45. *
  46. * This field can have these values:
  47. * 1. `true` - log with default message
  48. * 2. string - a string to prepend to the message being logged
  49. *
  50. * @remarks to further customize logging, set the `log` function.
  51. */
  52. message?: true | string;
  53. /**
  54. * function to log the module not found message, default log function uses `console.log`
  55. */
  56. log?: LogFunction;
  57. /**
  58. * `require` is the node.js require function from caller's context.
  59. *
  60. * If not provided, then use the one received when creating the optional require function
  61. */
  62. require?: NodeRequire;
  63. /**
  64. * If `true`, then do an optional `require.resolve` and return the full path
  65. */
  66. resolve?: boolean;
  67. };
  68. export declare function setDefaultLog(log: LogFunction): void;
  69. /**
  70. * try to require a module with optional handling in case it's not found or fail to require
  71. *
  72. * @param callerRequire - `require` from caller context
  73. * @param path - path to module to require
  74. * @param optsOrMsg - options or message to log in case of exceptions
  75. * @returns require result
  76. */
  77. export declare function tryRequire(callerRequire: NodeRequire, path: string, optsOrMsg?: OptionalRequireOpts | string | true): unknown;
  78. /**
  79. * try to resolve a module with optional handling in case it's not found or fail to require
  80. *
  81. * @param callerRequire - `require` from caller context
  82. * @param path - path to module to require
  83. * @param optsOrMsg - options or message to log in case of exceptions
  84. * @returns resolve result
  85. */
  86. export declare function tryResolve(callerRequire: NodeRequire, path: string, optsOrMsg?: OptionalRequireOpts | string | true): string;
  87. /**
  88. * function to require a module with optional handling in case it's not found or fail to require
  89. */
  90. export declare type OptionalRequireFunction<T = any> = {
  91. /**
  92. * @param path - path to module to require
  93. * @param optsOrMsg - options or message to log when module not found
  94. */
  95. (path: string, optsOrMsg?: OptionalRequireOpts | string | true): T;
  96. /**
  97. * resolve the module's full path
  98. *
  99. * @param path - path to module to resolve
  100. * @param optsOrMsg - options or message to log when module not found
  101. * @returns resolve result
  102. */
  103. resolve: (path: string, opsOrMsg?: OptionalRequireOpts | string | true) => string;
  104. /**
  105. * function to log message, default to use `console.log`, you can replace this with
  106. * another function.
  107. */
  108. log: LogFunction;
  109. };
  110. /**
  111. * Make an optional require function, using the `require` from caller's context.
  112. *
  113. * @param callerRequire - `require` from caller's context
  114. * @param log - function to log if module is not found
  115. * @returns required module
  116. */
  117. export declare function makeOptionalRequire<T = any>(callerRequire: NodeRequire, log?: (message: string, path: string) => void): OptionalRequireFunction<T>;
  118. /**
  119. * A default optionalRequire function using `require` from optional-require's context.
  120. *
  121. * @remarks because `require` is from optional-require's context, you won't be able to
  122. * do `optionalRequire("./my-module")`.
  123. *
  124. * You can still override the `require` using `options.require` with the one from your
  125. * calling context.
  126. *
  127. */
  128. export declare const optionalRequire: OptionalRequireFunction<any>;
  129. /**
  130. * An optionalRequire function using `require` from CWD context
  131. *
  132. * @remarks because `require` is from CWD context, if you do `optionalRequireCwd("./my-module")`
  133. * then it will look for `my-module` in CWD.
  134. *
  135. * @remarks You can still override the `require` using `options.require` with the one from your
  136. * calling context.
  137. */
  138. export declare const optionalRequireCwd: OptionalRequireFunction<any>;