Skip to content

Commit d40c2ef

Browse files
committed
Create & list todo items
1 parent a9d5fd8 commit d40c2ef

File tree

10 files changed

+184
-5
lines changed

10 files changed

+184
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/node_modules/
22
/coverage/
3+
/test/tmp/*
4+
!/test/tmp/.keep

bin/todo

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,36 @@
66

77
var argv = require('argvee')();
88

9+
/**
10+
* Internal dependencies.
11+
*/
12+
13+
var Db = require('../lib/db');
14+
var Todos = require('../lib/collection');
15+
916
/**
1017
* Primary command.
1118
*/
1219

1320
var command = argv.commands.shift();
1421

1522
/**
16-
* Arguments.
23+
* Todo items.
1724
*/
1825

19-
var item = argv.commands.join(' ');
26+
var todos = new Todos(new Db('/tmp/todos.txt'));
2027

21-
if (!item) {
22-
console.error('todo: please enter a todo item');
28+
process.on('uncaughtException', function(err) {
29+
console.error('todo: ' + err.message);
2330
process.exit(1);
31+
});
32+
33+
if ('ls' === command) {
34+
console.log(todos.all());
35+
process.exit(0);
36+
}
37+
38+
if ('add' === command) {
39+
var desc = argv.commands.join(' ');
40+
todos.create(desc);
2441
}

hydro.conf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ module.exports = function(hydro) {
3232
chai: {
3333
styles: 'should',
3434
stack: true,
35+
plugins: ['jack-chai'],
3536
},
3637
globals: {
3738
cli: cli,
3839
},
3940
plugins: [
4041
'hydro-bdd',
4142
'hydro-chai',
43+
'hydro-jack'
4244
],
4345
tests: [
4446
'test/*.js',

lib/collection.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Todo collection.
3+
*
4+
* @param {Object} database
5+
* @constructor
6+
*/
7+
8+
function Collection(db) {
9+
this.db = db;
10+
}
11+
12+
/**
13+
* Create a new todo item with `desc`.
14+
*
15+
* @param {String} todo description
16+
* @api public
17+
*/
18+
19+
Collection.prototype.create = function(desc) {
20+
if (!desc) throw new Error('please enter a todo item');
21+
this.db.write(desc);
22+
};
23+
24+
/**
25+
* Return all todo items.
26+
*
27+
* @returns {String}
28+
* @api public
29+
*/
30+
31+
Collection.prototype.all = function() {
32+
return this.db.read();
33+
};
34+
35+
/**
36+
* Primary export.
37+
*/
38+
39+
module.exports = Collection;

lib/db.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* External dependencies.
3+
*/
4+
5+
var fs = require('fs');
6+
7+
/**
8+
* Simple file-based database.
9+
*
10+
* @param {String} path to store the information
11+
* @constructor
12+
*/
13+
14+
function Db(path) {
15+
this.path = path;
16+
}
17+
18+
/**
19+
* Store given `str`.
20+
*
21+
* @param {String} str
22+
* @api public
23+
*/
24+
25+
Db.prototype.write = function(str) {
26+
fs.writeFileSync(this.path, str, 'utf8');
27+
};
28+
29+
/**
30+
* Return the contents of the database.
31+
*
32+
* @returns {String}
33+
* @api public
34+
*/
35+
36+
Db.prototype.read = function() {
37+
return fs.readFileSync(this.path, 'utf8');
38+
};
39+
40+
/**
41+
* Primary export.
42+
*/
43+
44+
module.exports = Db;

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
"devDependencies": {
1212
"chai": "*",
1313
"nixt": "*",
14+
"jack": "git://github.com/jackjs/jack.git#master",
15+
"jack-chai": "~0.1.0",
16+
"istanbul": "~0.2.3",
1417
"hydro": "*",
1518
"hydro-doc": "0.0.2",
1619
"hydro-bdd": "~0.1.0",
1720
"hydro-chai": "~0.1.3",
18-
"istanbul": "~0.2.3"
21+
"hydro-jack": "0.0.1"
1922
},
2023
"repository": {
2124
"type": "git",

test/acceptance/ls.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
describe('todo ls', function() {
2+
it('lists added todo items', function(done) {
3+
cli()
4+
.exec('./todo add have more fun')
5+
.run('ls')
6+
.stdout(/have more fun/)
7+
.code(0)
8+
.end(done);
9+
});
10+
});

test/collection.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var Collection = require('../lib/collection');
2+
var db = {};
3+
4+
describe('Collection', function() {
5+
describe('#create', function() {
6+
it('creates a new todo item', function() {
7+
var collection = new Collection(db);
8+
jack(db, 'write');
9+
collection.create('todo item');
10+
db.write.should.have.been.called.with.args('todo item');
11+
});
12+
13+
it('throws an error when called with no description', function() {
14+
var collection = new Collection;
15+
16+
should.throw(function() {
17+
collection.create('');
18+
}, Error, 'please enter a todo item');
19+
});
20+
});
21+
22+
describe('#all', function() {
23+
it('returns all todo items', function() {
24+
var collection = new Collection(db);
25+
jack(db, 'read', function() {
26+
return 'todos';
27+
});
28+
collection.all().should.eq('todos');
29+
});
30+
});
31+
});

test/db.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var fs = require('fs');
2+
var join = require('path').join;
3+
var Db = require('../lib/db');
4+
var tmp = join(__dirname, 'tmp', 'todos.json');
5+
6+
describe('db', function() {
7+
describe('#write', function() {
8+
it('stores information into a file', function() {
9+
var db = new Db(tmp);
10+
db.write('foo');
11+
fs.readFileSync(tmp, 'utf8').should.eq('foo');
12+
fs.unlinkSync(tmp);
13+
});
14+
15+
it('throws an error when it cannot save the file', function() {
16+
var db = new Db('/this/is/invalid/path/hopefully.json');
17+
18+
should.throw(function() {
19+
db.write('foo');
20+
});
21+
});
22+
});
23+
24+
describe('#read', function() {
25+
it('returns the entire database', function() {
26+
var db = new Db(tmp);
27+
fs.writeFileSync(tmp, 'todos', 'utf8');
28+
db.read().should.eq('todos');
29+
});
30+
});
31+
});

test/tmp/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)