Skip to content

Commit

Permalink
feat: support nested dir
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 3, 2015
1 parent 16b0a1a commit 0f93417
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ node_modules

dump.rdb
.DS_Store
test/dist
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TESTS = test/*.test.js
REPORTER = tap
REPORTER = spec
TIMEOUT = 3000
MOCHA_OPTS =

Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,23 @@ function Client(options) {
mkdirp.sync(this.dir);
}

function ensureDirExists(filepath) {
return function (callback) {
mkdirp(path.dirname(filepath), callback);
};
}

Client.prototype.upload = function* (filepath, options) {
var destpath = this._getpath(options.key);
yield ensureDirExists(destpath);
var content = yield fs.readFile(filepath);
yield fs.writeFile(destpath, content);
return { key: options.key };
};

Client.prototype.uploadBuffer = function* (content, options) {
var filepath = this._getpath(options.key);
yield ensureDirExists(filepath);
yield fs.writeFile(filepath, content);
return { key: options.key };
};
Expand All @@ -60,6 +68,5 @@ Client.prototype.remove = function* (key) {
*/

Client.prototype._getpath = function (key) {
key = key.replace(/\//g, '-').replace(/\\/g, '_');
return path.join(this.dir, key);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"devDependencies": {
"autod": "~0.3.2",
"co-mocha": "0.0.4",
"co-mocha": "~0.0.4",
"istanbul-harmony": "^0.2.9",
"jshint": "^2.5.3",
"mocha": "^1.21.4",
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ describe('fs-cnpm', function () {
var res = yield client.uploadBuffer('hello', {key: 'hello'});
res.key.should.equal('hello');
(yield fs.readFile(path.join(dir, 'hello'), 'utf8')).should.equal('hello');

var res = yield client.uploadBuffer('hello', {key: '/a/b/c/d/e/f/g.txt'});
res.key.should.equal('/a/b/c/d/e/f/g.txt');
(yield fs.readFile(path.join(dir, '/a/b/c/d/e/f/g.txt'), 'utf8')).should.equal('hello');

var res = yield client.uploadBuffer('hello', {key: 'foo/-/foo-1.3.2.txt'});
res.key.should.equal('foo/-/foo-1.3.2.txt');
(yield fs.readFile(path.join(dir, 'foo/-/foo-1.3.2.txt'), 'utf8')).should.equal('hello');
});
});

Expand All @@ -66,6 +74,7 @@ describe('fs-cnpm', function () {
it('should remove ok', function* () {
yield client.remove('hello');
yield client.remove('hello1');
yield client.remove('foo/-/foo-1.3.2.txt');
(yield fs.exists(path.join(dir, 'hello'))).should.equal(false);
(yield fs.exists(path.join(dir, 'hello1'))).should.equal(false);
});
Expand Down

0 comments on commit 0f93417

Please sign in to comment.