You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
8.8 KiB

4 years ago
  1. declare module "upath" {
  2. /**
  3. * A parsed path object generated by path.parse() or consumed by path.format().
  4. */
  5. export interface ParsedPath {
  6. /**
  7. * The root of the path such as '/' or 'c:\'
  8. */
  9. root: string;
  10. /**
  11. * The full directory path such as '/home/user/dir' or 'c:\path\dir'
  12. */
  13. dir: string;
  14. /**
  15. * The file name including extension (if any) such as 'index.html'
  16. */
  17. base: string;
  18. /**
  19. * The file extension (if any) such as '.html'
  20. */
  21. ext: string;
  22. /**
  23. * The file name without extension (if any) such as 'index'
  24. */
  25. name: string;
  26. }
  27. /**
  28. * Version of the library
  29. */
  30. export var VERSION: string;
  31. /**
  32. * Just converts all `to/` and consolidates duplicates, without performing any normalization.
  33. *
  34. * @param p string path to convert to unix.
  35. */
  36. export function toUnix(p: string): string;
  37. /**
  38. * Exactly like path.normalize(path), but it keeps the first meaningful ./.
  39. *
  40. * Note that the unix / is returned everywhere, so windows \ is always converted to unix /.
  41. *
  42. * @param p string path to normalize.
  43. */
  44. export function normalizeSafe(p: string): string;
  45. /**
  46. * Exactly like path.normalizeSafe(path), but it trims any useless ending /.
  47. *
  48. * @param p string path to normalize
  49. */
  50. export function normalizeTrim(p: string): string;
  51. /**
  52. * Exactly like path.join(), but it keeps the first meaningful ./.
  53. *
  54. * Note that the unix / is returned everywhere, so windows \ is always converted to unix /.
  55. *
  56. * @param paths string paths to join
  57. */
  58. export function joinSafe(...p: any[]): string;
  59. /**
  60. * Adds .ext to filename, but only if it doesn't already have the exact extension.
  61. *
  62. * @param file string filename to add extension to
  63. * @param ext string extension to add
  64. */
  65. export function addExt(file: string, ext: string): string;
  66. /**
  67. * Trims a filename's extension.
  68. *
  69. * Extensions are considered to be up to maxSize chars long, counting the dot (defaults to 7).
  70. *
  71. * An Array of ignoreExts (eg ['.min']) prevents these from being considered as extension, thus are not trimmed.
  72. *
  73. * @param filename string filename to trim it's extension
  74. * @param ignoreExts array extensions to ignore
  75. * @param maxSize number max length of the extension
  76. */
  77. export function trimExt(filename: string, ignoreExts?: string[], maxSize?: number): string;
  78. /**
  79. * Removes the specific ext extension from filename, if it has it. Otherwise it leaves it as is. As in all upath functions, it be .ext or ext.
  80. *
  81. * @param file string filename to remove extension to
  82. * @param ext string extension to remove
  83. */
  84. export function removeExt(filename: string, ext: string): string;
  85. /**
  86. * Changes a filename's extension to ext. If it has no (valid) extension, it adds it.
  87. *
  88. * Valid extensions are considered to be up to maxSize chars long, counting the dot (defaults to 7).
  89. *
  90. * An Array of ignoreExts (eg ['.min']) prevents these from being considered as extension, thus are not changed - the new extension is added instead.
  91. *
  92. * @param filename string filename to change it's extension
  93. * @param ext string extension to change to
  94. * @param ignoreExts array extensions to ignore
  95. * @param maxSize number max length of the extension
  96. */
  97. export function changeExt(filename: string, ext: string, ignoreExts?: string[], maxSize?: number): string;
  98. /**
  99. * Adds .ext to filename, only if it doesn't already have any old extension.
  100. *
  101. * (Old) extensions are considered to be up to maxSize chars long, counting the dot (defaults to 7).
  102. *
  103. * An Array of ignoreExts (eg ['.min']) will force adding default .ext even if one of these is present.
  104. *
  105. * @param filename string filename to default to it's extension
  106. * @param ext string extension to default to
  107. * @param ignoreExts array extensions to ignore
  108. * @param maxSize number max length of the extension
  109. */
  110. export function defaultExt(filename: string, ext: string, ignoreExts?: string[], maxSize?: number): string;
  111. /**
  112. * Normalize a string path, reducing '..' and '.' parts.
  113. * When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. On Windows backslashes are used.
  114. *
  115. * @param p string path to normalize.
  116. */
  117. export function normalize(p: string): string;
  118. /**
  119. * Join all arguments together and normalize the resulting path.
  120. * Arguments must be strings. In v0.8, non-string arguments were silently ignored. In v0.10 and up, an exception is thrown.
  121. *
  122. * @param paths string paths to join.
  123. */
  124. export function join(...paths: any[]): string;
  125. /**
  126. * Join all arguments together and normalize the resulting path.
  127. * Arguments must be strings. In v0.8, non-string arguments were silently ignored. In v0.10 and up, an exception is thrown.
  128. *
  129. * @param paths string paths to join.
  130. */
  131. export function join(...paths: string[]): string;
  132. /**
  133. * The right-most parameter is considered {to}. Other parameters are considered an array of {from}.
  134. *
  135. * Starting from leftmost {from} parameter, resolves {to} to an absolute path.
  136. *
  137. * If {to} isn't already absolute, {from} arguments are prepended in right to left order, until an absolute path is found. If after using all {from} paths still no absolute path is found, the current working directory is used as well. The resulting path is normalized, and trailing slashes are removed unless the path gets resolved to the root directory.
  138. *
  139. * @param pathSegments string paths to join. Non-string arguments are ignored.
  140. */
  141. export function resolve(...pathSegments: any[]): string;
  142. /**
  143. * Determines whether {path} is an absolute path. An absolute path will always resolve to the same location, regardless of the working directory.
  144. *
  145. * @param path path to test.
  146. */
  147. export function isAbsolute(path: string): boolean;
  148. /**
  149. * Solve the relative path from {from} to {to}.
  150. * At times we have two absolute paths, and we need to derive the relative path from one to the other. This is actually the reverse transform of path.resolve.
  151. *
  152. * @param from
  153. * @param to
  154. */
  155. export function relative(from: string, to: string): string;
  156. /**
  157. * Return the directory name of a path. Similar to the Unix dirname command.
  158. *
  159. * @param p the path to evaluate.
  160. */
  161. export function dirname(p: string): string;
  162. /**
  163. * Return the last portion of a path. Similar to the Unix basename command.
  164. * Often used to extract the file name from a fully qualified path.
  165. *
  166. * @param p the path to evaluate.
  167. * @param ext optionally, an extension to remove from the result.
  168. */
  169. export function basename(p: string, ext?: string): string;
  170. /**
  171. * Return the extension of the path, from the last '.' to end of string in the last portion of the path.
  172. * If there is no '.' in the last portion of the path or the first character of it is '.', then it returns an empty string
  173. *
  174. * @param p the path to evaluate.
  175. */
  176. export function extname(p: string): string;
  177. /**
  178. * The platform-specific file separator. '\\' or '/'.
  179. */
  180. export var sep: string;
  181. /**
  182. * The platform-specific file delimiter. ';' or ':'.
  183. */
  184. export var delimiter: string;
  185. /**
  186. * Returns an object from a path string - the opposite of format().
  187. *
  188. * @param pathString path to evaluate.
  189. */
  190. export function parse(pathString: string): ParsedPath;
  191. /**
  192. * Returns a path string from an object - the opposite of parse().
  193. *
  194. * @param pathString path to evaluate.
  195. */
  196. export function format(pathObject: ParsedPath): string;
  197. export module posix {
  198. export function normalize(p: string): string;
  199. export function join(...paths: any[]): string;
  200. export function resolve(...pathSegments: any[]): string;
  201. export function isAbsolute(p: string): boolean;
  202. export function relative(from: string, to: string): string;
  203. export function dirname(p: string): string;
  204. export function basename(p: string, ext?: string): string;
  205. export function extname(p: string): string;
  206. export var sep: string;
  207. export var delimiter: string;
  208. export function parse(p: string): ParsedPath;
  209. export function format(pP: ParsedPath): string;
  210. }
  211. export module win32 {
  212. export function normalize(p: string): string;
  213. export function join(...paths: any[]): string;
  214. export function resolve(...pathSegments: any[]): string;
  215. export function isAbsolute(p: string): boolean;
  216. export function relative(from: string, to: string): string;
  217. export function dirname(p: string): string;
  218. export function basename(p: string, ext?: string): string;
  219. export function extname(p: string): string;
  220. export var sep: string;
  221. export var delimiter: string;
  222. export function parse(p: string): ParsedPath;
  223. export function format(pP: ParsedPath): string;
  224. }
  225. }