utils.js 618 B

1234567891011121314151617181920212223242526272829
  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. console.log(path);
  14. let exists = statPathSync(path);
  15. if (!exists) {
  16. fs.mkdirSync(path, {recursive: true, mode: 0o755});
  17. } else if (!exists.isDirectory()) {
  18. throw new Error(`Not a directory: ${path}`);
  19. }
  20. }
  21. module.exports = {
  22. sleep,
  23. statPathSync,
  24. mkDirIfNotExistsSync,
  25. };