getDirectory.js 568 B

1234567891011121314151617181920212223
  1. //
  2. 'use strict';
  3. const path = require('path');
  4. const isDirectory = require('is-directory');
  5. module.exports = function getDirectory(
  6. filepath ,
  7. sync
  8. ) {
  9. if (sync === true) {
  10. return isDirectory.sync(filepath) ? filepath : path.dirname(filepath);
  11. }
  12. return new Promise((resolve, reject) => {
  13. return isDirectory(filepath, (err, filepathIsDirectory) => {
  14. if (err) {
  15. return reject(err);
  16. }
  17. return resolve(filepathIsDirectory ? filepath : path.dirname(filepath));
  18. });
  19. });
  20. };