make-dir.ts 529 B

123456789101112131415161718192021
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as fs from 'fs';
  7. export function makeDir(path: string, root?: string): boolean {
  8. let dirs = path.split(/\/|\\/g),
  9. dir = dirs.shift();
  10. root = (root || '') + dir + '/';
  11. try { fs.mkdirSync(root); }
  12. catch (e) {
  13. if (!fs.statSync(root).isDirectory()) throw new Error(e);
  14. }
  15. return !dirs.length || makeDir(dirs.join('/'), root);
  16. }