Permalink
Browse files

If env is nodester, store files elsewhere due to permissions bug

Also fix bug with invalid name detection
  • Loading branch information...
1 parent 8604887 commit 666c209d5a80d142e9bda4e916132c187e4fe66c @wachunga committed Feb 25, 2012
Showing with 144 additions and 134 deletions.
  1. +49 −51 lib/issueDao.js
  2. +77 −74 lib/projectDao.js
  3. +8 −8 lib/tracker.js
  4. +10 −1 server.js
View
100 lib/issueDao.js
@@ -2,65 +2,63 @@ var fs = require('fs'),
_ = require('underscore'),
Issue = require('./Issue');
-var issueDao = (function () {
+var issueDao = module.exports = {};
- function getIssueFileFromProject(project) {
- return __dirname + '/../db/' + project.slug + '.json';
- }
-
- var issues = {};
+var home = '';
+var issues = {};
- return {
+issueDao.init = function (dir) {
+ home = dir;
+ console.log('storing issues in: ' + home);
+};
- load: function (project) {
- var issueFile = getIssueFileFromProject(project);
- console.log('Loading issues from ' + issueFile);
- try {
- var issueJSON = fs.readFileSync(issueFile);
- issues[project.slug] = Issue.applyDefaults(JSON.parse(issueJSON.toString()));
- } catch (e) {
- console.log("Issue file does not appear to exist yet, but that's okay. It'll get created with the first issue.", e);
- }
- return issues[project.slug] || [];
- },
+function getIssueFileFromProject(project) {
+ return home + project.slug + '.json';
+}
- add: function (issue, project) {
- if (!issues[project.slug]) {
- issues[project.slug] = [];
- }
+issueDao.load = function (project) {
+ var issueFile = getIssueFileFromProject(project);
+ console.log('Loading issues from ' + issueFile);
+ try {
+ var issueJSON = fs.readFileSync(issueFile);
+ issues[project.slug] = Issue.applyDefaults(JSON.parse(issueJSON.toString()));
+ } catch (e) {
+ console.log("Issue file does not appear to exist yet, but that's okay. It'll get created with the first issue.", e);
+ }
+ return issues[project.slug] || [];
+};
- issue.id = issues[project.slug].length + 1;
- issues[project.slug].push(issue);
- this.write(project);
- return issue;
- },
+issueDao.add = function (issue, project) {
+ if (!issues[project.slug]) {
+ issues[project.slug] = [];
+ }
- find: function (id, project) {
- return issues[project.slug][id-1];
- },
+ issue.id = issues[project.slug].length + 1;
+ issues[project.slug].push(issue);
+ this.write(project);
+ return issue;
+};
- update: function (issue, project) {
- issues[project.slug][issue.id-1] = issue;
- this.write(project); // TODO: only persist updated
- },
+issueDao.find = function (id, project) {
+ return issues[project.slug][id-1];
+};
- write: function (project) {
- var issueFile = getIssueFileFromProject(project);
- fs.writeFile(issueFile, JSON.stringify(issues[project.slug]), function (err) {
- if (err) {
- console.log("Couldn't write issues file.");
- throw err;
- }
- });
- },
+issueDao.update = function (issue, project) {
+ issues[project.slug][issue.id-1] = issue;
+ this.write(project); // TODO: only persist updated
+};
- reset: function (project) {
- issues[project.slug] = [];
- this.write(project);
+issueDao.write = function (project) {
+ var issueFile = getIssueFileFromProject(project);
+ fs.writeFile(issueFile, JSON.stringify(issues[project.slug]), function (err) {
+ if (err) {
+ console.log("Couldn't write issues file.");
+ throw err;
}
+ });
+};
- };
-
-})();
-
-module.exports = issueDao;
+issueDao.reset = function (project) {
+ issues[project.slug] = [];
+ this.write(project);
+};
View
151 lib/projectDao.js
@@ -2,82 +2,85 @@ var fs = require('fs'),
Project = require('./project'),
_ = require('underscore');
-var projectDao = (function () {
+var projectDao = module.exports = {};
- var FILENAME = 'projects';
+var FILENAME = 'projects';
- var projectsFile = __dirname + '/../db/' + FILENAME + '.json';
- var projects = load() || {};
+var projectsFile;
+var projects = {};
- function load() {
- try {
- var json = fs.readFileSync(projectsFile);
- return Project.applyDefaults(JSON.parse(json.toString()));
- } catch (e) {
- console.log("Project file " + projectsFile + " appears to not yet exist, but that's okay. It'll get created with the first project.", e);
- }
- }
-
- return {
-
- filename: FILENAME,
-
- getSlug: function (name) {
- return Project.slugify(name);
- },
-
- create: function (name, unlisted) {
- var project = new Project(name, unlisted);
- projects[project.slug] = project;
- console.log('Created new project', project);
- this.write();
- return project;
- },
-
- write: function () {
- fs.writeFile(projectsFile, JSON.stringify(projects), function (err) {
- if (err) {
- console.log("Couldn't write projects file.");
- throw err;
- }
- });
- },
-
- remove: function (slug) {
- var project = projects[slug];
- if (!project) {
- return false;
- }
- projects[slug].deleted = true;
- this.write();
- return true;
- },
-
- exists: function (name) {
- return !!this.find(this.getSlug(name));
- },
-
- find: function (slug) {
- return projects[slug];
- },
-
- findAll: function () {
- return _.values(projects);
- },
-
- findListed: function () {
- return _.filter(projects, function (project) {
- return !project.unlisted && !project.deleted;
- });
- },
-
- findUnlisted: function () {
- return _.filter(projects, function (project) {
- return project.unlisted && !project.deleted;
- });
- }
+projectDao.init = function (dir) {
+ projectsFile = dir + FILENAME + '.json';
+ console.log('storing projects in: ' + projectsFile);
- };
-})();
+ projects = load() || {};
+};
-module.exports = projectDao;
+function load() {
+ try {
+ var json = fs.readFileSync(projectsFile);
+ return Project.applyDefaults(JSON.parse(json.toString()));
+ } catch (e) {
+ console.log("Project file " + projectsFile + " appears to not yet exist, but that's okay. It'll get created with the first project.", e);
+ }
+}
+
+projectDao.isValidName = function (name) {
+ return name !== FILENAME;
+};
+
+projectDao.getSlug = function (name) {
+ return Project.slugify(name);
+};
+
+projectDao.create = function (name, unlisted) {
+ var project = new Project(name, unlisted);
+ projects[project.slug] = project;
+ console.log('Created new project', project);
+ this.write();
+ return project;
+};
+
+projectDao.write = function () {
+ fs.writeFile(projectsFile, JSON.stringify(projects), function (err) {
+ if (err) {
+ console.log("Couldn't write projects file.");
+ throw err;
+ }
+ });
+ console.log('wrote projects');
+};
+
+projectDao.remove = function (slug) {
+ var project = projects[slug];
+ if (!project) {
+ return false;
+ }
+ projects[slug].deleted = true;
+ this.write();
+ return true;
+};
+
+projectDao.exists = function (name) {
+ return !!this.find(this.getSlug(name));
+};
+
+projectDao.find = function (slug) {
+ return projects[slug];
+};
+
+projectDao.findAll = function () {
+ return _.values(projects);
+};
+
+projectDao.findListed = function () {
+ return _.filter(projects, function (project) {
+ return !project.unlisted && !project.deleted;
+ });
+};
+
+projectDao.findUnlisted = function () {
+ return _.filter(projects, function (project) {
+ return project.unlisted && !project.deleted;
+ });
+};
View
16 lib/tracker.js
@@ -1,11 +1,11 @@
-var sio = require('socket.io');
-var _ = require('underscore');
-
-var Issue = require('./Issue');
-var issueDao = require('./issueDao');
-var projectDao = require('./projectDao');
-var historyDao = require('./historyDao');
-var ET = require('../public/js/omegaEvent').OmegaEvent.Type;
+var sio = require('socket.io'),
+ _ = require('underscore'),
+ Issue = require('./Issue'),
+
+ issueDao = require('./issueDao'),
+ projectDao = require('./projectDao'),
+ historyDao = require('./historyDao'),
+ ET = require('../public/js/omegaEvent').OmegaEvent.Type;
var tracker = (function () {
View
11 server.js
@@ -2,6 +2,7 @@
var express = require('express'),
_ = require('underscore'),
+ issueDao = require('./lib/issueDao'),
projectDao = require('./lib/projectDao'),
tracker = require('./lib/tracker');
@@ -26,6 +27,13 @@ var PORT = process.env.app_port || argv.port;
// build 'public-built/' using 'node r.js -o app.build.js'
var www_public = argv.optimized ? '/public-built' : '/public';
+var db_dir = __dirname + '/db/';
+if (process.env['NODE_ENV'] === 'nodester') {
+ db_dir = __dirname + '/'; // override due to https://github.com/nodester/nodester/issues/313
+}
+projectDao.init(db_dir);
+issueDao.init(db_dir);
+
var app = express.createServer(
express.logger(),
express.cookieParser(),
@@ -48,8 +56,9 @@ app.post('/project', function (req, res) {
if (!name) {
res.json({ error: 'empty' }, 400);
return;
- } else if (name === projectDao.filename) {
+ } else if (!projectDao.isValidName(name)) {
res.json({ error: 'invalid' }, 400);
+ return;
}
if (projectDao.exists(name)) {

0 comments on commit 666c209

Please sign in to comment.