UNPKG

3.9 kBTypeScriptView Raw
1// Type definitions for mkdirp 1.0
2// Project: https://github.com/substack/node-mkdirp
3// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
4// mrmlnc <https://github.com/mrmlnc>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6
7/// <reference types="node" />
8
9import fs = require('fs');
10
11/**
12 * Create a new directory and any necessary subdirectories at dir with octal
13 * permission string `opts.mode`. If opts is a string or number, it will be
14 * treated as the `opts.mode`. If opts.mode isn't specified, it defaults to
15 * 0o777 & (~`process.umask()`).
16 *
17 * Promise resolves to first directory made that had to be created, or
18 * undefined if everything already exists. Promise rejects if any errors are
19 * encountered. Note that, in the case of promise rejection, some directories
20 * may have been created, as recursive directory creation is not an atomic operation.
21 * You can optionally pass in an alternate fs implementation by passing in
22 * opts.fs.
23 *
24 * Your implementation should have `opts.fs.mkdir(path, opts, cb)` and
25 * `opts.fs.stat(path, cb)`.
26 *
27 * You can also override just one or the other of mkdir and stat by passing in
28 * `opts.stat` or `opts.mkdir`, or providing an fs option that only overrides one
29 * of these.
30 */
31declare function mkdirp(dir: string, opts?: mkdirp.Mode | mkdirp.Options): Promise<string|undefined>;
32
33declare namespace mkdirp {
34 type Mode = number | string | undefined;
35
36 interface FsImplementation {
37 mkdir: typeof fs.mkdir;
38 stat: typeof fs.stat;
39 }
40
41 interface FsImplementationSync {
42 mkdirSync: typeof fs.mkdirSync;
43 statSync: typeof fs.statSync;
44 }
45
46 interface Options {
47 mode?: Mode;
48 fs?: FsImplementation;
49 }
50
51 interface OptionsSync {
52 mode?: Mode;
53 fs?: FsImplementationSync;
54 }
55
56 /**
57 * Synchronously create a new directory and any necessary subdirectories at
58 * dir with octal permission string `opts.mode`. If opts is a string or number,
59 * it will be treated as the `opts.mode`. If `opts.mode` isn't specified, it
60 * defaults to 0o777 & (~`process.umask()`).
61 * You can optionally pass in an alternate fs implementation by passing in
62 * `opts.fs`. Your implementation should have `opts.fs.mkdirSync(path, mode)`
63 * and `opts.fs.statSync(path)`. You can also override just one or the other
64 * of `mkdirSync` and `statSync` by passing in `opts.statSync` or `opts.mkdirSync`,
65 * or providing an fs option that only overrides one of these.
66 * @returns Returns the first directory that had to be created, or undefined if everything already exists.
67 */
68 function sync(dir: string, opts?: Mode | OptionsSync): string|undefined;
69
70 /**
71 * Use the manual implementation (not the native one). This is the default
72 * when the native implementation is not available or the stat/mkdir
73 * implementation is overridden.
74 */
75 function manual(dir: string, opts?: Mode | Options): Promise<string|undefined>;
76
77 /**
78 * Use the manual implementation (not the native one). This is the default
79 * when the native implementation is not available or the stat/mkdir
80 * implementation is overridden.
81 */
82 function manualSync(dir: string, opts?: Mode | OptionsSync): string|undefined;
83
84 /**
85 * Use the native implementation (not the manual one). This is the default
86 * when the native implementation is available and stat/mkdir are not
87 * overridden.
88 */
89 function native(dir: string, opts?: Mode | Options): Promise<string|undefined>;
90
91 /**
92 * Use the native implementation (not the manual one). This is the default
93 * when the native implementation is available and stat/mkdir are not
94 * overridden.
95 */
96 function nativeSync(dir: string, opts?: Mode | OptionsSync): string|undefined;
97}
98export = mkdirp;