mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-05-20 06:12:55 +08:00
initial config refactor
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
var fs = require('fs')
|
||||
, path = require('path')
|
||||
, Resource = require('./resource');
|
||||
, Resource = require('./resource')
|
||||
, ignore = {
|
||||
'public': true,
|
||||
'data': true,
|
||||
'resources': true,
|
||||
'.dpd': true
|
||||
}
|
||||
, sh = require('shelljs');
|
||||
|
||||
|
||||
/*!
|
||||
* Loads resources from a project folder
|
||||
@@ -10,21 +18,60 @@ var fs = require('fs')
|
||||
* @param {Function} callback
|
||||
*/
|
||||
module.exports.loadConfig = function(basepath, fn) {
|
||||
var resourcesPath = path.join(basepath, '/app.dpd');
|
||||
|
||||
fs.readFile(resourcesPath, 'utf-8', function(err, data) {
|
||||
if (err) { return fn(err); }
|
||||
|
||||
var jsonData
|
||||
, error;
|
||||
|
||||
try {
|
||||
jsonData = JSON.parse(data);
|
||||
} catch (ex) {
|
||||
error = ex;
|
||||
var remaining = 0
|
||||
, resources = {}
|
||||
, error;
|
||||
|
||||
function done() {
|
||||
if(!remaining) {
|
||||
if(error) return fn(error);
|
||||
fn(null, resources);
|
||||
remaining = -1;
|
||||
}
|
||||
}
|
||||
|
||||
fs.readdir(basepath, function (err, dir) {
|
||||
if(dir && dir.length) {
|
||||
dir.forEach(function (file) {
|
||||
remaining++;
|
||||
fs.stat(basepath + '/' + file, function (err, stat) {
|
||||
remaining--;
|
||||
error = err;
|
||||
if(stat && stat.isDirectory()) {
|
||||
var spath = basepath + '/' + file + '/' + 'settings.json'
|
||||
, resource = file;
|
||||
|
||||
fn(error, jsonData);
|
||||
if(!ignore[file]) {
|
||||
fs.exists(spath, function (exists) {
|
||||
if(exists) {
|
||||
remaining++;
|
||||
fs.readFile(spath, 'utf-8', function(err, data) {
|
||||
remaining--;
|
||||
var settings;
|
||||
if(err) error = err;
|
||||
try {
|
||||
settings = JSON.parse(data);
|
||||
} catch(e) {
|
||||
error = e;
|
||||
return fn(error);
|
||||
}
|
||||
|
||||
resources[resource] = settings;
|
||||
done();
|
||||
});
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
fn(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -38,10 +85,41 @@ module.exports.loadConfig = function(basepath, fn) {
|
||||
*/
|
||||
module.exports.saveConfig = function(resources, basepath, fn) {
|
||||
var resourcesPath = path.join(basepath, '/app.dpd')
|
||||
, json = JSON.stringify(resources, null, '\t');
|
||||
, remaining = 0
|
||||
, json;
|
||||
|
||||
fs.writeFile(resourcesPath, json, 'utf-8', function(err) {
|
||||
if (err) return fn(err);
|
||||
fn();
|
||||
});
|
||||
if(resources) {
|
||||
Object.keys(resources).forEach(function (id) {
|
||||
var r = resources[id]
|
||||
, rpath = basepath + r.path;
|
||||
|
||||
remaining++;
|
||||
|
||||
function save(err) {
|
||||
if(err) return fn(err);
|
||||
json = JSON.stringify(r, null, '\t');
|
||||
fs.writeFile(rpath + '/settings.json', json, 'utf-8', function(err) {
|
||||
if (err) return fn(err);
|
||||
if(--remaining == 0) {
|
||||
remaining = -1;
|
||||
fn();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fs.exists(rpath, function(exists) {
|
||||
if(exists) {
|
||||
save();
|
||||
} else {
|
||||
fs.mkdir(rpath, save);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
if(!remaining) fn();
|
||||
};
|
||||
|
||||
module.exports.remove = function (basepath, path, fn) {
|
||||
fn(sh.rm('-r', basepath + path));
|
||||
}
|
||||
|
||||
@@ -130,7 +130,8 @@ InternalResources.prototype.handle = function(ctx, next) {
|
||||
switch (ctx.req.method) {
|
||||
case 'POST':
|
||||
resource = ctx.body;
|
||||
id = uuid.create();
|
||||
// id = uuid.create();
|
||||
id = ctx.url;
|
||||
resources[id] = resource;
|
||||
|
||||
config.saveConfig(resources, basepath, function(err) {
|
||||
@@ -174,9 +175,13 @@ InternalResources.prototype.handle = function(ctx, next) {
|
||||
}
|
||||
var r = resources[id];
|
||||
delete resources[id];
|
||||
config.saveConfig(resources, basepath, function(err) {
|
||||
notifyType(r, ctx, ctx.done);
|
||||
config.remove(basepath, r.path, function (err) {
|
||||
if(err) return ctx.done(err);
|
||||
config.saveConfig(resources, basepath, function(err) {
|
||||
notifyType(r, ctx, ctx.done);
|
||||
});
|
||||
});
|
||||
|
||||
break;
|
||||
default:
|
||||
next();
|
||||
|
||||
@@ -25,18 +25,21 @@ describe('config-loader', function() {
|
||||
, 'test': 'value'
|
||||
};
|
||||
|
||||
fs.writeFileSync(path.join(basepath, '/app.dpd'), JSON.stringify({'123': resource1, '456': resource2}));
|
||||
// fs.writeFileSync(path.join(basepath, '/app.dpd'), JSON.stringify({'123': resource1, '456': resource2}));
|
||||
|
||||
configLoader.loadConfig(basepath, function(err, resources) {
|
||||
expect(Object.keys(resources)).to.have.length(2);
|
||||
expect(resources['123'].path).equal('/foo');
|
||||
expect(resources['123'].type).equal('Collection');
|
||||
expect(resources['123'].property).equal('value');
|
||||
expect(resources['456'].path).equal('/bar');
|
||||
expect(resources['456'].type).equal('Collection');
|
||||
expect(resources['456'].test).equal('value');
|
||||
|
||||
done();
|
||||
configLoader.saveConfig({'123': resource1, '456': resource2}, basepath, function(err) {
|
||||
configLoader.loadConfig(basepath, function(err, resources) {
|
||||
|
||||
expect(Object.keys(resources)).to.have.length(2);
|
||||
expect(resources['foo'].path).equal('/foo');
|
||||
expect(resources['foo'].type).equal('Collection');
|
||||
expect(resources['foo'].property).equal('value');
|
||||
expect(resources['bar'].path).equal('/bar');
|
||||
expect(resources['bar'].type).equal('Collection');
|
||||
expect(resources['bar'].test).equal('value');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -57,11 +60,12 @@ describe('config-loader', function() {
|
||||
configLoader.saveConfig({'123': resource1, '456': resource2}, basepath, function(err) {
|
||||
var resourcePath = path.join(basepath, '/app.dpd');
|
||||
|
||||
var resources = JSON.parse(fs.readFileSync(resourcePath));
|
||||
configLoader.loadConfig(basepath, function(err, resources) {
|
||||
expect(Object.keys(resources)).to.have.length(2);
|
||||
expect(resources.foo).to.eql(resource1);
|
||||
expect(resources.bar).to.eql(resource2);
|
||||
});
|
||||
|
||||
expect(Object.keys(resources)).to.have.length(2);
|
||||
expect(resources['123']).to.eql(resource1);
|
||||
expect(resources['456']).to.eql(resource2);
|
||||
|
||||
done(err);
|
||||
});
|
||||
|
||||
@@ -42,6 +42,12 @@ describe('resources', function(){
|
||||
describe('InternalResources', function() {
|
||||
describe('.handle(ctx)', function() {
|
||||
beforeEach(function(done) {
|
||||
// reset
|
||||
sh.rm('-rf', __dirname + '/support/proj');
|
||||
if(!sh.test('-d', __dirname + '/support/proj')) {
|
||||
sh.mkdir(__dirname + '/support/proj');
|
||||
}
|
||||
|
||||
this.ir = new InternalResources({path: '/__resources', configPath: configPath}, {});
|
||||
config.saveConfig({}, configPath, function(err) {
|
||||
done(err);
|
||||
@@ -75,18 +81,18 @@ describe('InternalResources', function() {
|
||||
}});
|
||||
});
|
||||
|
||||
it('should updating a resource when handling a PUT request', function(done) {
|
||||
it('should update a resource when handling a PUT request', function(done) {
|
||||
var r = {path: '/foo', type: 'Bar', val: 1};
|
||||
var test = this;
|
||||
|
||||
config.saveConfig({'123': r}, configPath, function(err) {
|
||||
config.saveConfig({'foo': r}, configPath, function(err) {
|
||||
|
||||
r.val = 2;
|
||||
test.ir.handle({req: {method: 'PUT', url: '/__resources/123', isRoot: true}, url: '/123', body: r, done: function() {
|
||||
test.ir.handle({req: {method: 'PUT', url: '/__resources/foo', isRoot: true}, url: '/foo', body: r, done: function() {
|
||||
|
||||
config.loadConfig(configPath, function(err, resourceList) {
|
||||
expect(Object.keys(resourceList)).to.have.length(1);
|
||||
expect(resourceList['123'].val).to.equal(2);
|
||||
expect(resourceList['foo'].val).to.equal(2);
|
||||
done();
|
||||
});
|
||||
}}, function() {
|
||||
@@ -100,7 +106,7 @@ describe('InternalResources', function() {
|
||||
, q2 = {path: '/bar', type: 'Bar'}
|
||||
, test = this;
|
||||
|
||||
config.saveConfig({'123': q, '456': q2}, configPath, function() {
|
||||
config.saveConfig({'foo': q, 'bar': q2}, configPath, function() {
|
||||
test.ir.handle({req: {method: 'GET', url: '/__resources', isRoot: true}, url: '/', done: function(err, result) {
|
||||
expect(result).to.have.length(2);
|
||||
result.forEach(function(r) {
|
||||
@@ -118,8 +124,8 @@ describe('InternalResources', function() {
|
||||
, q2 = {path: '/bar', type: 'Bar'}
|
||||
, test = this;
|
||||
|
||||
config.saveConfig({'123': q, '456': q2}, configPath, function() {
|
||||
test.ir.handle({req: {method: 'GET', url: '/__resources/456', isRoot: true}, url: '/456', done: function(err, result) {
|
||||
config.saveConfig({'foo': q, 'bar': q2}, configPath, function() {
|
||||
test.ir.handle({req: {method: 'GET', url: '/__resources/bar', isRoot: true}, url: '/bar', done: function(err, result) {
|
||||
expect(result).to.eql(q2);
|
||||
done();
|
||||
}}, function() {
|
||||
@@ -133,8 +139,8 @@ describe('InternalResources', function() {
|
||||
, q2 = {path: '/bar', type: 'Bar'}
|
||||
, test = this;
|
||||
|
||||
config.saveConfig({'123': q, '456': q2}, configPath, function() {
|
||||
test.ir.handle({req: {method: 'DELETE', url: '/__resources/456', isRoot: true}, url: '/456', done: function() {
|
||||
config.saveConfig({'foo': q, 'bar': q2}, configPath, function() {
|
||||
test.ir.handle({req: {method: 'DELETE', url: '/__resources/bar', isRoot: true}, url: '/bar', done: function() {
|
||||
config.loadConfig(configPath, function(err, result) {
|
||||
expect(Object.keys(result)).to.have.length(1);
|
||||
done(err);
|
||||
|
||||
@@ -9,7 +9,6 @@ describe('Server', function() {
|
||||
var server = new Server()
|
||||
, defaultOptions = {
|
||||
port: 2403,
|
||||
// host: 'localhost',
|
||||
db: {
|
||||
name: 'deployd',
|
||||
port: 27017,
|
||||
|
||||
@@ -8,6 +8,7 @@ http = require('http');
|
||||
TEST_DB = {name: 'test-db', host: 'localhost', port: 27017};
|
||||
mongodb = require('mongodb');
|
||||
var Stream = require('stream');
|
||||
sh = require('shelljs');
|
||||
|
||||
// request mock
|
||||
var port = 7000;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"123": {
|
||||
"path": "/foo",
|
||||
"type": "Bar"
|
||||
}
|
||||
}
|
||||
4
test/support/proj/foo/settings.json
Normal file
4
test/support/proj/foo/settings.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"path": "/foo",
|
||||
"type": "Bar"
|
||||
}
|
||||
Reference in New Issue
Block a user