Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/assets/config/dynamic-config.js
Original file line number Diff line number Diff line change
@@ -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',
},
};
71 changes: 38 additions & 33 deletions src/helpers/config-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
Loading