UNPKG

3.19 kBJavaScriptView Raw
1/*
2
3 This file is a part of node-on-train project.
4
5 Copyright (C) Thanh D. Dang <thanhdd.it@gmail.com>
6
7 node-on-train is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 node-on-train is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22
23var fs = require('fs');
24var path = require('path');
25var inflection = require('inflection');
26var root_app = process.cwd();
27var train_generate = require('./train_generate.js');
28
29module.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 //--- Edit package.json ---//
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 //--- Edit package.json ---//
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}