Skip to content

Commit f8638a5

Browse files
committed
add command init database
1 parent d18ce18 commit f8638a5

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ $ code-push login http://codepush.19910225.com:8080 #登录
3232

3333
[CodePushDemo](https://github.com/lisong/code-push-demo-app)
3434

35-
## INSTALL
35+
## INSTALL FROM NPM PACKAGE
3636

3737
```shell
38-
$ cd /path/to/code-push-server
39-
$ mysql -uroot -e"create database codepush default charset utf8;"
40-
$ mysql -uroot codepush < ./sql/codepush.sql
41-
$ mysql -uroot codepush < ./sql/codepush-v0.1.1.sql
42-
$ mysql -uroot codepush < ./sql/codepush-v0.1.5.sql
38+
$ npm install code-push-server -g
39+
$ code-push-server-db init --dbhost localhost --dbuser root --dbpassword #初始化数据库
40+
$ code-push-server #启动服务
41+
```
42+
43+
## INSTALL FROM SOURCE CODE
44+
45+
```shell
46+
$ git clone https://github.com/lisong/code-push-server.git
47+
$ cd code-push-server
48+
$ ./bin/db init --dbhost localhost --dbuser root --dbpassword
4349
$ npm install
4450
```
4551

bin/db

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
var fs = require('fs');
7+
var path = require('path');
8+
var yargs = require('yargs')
9+
.usage('Usage: $0 <command> [options]')
10+
.command('init', '初始化数据库')
11+
.example('$0 init --dbname codepush --dbhost localhost --dbuser root --dbpassword 123456', '初始化code-push-server数据库')
12+
.default({dbname: 'codepush', dbhost: 'localhost', dbuser: 'root', dbpassword: null})
13+
.help('h')
14+
.alias('h', 'help');
15+
var argv = yargs
16+
.argv;
17+
var command = argv._[0];
18+
var mysql = require('mysql');
19+
var Promise = require("bluebird");
20+
var dbname = argv.dbname ? argv.dbname : 'codepush';
21+
var dbhost = argv.dbhost ? argv.dbhost : 'localhost';
22+
var dbuser = argv.dbuser ? argv.dbuser : 'root';
23+
var dbpassword = argv.dbpassword ? argv.dbpassword : null;
24+
if (command === 'init') {
25+
var connection2;
26+
var connection = mysql.createConnection({
27+
host: dbhost,
28+
user: dbuser,
29+
password: dbpassword
30+
});
31+
Promise.promisifyAll(connection);
32+
connection.connect();
33+
connection.queryAsync(`CREATE DATABASE ${dbname}`)
34+
.then(function(){
35+
connection2 = mysql.createConnection({
36+
host: dbhost,
37+
user: dbuser,
38+
password: dbpassword,
39+
database: dbname,
40+
multipleStatements: true
41+
});
42+
connection2.connect();
43+
Promise.promisifyAll(connection2);
44+
return connection2;
45+
})
46+
.then(function(connection2){
47+
var sql = fs.readFileSync(path.resolve(__dirname, '../sql/codepush.sql'), 'utf-8');
48+
var sql2 = fs.readFileSync(path.resolve(__dirname, '../sql/codepush-v0.1.1.sql'), 'utf-8');
49+
var sql3 = fs.readFileSync(path.resolve(__dirname, '../sql/codepush-v0.1.5.sql'), 'utf-8');
50+
return connection2.queryAsync(sql)
51+
.then(function(){
52+
return connection2.queryAsync(sql2);
53+
})
54+
.then(function(){
55+
return connection2.queryAsync(sql3);
56+
});
57+
})
58+
.then(function(){
59+
console.log('success.');
60+
})
61+
.catch(function(e){
62+
console.log(e);
63+
})
64+
.finally(function(){
65+
if(connection) connection.end();
66+
if(connection2) connection2.end()
67+
});
68+
} else {
69+
yargs.showHelp();
70+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"url": "https://github.com/lisong/"
1414
},
1515
"bin": {
16-
"code-push-server": "./bin/www"
16+
"code-push-server": "./bin/www",
17+
"code-push-server-db": "./bin/db"
1718
},
1819
"engines": {
1920
"node": ">= 5.0",
@@ -55,6 +56,7 @@
5556
"serve-favicon": "~2.3.0",
5657
"slash": "^1.0.0",
5758
"validator": "^5.0.0",
59+
"yargs": "^6.2.0",
5860
"yazl": "^2.3.0"
5961
},
6062
"devDependencies": {

0 commit comments

Comments
 (0)