diff --git a/.gitignore b/.gitignore index 256aef4..11ee2f7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ node_modules dump.rdb .DS_Store +test/dist diff --git a/Makefile b/Makefile index e93bce1..baeec33 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ TESTS = test/*.test.js -REPORTER = tap +REPORTER = spec TIMEOUT = 3000 MOCHA_OPTS = diff --git a/index.js b/index.js index 82bd92d..bc14992 100644 --- a/index.js +++ b/index.js @@ -31,8 +31,15 @@ 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 }; @@ -40,6 +47,7 @@ Client.prototype.upload = function* (filepath, options) { Client.prototype.uploadBuffer = function* (content, options) { var filepath = this._getpath(options.key); + yield ensureDirExists(filepath); yield fs.writeFile(filepath, content); return { key: options.key }; }; @@ -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); }; diff --git a/package.json b/package.json index a54f87f..5150447 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/index.test.js b/test/index.test.js index 3b6bb86..f9b486b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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'); }); }); @@ -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); });