index.js 781 B

12345678910111213141516171819202122232425262728293031
  1. const path = require('path');
  2. const fs = require('fs');
  3. const pify = require('pify');
  4. function getPaths(abs, rel, ext) {
  5. return pify(fs.stat)(path.join(abs, rel))
  6. .then(stats => {
  7. if (stats.isDirectory()) {
  8. // a directory
  9. return {
  10. rel: path.join(rel, `index.${ext}`),
  11. ext
  12. };
  13. }
  14. // a file
  15. return { rel, ext: path.extname(rel).slice(1) };
  16. })
  17. .catch(err => {
  18. // not a valid file/directory
  19. if (!path.extname(rel) || path.extname(rel).slice(1) !== ext) {
  20. // Template file has been provided without the right extension
  21. // so append to it to try another lookup
  22. return getPaths(abs, `${rel}.${ext}`, ext);
  23. }
  24. throw err;
  25. });
  26. }
  27. module.exports = getPaths;