Project.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var SavableData = require("./SavableData");
  2. var global = require('./global')
  3. var E = require("./Errors");
  4. var fs = require("fs");
  5. var Resource = require("./Resource");
  6. var utils = require('./utils');
  7. module.exports = class Project extends SavableData {
  8. constructor()
  9. {
  10. super();
  11. this.stages = [];
  12. this.map = "";
  13. this.name = "";
  14. this.exclude = ["basePath"];
  15. this.location = null;
  16. this.basePath = "";
  17. this.user=null;
  18. this.resources = {}
  19. }
  20. load(user, name)
  21. {
  22. this.name=name;
  23. this.basePath = global.pathToProjectDir(user, name);
  24. this.path = global.pathToProjectFile(user, name);
  25. try {
  26. var obj = JSON.parse(fs.readFileSync(this.path));
  27. if(obj!=null) Object.assign(this, obj);
  28. } catch (e) {
  29. return E.ENOENT;
  30. }
  31. return this;
  32. }
  33. getResource(user, project, resName)
  34. {
  35. var res = new Resource();
  36. return res.load(user, project, resName);
  37. }
  38. createResource(user, project, res)
  39. {
  40. var old = this.getResource(user, project, res);
  41. if(E.check(old))
  42. return E.PROJECT_EXISTS;
  43. var p = new Resource();
  44. p.name=res;
  45. p.save(global.pathToResourceFile(user, project, res));
  46. this.resources[res] = true;
  47. this.save();
  48. return E.SUCCESS;
  49. }
  50. renameResource(user, project, oldName, newName)
  51. {
  52. var n = this.getResource(user, project, oldName);
  53. n.name=newName;
  54. var dir = global.pathToResourceDir(user, project);
  55. var oldPath = global.pathToResourceFile(user, project, oldName);
  56. var newPath = global.pathToResourceFile(user, project, newName);
  57. try {
  58. utils.mv(oldPath, newPath);
  59. if(this.location!=null)
  60. {
  61. var oldResPath=this.location.path;
  62. this.location.path=dir+newName+this.location.ext;
  63. utils.mv(oldResPath, this.location.path);
  64. }
  65. } catch (e) {
  66. console.log(e);
  67. return E.UNKNWON;
  68. }
  69. delete this.resources[oldName];
  70. this.resources[newName] = true;
  71. n.save(newPath);
  72. this.save(global.pathToProjectFile(user, project));
  73. return E.SUCCESS;
  74. }
  75. removeResource(user, project, res)
  76. {
  77. var x = this.getResource(user, project, res);
  78. if(!E.check(x))
  79. return E.BAD_RES;
  80. delete this.resources[res];
  81. utils.rm(global.pathToResourceFile(user, project, res));
  82. if(this.location!=null)
  83. utils.rm(this.location.path);
  84. this.save(global.pathToProjectFile(user, project));
  85. return E.SUCCESS;
  86. }
  87. editResource(user, project, name, data)
  88. {
  89. if(data.name!=undefined && data.name!=name)
  90. {
  91. var t = this.renameResource(user, project, name, data.name);
  92. if( !E.check(t) )
  93. return t;
  94. name=data.name;
  95. }
  96. var res = this.getResource(user, project, name);
  97. if(!E.check(res))
  98. return res;
  99. Object.assign(res, data);
  100. res.save(global.pathToResourceFile(user, project, name));
  101. return E.SUCCESS;
  102. }
  103. }