make-dir.ts 537 B

12345678910111213141516171819202122
  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 {
  12. fs.mkdirSync(root);
  13. } catch (e) {
  14. if (!fs.statSync(root).isDirectory()) throw new Error(e);
  15. }
  16. return !dirs.length || makeDir(dirs.join('/'), root);
  17. }