getDirectory.js 599 B

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