File tree 10 files changed +184
-5
lines changed 10 files changed +184
-5
lines changed Original file line number Diff line number Diff line change 1
1
/node_modules /
2
2
/coverage /
3
+ /test /tmp /*
4
+ ! /test /tmp /.keep
Original file line number Diff line number Diff line change 6
6
7
7
var argv = require ( 'argvee' ) ( ) ;
8
8
9
+ /**
10
+ * Internal dependencies.
11
+ */
12
+
13
+ var Db = require ( '../lib/db' ) ;
14
+ var Todos = require ( '../lib/collection' ) ;
15
+
9
16
/**
10
17
* Primary command.
11
18
*/
12
19
13
20
var command = argv . commands . shift ( ) ;
14
21
15
22
/**
16
- * Arguments .
23
+ * Todo items .
17
24
*/
18
25
19
- var item = argv . commands . join ( ' ' ) ;
26
+ var todos = new Todos ( new Db ( '/tmp/todos.txt' ) ) ;
20
27
21
- if ( ! item ) {
22
- console . error ( 'todo: please enter a todo item' ) ;
28
+ process . on ( 'uncaughtException' , function ( err ) {
29
+ console . error ( 'todo: ' + err . message ) ;
23
30
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 ) ;
24
41
}
Original file line number Diff line number Diff line change @@ -32,13 +32,15 @@ module.exports = function(hydro) {
32
32
chai : {
33
33
styles : 'should' ,
34
34
stack : true ,
35
+ plugins : [ 'jack-chai' ] ,
35
36
} ,
36
37
globals : {
37
38
cli : cli ,
38
39
} ,
39
40
plugins : [
40
41
'hydro-bdd' ,
41
42
'hydro-chai' ,
43
+ 'hydro-jack'
42
44
] ,
43
45
tests : [
44
46
'test/*.js' ,
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change 11
11
"devDependencies" : {
12
12
"chai" : " *" ,
13
13
"nixt" : " *" ,
14
+ "jack" : " git://github.com/jackjs/jack.git#master" ,
15
+ "jack-chai" : " ~0.1.0" ,
16
+ "istanbul" : " ~0.2.3" ,
14
17
"hydro" : " *" ,
15
18
"hydro-doc" : " 0.0.2" ,
16
19
"hydro-bdd" : " ~0.1.0" ,
17
20
"hydro-chai" : " ~0.1.3" ,
18
- "istanbul " : " ~0.2.3 "
21
+ "hydro-jack " : " 0.0.1 "
19
22
},
20
23
"repository" : {
21
24
"type" : " git" ,
Original file line number Diff line number Diff line change
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 ( / h a v e m o r e f u n / )
7
+ . code ( 0 )
8
+ . end ( done ) ;
9
+ } ) ;
10
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments