index.d.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /// <reference types="node"/>
  2. import {MergeExclusive, TypedArray} from 'type-fest';
  3. declare namespace tempy {
  4. type FileOptions = MergeExclusive<
  5. {
  6. /**
  7. File extension.
  8. Mutually exclusive with the `name` option.
  9. _You usually won't need this option. Specify it only when actually needed._
  10. */
  11. readonly extension?: string;
  12. },
  13. {
  14. /**
  15. Filename.
  16. Mutually exclusive with the `extension` option.
  17. _You usually won't need this option. Specify it only when actually needed._
  18. */
  19. readonly name?: string;
  20. }
  21. >;
  22. type DirectoryOptions = {
  23. /**
  24. _You usually won't need this option. Specify it only when actually needed._
  25. Directory prefix.
  26. Useful for testing by making it easier to identify cache directories that are created.
  27. */
  28. readonly prefix?: string;
  29. };
  30. /**
  31. The temporary path created by the function. Can be asynchronous.
  32. */
  33. type TaskCallback<ReturnValueType> = (tempPath: string) => Promise<ReturnValueType> | ReturnValueType;
  34. }
  35. declare const tempy: {
  36. file: {
  37. /**
  38. The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed.
  39. @returns A promise that resolves after the callback is executed and the file is cleaned up.
  40. @example
  41. ```
  42. import tempy = require('tempy');
  43. await tempy.file.task(tempFile => {
  44. console.log(tempFile);
  45. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
  46. });
  47. ```
  48. */
  49. task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;
  50. /**
  51. Get a temporary file path you can write to.
  52. @example
  53. ```
  54. import tempy = require('tempy');
  55. tempy.file();
  56. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
  57. tempy.file({extension: 'png'});
  58. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/a9fb0decd08179eb6cf4691568aa2018.png'
  59. tempy.file({name: 'unicorn.png'});
  60. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/f7f62bfd4e2a05f1589947647ed3f9ec/unicorn.png'
  61. tempy.directory();
  62. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
  63. ```
  64. */
  65. (options?: tempy.FileOptions): string;
  66. };
  67. directory: {
  68. /**
  69. The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed.
  70. @returns A promise that resolves after the callback is executed and the directory is cleaned up.
  71. @example
  72. ```
  73. import tempy = require('tempy');
  74. await tempy.directory.task(tempDirectory => {
  75. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
  76. })
  77. ```
  78. */
  79. task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.DirectoryOptions) => Promise<ReturnValueType>;
  80. /**
  81. Get a temporary directory path. The directory is created for you.
  82. @example
  83. ```
  84. import tempy = require('tempy');
  85. tempy.directory();
  86. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
  87. tempy.directory({prefix: 'a'});
  88. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/name_3c085674ad31223b9653c88f725d6b41'
  89. ```
  90. */
  91. (options?: tempy.DirectoryOptions): string;
  92. };
  93. write: {
  94. /**
  95. Write data to a random temp file. The file is automatically cleaned up after the callback is executed.
  96. @returns A promise that resolves after the callback is executed and the file is cleaned up.
  97. @example
  98. ```
  99. import tempy = require('tempy');
  100. await tempy.write.task('🦄', tempFile => {
  101. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/4f504b9edb5ba0e89451617bf9f971dd'
  102. });
  103. ```
  104. */
  105. task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;
  106. /**
  107. Write data to a random temp file.
  108. @example
  109. ```
  110. import tempy = require('tempy');
  111. await tempy.write('🦄');
  112. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
  113. ```
  114. */
  115. (fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, options?: tempy.FileOptions): Promise<string>;
  116. };
  117. /**
  118. Synchronously write data to a random temp file.
  119. @example
  120. ```
  121. import tempy = require('tempy');
  122. tempy.writeSync('🦄');
  123. //=> '/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T/2f3d094aec2cb1b93bb0f4cffce5ebd6'
  124. ```
  125. */
  126. writeSync: (fileContent: string | Buffer | TypedArray | DataView, options?: tempy.FileOptions) => string;
  127. /**
  128. Get the root temporary directory path.
  129. For example: `/private/var/folders/3x/jf5977fn79jbglr7rk0tq4d00000gn/T`.
  130. */
  131. readonly root: string;
  132. };
  133. export = tempy;