1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var fs = require('fs');
|
24 | var path = require('path');
|
25 | var inflection = require('inflection');
|
26 | var root_app = process.cwd();
|
27 | var train_generate = require('./train_generate.js');
|
28 |
|
29 | module.exports = function() {
|
30 | var database = process.argv[4].toLowerCase();
|
31 | var npm_module = ['mysql2'];
|
32 | if (database == 'sqlite')
|
33 | npm_module = ['sqlite3'];
|
34 | else if (database == 'postgres')
|
35 | npm_module = ['pg','pg-hstore'];
|
36 | else if (database == 'mssql')
|
37 | npm_module = ['tedious'];
|
38 |
|
39 | var development_options = '\t\t"host": "localhost",\n';
|
40 | development_options += '\t\t"database": "database_development",\n';
|
41 | development_options += '\t\t"username": "username",\n';
|
42 | development_options += '\t\t"password": "password"';
|
43 | var test_options = '\t\t"host": "localhost",\n';
|
44 | test_options += '\t\t"database": "database_test",\n';
|
45 | test_options += '\t\t"username": "username",\n';
|
46 | test_options += '\t\t"password": "password"';
|
47 | var production_options = '\t\t"host": "localhost",\n';
|
48 | production_options += '\t\t"database": "database_production",\n';
|
49 | production_options += '\t\t"username": "username",\n';
|
50 | production_options += '\t\t"password": "password"';
|
51 | if (database == 'sqlite') {
|
52 | development_options = '\t\t"storage": "db/development.sqlite3"';
|
53 | test_options = '\t\t"storage": "db/test.sqlite3"';
|
54 | production_options = '\t\t"storage": "db/production.sqlite3"';
|
55 | }
|
56 |
|
57 | var file_templates = {
|
58 | 'config/database.json': [
|
59 | {
|
60 | file_path: 'config/database.json',
|
61 | info_render: {
|
62 | dialect: database,
|
63 | development_options: development_options,
|
64 | test_options: test_options,
|
65 | production_options: production_options
|
66 | }
|
67 | }
|
68 | ]
|
69 | }
|
70 |
|
71 |
|
72 | var package_file = root_app + "/package.json";
|
73 | var package_file_content = fs.readFileSync(package_file).toString();
|
74 | if (package_file_content.indexOf('sequelize') < 0) {
|
75 | package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"sequelize": "*",');
|
76 | }
|
77 |
|
78 | for (var i in npm_module) {
|
79 | var npm = npm_module[i];
|
80 | if (package_file_content.indexOf(npm) < 0) {
|
81 | package_file_content = package_file_content.replace('"dependencies": {', '"dependencies": {\n\t\t"'+npm+'": "*",');
|
82 | }
|
83 | }
|
84 |
|
85 | fs.writeFileSync(package_file, package_file_content);
|
86 |
|
87 |
|
88 | var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../');
|
89 | var path_templ = lib + 'template/database';
|
90 |
|
91 | train_generate(path_templ, null, file_templates);
|
92 | }
|