Skip to content

Commit 1bac8b8

Browse files
committed
Full setup/reset by running setup.js
1 parent b8527ef commit 1bac8b8

File tree

4 files changed

+78
-66
lines changed

4 files changed

+78
-66
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"mysql": "0.9.5",
1515
"semver": "1.0.12",
1616
"sqlite3": "2.1.1",
17-
"step": "0.0.5"
17+
"step": "0.0.5",
18+
"rimraf": "1.0.9"
1819
},
1920
"engines": {
2021
"node": "0.6.5"

src/pluginsdb.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,45 @@ var pluginsDb = module.exports = {
8888

8989
getAllActions: auto(function( fn ) {
9090
db.all( "SELECT * FROM actions", fn );
91-
})
91+
}),
92+
93+
_reset: function( fn ) {
94+
var fs = require( "fs" ),
95+
Step = require( "step" );
96+
97+
Step(
98+
function() {
99+
fs.unlink( config.pluginsDb, this );
100+
},
101+
102+
function( error ) {
103+
if ( !error || error.code === "ENOENT" ) {
104+
return connect( this );
105+
}
106+
107+
fn( error );
108+
},
109+
110+
function( error ) {
111+
if ( error ) {
112+
return fn( error );
113+
}
114+
115+
db.run( "CREATE TABLE owners (" +
116+
"plugin TEXT PRIMARY KEY, " +
117+
"owner TEXT " +
118+
")", this.parallel() );
119+
120+
db.run( "CREATE TABLE actions (" +
121+
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
122+
"action TEXT, " +
123+
"data TEXT " +
124+
")", this.parallel() );
125+
},
126+
127+
function( error ) {
128+
fn( error );
129+
}
130+
);
131+
}
92132
};

src/setup.js

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,23 @@
1-
var fs = require( "fs" ),
2-
sqlite = require( "sqlite3" ),
3-
config = require( "./config" ),
4-
success = true;
5-
6-
try {
7-
fs.unlinkSync( config.pluginsDb );
8-
} catch( error ) {
9-
if ( error.code !== "ENOENT" ) {
10-
return logError( "Could not check status of, or delete, plugin.db " );
11-
}
12-
}
13-
14-
var db = new sqlite.Database( config.pluginsDb, function( error ) {
15-
if ( error ) {
16-
return logError( error, "Could not open database." );
17-
}
18-
19-
setup();
20-
});
21-
22-
function setup() {
23-
createOwnersTable(function( error ) {
24-
if ( error ) {
25-
return logError( error, "Could not create owners table." );
26-
}
27-
28-
console.log( "Created owners table." );
29-
});
30-
31-
createActionsTable(function( error ) {
1+
var Step = require( "step" ),
2+
rimraf = require( "rimraf" ),
3+
pluginsDb = require( "./pluginsdb" ),
4+
wordpress = require( "./wordpress" ),
5+
config = require( "./config" );
6+
7+
Step(
8+
function() {
9+
pluginsDb._reset( this.parallel() );
10+
wordpress._reset( this.parallel() );
11+
rimraf( config.repoDir, this.parallel() );
12+
},
13+
14+
function( error ) {
3215
if ( error ) {
33-
return logError( error, "Could not create actions table." );
16+
console.log( "ERROR", "Setup failed." );
17+
console.log( error, error.stack );
18+
return;
3419
}
3520

36-
console.log( "Created actions table." );
37-
});
38-
}
39-
40-
function logError( error, msg ) {
41-
console.log( "ERROR!", msg );
42-
console.log( error );
43-
success = false;
44-
}
45-
46-
function createOwnersTable( fn ) {
47-
db.run( "CREATE TABLE owners (" +
48-
"plugin TEXT PRIMARY KEY, " +
49-
"owner TEXT " +
50-
");", fn );
51-
}
52-
53-
function createActionsTable( fn ) {
54-
db.run( "CREATE TABLE actions (" +
55-
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
56-
"action TEXT, " +
57-
"data TEXT " +
58-
");", fn );
59-
}
60-
61-
process.on( "exit", function() {
62-
if ( success ) {
63-
console.log( "SUCCESS!", "Setup complete." );
64-
} else {
65-
console.log( "ERROR!", "Setup failed." );
21+
console.log( "SUCCESS", "Setup complete." );
6622
}
67-
});
23+
);

src/wordpress.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,20 @@ var wordpress = module.exports = {
261261
db.end();
262262
db = null;
263263
}
264-
}
264+
},
265+
266+
_reset: auto(function( fn ) {
267+
Step(
268+
function() {
269+
db.query( "TRUNCATE TABLE `" + postsTable + "`", this.parallel() );
270+
db.query( "TRUNCATE TABLE `" + postmetaTable + "`", this.parallel() );
271+
wordpress.flush( this.parallel() );
272+
},
273+
274+
function( error ) {
275+
wordpress.end();
276+
fn( error );
277+
}
278+
);
279+
})
265280
};

0 commit comments

Comments
 (0)