utils.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const fs = require('fs');
  2. const path = require('path');
  3. function mkdir(targetDir, {isRelativeToScript = false} = {}) {
  4. const sep = path.sep;
  5. const initDir = path.isAbsolute(targetDir) ? sep : '';
  6. const baseDir = isRelativeToScript ? __dirname : '.';
  7. targetDir.split(sep).reduce((parentDir, childDir) => {
  8. const curDir = path.resolve(baseDir, parentDir, childDir);
  9. try {
  10. fs.mkdirSync(curDir);
  11. } catch (err) {
  12. if (err.code !== 'EEXIST') {
  13. throw err;
  14. }
  15. }
  16. return curDir;
  17. }, initDir);
  18. }
  19. function rmdir(p) {
  20. console.log("path = "+p);
  21. if (fs.existsSync(p)) {
  22. fs.readdirSync(p).forEach(function(file, index){
  23. var curPath = p + "/" + file;
  24. if (fs.lstatSync(curPath).isDirectory()) { // recurse
  25. rmdir(curPath);
  26. } else { // delete file
  27. fs.unlinkSync(curPath);
  28. }
  29. });
  30. fs.rmdirSync(p);
  31. }
  32. };
  33. function mv(old, ne) {
  34. console.log("old: '"+old+"' new = '"+ne+"'")
  35. return fs.renameSync(old, ne);
  36. };
  37. function rm(file)
  38. {
  39. fs.unlinkSync(file);
  40. }
  41. module.exports.mkdir = mkdir;
  42. module.exports.rmdir = rmdir;
  43. module.exports.mv = mv;
  44. module.exports.rm = rm;