utils.js 582 B

12345678910111213141516171819202122232425262728
  1. const Promise = require('bluebird');
  2. const fs = require('fs');
  3. function sleep(ms) {
  4. return new Promise(resolve => setTimeout(resolve, ms));
  5. }
  6. function statPathSync(path) {
  7. try {
  8. return fs.statSync(path);
  9. } catch (ex) {}
  10. return false;
  11. }
  12. function mkDirIfNotExistsSync(path) {
  13. let exists = statPathSync(path);
  14. if (!exists) {
  15. fs.mkdirSync(path, {recursive: true});
  16. } else if (!exists.isDirectory()) {
  17. throw new Error(`Not a directory: ${path}`);
  18. }
  19. }
  20. module.exports = {
  21. sleep,
  22. statPathSync,
  23. mkDirIfNotExistsSync,
  24. };