|
| 1 | +/* |
| 2 | +Copyright 2010 James Halliday (mail@substack.net) |
| 3 | +
|
| 4 | +This project is free software released under the MIT/X11 license: |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +THE SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +var path = require("path"); |
| 26 | +var fs = require("fs"); |
| 27 | +var _0777 = parseInt("0777", 8); |
| 28 | + |
| 29 | +module.exports = function mkdirp(p, opts, made) { |
| 30 | + if (!opts || typeof opts !== "object") { |
| 31 | + opts = { mode: opts }; |
| 32 | + } |
| 33 | + var mode = opts.mode; |
| 34 | + if (mode === undefined) { |
| 35 | + mode = _0777 & (~process.umask()); |
| 36 | + } |
| 37 | + if (!made) made = null; |
| 38 | + p = path.resolve(p); |
| 39 | + try { |
| 40 | + fs.mkdirSync(p, mode); |
| 41 | + made = made || p; |
| 42 | + } catch (err0) { |
| 43 | + switch (err0.code) { |
| 44 | + case "ENOENT": |
| 45 | + made = mkdirp(path.dirname(p), opts, made); |
| 46 | + mkdirp(p, opts, made); |
| 47 | + break; |
| 48 | + default: |
| 49 | + var stat; |
| 50 | + try { |
| 51 | + stat = fs.statSync(p); |
| 52 | + } catch (err1) { |
| 53 | + throw err0; |
| 54 | + } |
| 55 | + if (!stat.isDirectory()) throw err0; |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + return made; |
| 60 | +}; |
0 commit comments