diff --git a/src/assets/config/dynamic-config.js b/src/assets/config/dynamic-config.js new file mode 100644 index 000000000..9c84f691b --- /dev/null +++ b/src/assets/config/dynamic-config.js @@ -0,0 +1,26 @@ +// load .env variables +require('dotenv').config(); // assuming .env is in the current directory + +module.exports = { + development: { + username: process.env.DB_USER || 'root', + password: process.env.DB_PASS || null, + database: process.env.DEV_DB_NAME || 'database_development', + host: process.env.DB_HOST || '127.0.0.1', + dialect: 'mysql', + }, + test: { + username: process.env.DB_USER || 'root', + password: process.env.DB_PASS || null, + database: process.env.TEST_DB_NAME || 'database_test', + host: process.env.DB_HOST || '127.0.0.1', + dialect: 'mysql', + }, + production: { + username: process.env.DB_USER || 'root', + password: process.env.DB_PASS || null, + database: process.env.PROD_DB_NAME || 'database_production', + host: process.env.DB_HOST || '127.0.0.1', + dialect: 'mysql', + }, +}; diff --git a/src/helpers/config-helper.js b/src/helpers/config-helper.js index d62adddb2..691d6e41b 100644 --- a/src/helpers/config-helper.js +++ b/src/helpers/config-helper.js @@ -63,46 +63,51 @@ const api = { return helpers.path.existsSync(api.getConfigFile()); }, - getDefaultConfig() { - return ( - JSON.stringify( - { - development: { - username: 'root', - password: null, - database: 'database_development', - host: '127.0.0.1', - dialect: 'mysql', + getDefaultConfig(configPath) { + if (configPath.endsWith('js')) { + return helpers.asset.read('config/dynamic-config.js'); + } else { + return ( + JSON.stringify( + { + development: { + username: 'root', + password: null, + database: 'database_development', + host: '127.0.0.1', + dialect: 'mysql', + }, + test: { + username: 'root', + password: null, + database: 'database_test', + host: '127.0.0.1', + dialect: 'mysql', + }, + production: { + username: 'root', + password: null, + database: 'database_production', + host: '127.0.0.1', + dialect: 'mysql', + }, }, - test: { - username: 'root', - password: null, - database: 'database_test', - host: '127.0.0.1', - dialect: 'mysql', - }, - production: { - username: 'root', - password: null, - database: 'database_production', - host: '127.0.0.1', - dialect: 'mysql', - }, - }, - undefined, - 2 - ) + '\n' - ); + undefined, + 2 + ) + '\n' + ); + } }, writeDefaultConfig() { - const configPath = path.dirname(api.getConfigFile()); + const configFilePath = api.getConfigFile(); + const configFolderPath = path.dirname(configFilePath); - if (!helpers.path.existsSync(configPath)) { - helpers.asset.mkdirp(configPath); + if (!helpers.path.existsSync(configFolderPath)) { + helpers.asset.mkdirp(configFolderPath); } - fs.writeFileSync(api.getConfigFile(), api.getDefaultConfig()); + fs.writeFileSync(configFilePath, api.getDefaultConfig(configFilePath)); }, readConfig() { diff --git a/test/init.test.js b/test/init.test.js index 518e4dadf..fd279eb74 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -126,6 +126,23 @@ const gulp = require('gulp'); }); }); + it('does not put JSON content in dynamic custom configuration file', (done) => { + gulp + .src(Support.resolveSupportPath('tmp')) + .pipe(helpers.clearDirectory()) + .pipe(helpers.runCli(flag + ' --config config/database.js')) + .pipe( + helpers.teardown(() => { + gulp + .src(Support.resolveSupportPath('tmp', 'config')) + .pipe(helpers.readFile('database.js')) + .pipe(helpers.ensureContent('module.exports')) + .pipe(helpers.ensureContent('process.env')) + .pipe(helpers.teardown(done)); + }) + ); + }); + it('does not overwrite an existing config.json file', (done) => { gulp .src(Support.resolveSupportPath('tmp'))