|
| 1 | +var fs = require('fs'); |
| 2 | +var util = require('util'); |
| 3 | +var async = require('async'); |
| 4 | + |
| 5 | +var getDateString = function( d ) { |
| 6 | + var now = new Date(); |
| 7 | + var twodigits = function( x ) { |
| 8 | + return x<10 ? '0' + x: x; |
| 9 | + }; |
| 10 | + return d.getFullYear() + "-" + twodigits(d.getMonth()+1) + '-' + twodigits(d.getDate()); |
| 11 | +}; |
| 12 | + |
| 13 | +// Required methods : |
| 14 | +// App.prototype.load() |
| 15 | +// App.prototype.require() |
| 16 | +// App.prototype.view() |
| 17 | +// App.name |
| 18 | +// App.metadata |
| 19 | +// App.staticURL |
| 20 | +// App.appURL |
| 21 | + |
| 22 | +var App = exports.App = function(name) { |
| 23 | + this.name = name; |
| 24 | + this.metadata = { |
| 25 | + created: getDateString( new Date() ), |
| 26 | + modified: getDateString( new Date() ), |
| 27 | + color: "#1abc9c", |
| 28 | + author: "Coder", |
| 29 | + name: name, |
| 30 | + hidden: false, |
| 31 | + public: false |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +var LocalApp = exports.LocalApp = function(name) { |
| 37 | + LocalApp.super_.call(this, name); |
| 38 | + |
| 39 | + this.staticURL = "/static/apps/" + name; |
| 40 | + this.appURL = "/app/" + name; |
| 41 | + |
| 42 | + this.rootPath = LocalApp.appdir + name, |
| 43 | + this.metafile = LocalApp.appdir + name + "/app/meta.json"; |
| 44 | + this.loadpath = LocalApp.appdir + name + "/app/index"; |
| 45 | + this.viewpath = "apps/" + name + "/"; |
| 46 | +} |
| 47 | + |
| 48 | +util.inherits(LocalApp, App); |
| 49 | +LocalApp.appcache = {} |
| 50 | +LocalApp.appdir = process.cwd() + "/apps/"; |
| 51 | + |
| 52 | +LocalApp.prototype.load = function(callback) { |
| 53 | + callback = callback || function() {}; |
| 54 | + var self = this; |
| 55 | + |
| 56 | + fs.readFile(self.metafile, { encoding: "utf-8" }, function(err, data) { |
| 57 | + if (err) { |
| 58 | + callback(err); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + var data = JSON.parse(data); |
| 63 | + for (attr in self.metadata) { |
| 64 | + |
| 65 | + if (typeof data[attr] !== 'undefined') { |
| 66 | + self.metadata[attr] = data[attr]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + callback(null); |
| 71 | + }); |
| 72 | +}; |
| 73 | + |
| 74 | +LocalApp.prototype.require = function() { |
| 75 | + return require(this.loadpath); |
| 76 | +}; |
| 77 | + |
| 78 | +LocalApp.prototype.view = function(name) { |
| 79 | + if (!name) |
| 80 | + name = "index"; |
| 81 | + return this.viewpath + name; |
| 82 | +}; |
| 83 | + |
| 84 | +LocalApp.prototype.save = function(callback) { |
| 85 | + var data = JSON.stringify(this.metadata); |
| 86 | + |
| 87 | + fs.writeFile(this.metafile, data, { encoding: "utf-8" }, function (err) { |
| 88 | + if (callback) callback(err); |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +LocalApp.prototype.invalidate = function() { |
| 93 | + var cached = require.cache[this.loadpath + '.js']; |
| 94 | + if ( cached ) { |
| 95 | + if ( cached.on_destroy ) { |
| 96 | + cached.on_destroy(); |
| 97 | + } |
| 98 | + delete require.cache[this.loadpath + ".js"]; |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +LocalApp.prototype.remove = function(cb) { |
| 103 | + this.invalidate(); |
| 104 | + delete LocalApp.appcache[name]; |
| 105 | + |
| 106 | + rimraf(this.rootPath, function (err) { |
| 107 | + if (err) { |
| 108 | + cb(err); |
| 109 | + } |
| 110 | + async.each([ process.cwd() + "/views/apps/" + name, process.cwd() + "/static/apps/" + name ], fs.unlink, function(err) { |
| 111 | + cb (err); |
| 112 | + }); |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +LocalApp.find = function(name, callback) { |
| 117 | + callback = callback || function() {}; |
| 118 | + |
| 119 | + if (LocalApp.appcache[name]) { |
| 120 | + callback(null, LocalApp.appcache[name]); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + var userapp = new LocalApp(name); |
| 125 | + userapp.load(function(err) { |
| 126 | + if (err) { |
| 127 | + callback(err, null); |
| 128 | + } |
| 129 | + else { |
| 130 | + LocalApp.appcache[name] = userapp; |
| 131 | + callback(null, userapp); |
| 132 | + } |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +LocalApp.list = function(callback, hidden) { |
| 137 | + fs.readdir(LocalApp.appdir, function(err, files) { |
| 138 | + if (err) { |
| 139 | + callback(err, null); |
| 140 | + } |
| 141 | + else { |
| 142 | + var apps = async.map(files, |
| 143 | + function(name, callback) { |
| 144 | + LocalApp.find(name, function(err, app) { |
| 145 | + // Don't propagate the error. If there is one, just pass a null value, it is filtered out later. |
| 146 | + callback(null, err ? null : app); |
| 147 | + }); |
| 148 | + }, |
| 149 | + function(err, apps) { |
| 150 | + if (err) { |
| 151 | + callback(err, null); |
| 152 | + } else { |
| 153 | + // Filter out the null ones and the ones we don't want (hidden, private, ...). |
| 154 | + async.filter(apps, |
| 155 | + function(item, callback) { |
| 156 | + callback(item && (hidden || !item.metadata.hidden)); |
| 157 | + }, |
| 158 | + function(apps) { |
| 159 | + callback(null, apps); |
| 160 | + }); |
| 161 | + } |
| 162 | + }); |
| 163 | + } |
| 164 | + }); |
| 165 | +} |
| 166 | + |
| 167 | +var createLinks = function(name, cb) { |
| 168 | + var apppath = "../../apps/" + name + "/"; |
| 169 | + var viewpath = process.cwd() + "/views/apps/" + name; |
| 170 | + var staticpath = process.cwd() + "/static/apps/" + name; |
| 171 | + |
| 172 | + fs.symlink(apppath + "views", viewpath, function(err) { |
| 173 | + if (err) { |
| 174 | + cb(err); |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + fs.symlink(apppath + "static", staticpath, function(err) { |
| 179 | + cb(err); |
| 180 | + }); |
| 181 | + }); |
| 182 | +} |
| 183 | + |
| 184 | +LocalApp.create = function(template, name, callback) { |
| 185 | + var appPath = process.cwd() + "/apps/" + name; |
| 186 | + var templatePath = path.resolve("apps/", template); |
| 187 | + |
| 188 | + ncp(templatePath, appPath, function(err) { |
| 189 | + if (err) { |
| 190 | + callback(err, null); |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + createLinks(name, function(err) { |
| 195 | + if (err) { |
| 196 | + callback(err, null); |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + LocalApp.find(name, callback); |
| 201 | + }); |
| 202 | + }); |
| 203 | +} |
| 204 | + |
0 commit comments