123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- var SavableData = require("./SavableData");
- var global = require('./global')
- var E = require("./Errors");
- var fs = require("fs");
- var Resource = require("./Resource");
- var utils = require('./utils');
- module.exports = class Project extends SavableData {
- constructor()
- {
- super();
- this.stages = [];
- this.map = "";
- this.name = "";
- this.exclude = ["basePath"];
- this.location = null;
- this.basePath = "";
- this.user=null;
- this.resources = {}
- }
- load(user, name)
- {
- this.name=name;
- this.basePath = global.pathToProjectDir(user, name);
- this.path = global.pathToProjectFile(user, name);
- try {
- var obj = JSON.parse(fs.readFileSync(this.path));
- if(obj!=null) Object.assign(this, obj);
- } catch (e) {
- return E.ENOENT;
- }
- return this;
- }
- getResource(user, project, resName)
- {
- var res = new Resource();
- return res.load(user, project, resName);
- }
- createResource(user, project, res)
- {
- var old = this.getResource(user, project, res);
- if(E.check(old))
- return E.PROJECT_EXISTS;
- var p = new Resource();
- p.name=res;
- p.save(global.pathToResourceFile(user, project, res));
- this.resources[res] = true;
- this.save();
- return E.SUCCESS;
- }
- renameResource(user, project, oldName, newName)
- {
- var n = this.getResource(user, project, oldName);
- n.name=newName;
- var dir = global.pathToResourceDir(user, project);
- var oldPath = global.pathToResourceFile(user, project, oldName);
- var newPath = global.pathToResourceFile(user, project, newName);
- try {
- utils.mv(oldPath, newPath);
- if(this.location!=null)
- {
- var oldResPath=this.location.path;
- this.location.path=dir+newName+this.location.ext;
- utils.mv(oldResPath, this.location.path);
- }
- } catch (e) {
- console.log(e);
- return E.UNKNWON;
- }
- delete this.resources[oldName];
- this.resources[newName] = true;
- n.save(newPath);
- this.save(global.pathToProjectFile(user, project));
- return E.SUCCESS;
- }
- removeResource(user, project, res)
- {
- var x = this.getResource(user, project, res);
- if(!E.check(x))
- return E.BAD_RES;
- delete this.resources[res];
- utils.rm(global.pathToResourceFile(user, project, res));
- if(this.location!=null)
- utils.rm(this.location.path);
- this.save(global.pathToProjectFile(user, project));
- return E.SUCCESS;
- }
- editResource(user, project, name, data)
- {
- if(data.name!=undefined && data.name!=name)
- {
- var t = this.renameResource(user, project, name, data.name);
- if( !E.check(t) )
- return t;
- name=data.name;
- }
- var res = this.getResource(user, project, name);
- if(!E.check(res))
- return res;
- Object.assign(res, data);
- res.save(global.pathToResourceFile(user, project, name));
- return E.SUCCESS;
- }
- }
|