2021-07-19 15:51:50 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as dotenv from 'dotenv';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import { exec } from 'child_process';
|
|
|
|
|
|
|
|
|
|
const createDatabase = (envPath: string): void => {
|
|
|
|
|
const result = dotenv.config({ path: envPath });
|
|
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
|
throw result.error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exec('command -v createdb', (err, stdout, stderr) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createdb =
|
2021-07-20 09:47:16 +00:00
|
|
|
`PGPASSWORD=${process.env.PG_PASS || ''} createdb ` +
|
|
|
|
|
`-h ${process.env.PG_HOST || ''} ` +
|
|
|
|
|
`-p ${process.env.PG_PORT || 5432} ` +
|
|
|
|
|
`-U ${process.env.PG_USER || ''} ` +
|
2021-07-19 17:08:59 +00:00
|
|
|
process.env.PG_DB;
|
2021-07-19 15:51:50 +00:00
|
|
|
|
|
|
|
|
exec(createdb, (err, stdout, stderr) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 17:08:59 +00:00
|
|
|
console.log(`Created database ${process.env.PG_DB}`);
|
2021-07-19 15:51:50 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-19 17:08:59 +00:00
|
|
|
const nodeEnvPath = path.resolve(
|
|
|
|
|
process.cwd(),
|
2021-07-23 14:16:01 +00:00
|
|
|
process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
|
2021-07-19 17:08:59 +00:00
|
|
|
);
|
2021-07-23 14:16:01 +00:00
|
|
|
|
2021-07-19 17:08:59 +00:00
|
|
|
const fallbackPath = path.resolve(process.cwd(), '../.env');
|
2021-07-19 15:51:50 +00:00
|
|
|
|
|
|
|
|
if (fs.existsSync(nodeEnvPath)) {
|
|
|
|
|
createDatabase(nodeEnvPath);
|
|
|
|
|
} else if (fs.existsSync(fallbackPath)) {
|
|
|
|
|
createDatabase(fallbackPath);
|
|
|
|
|
} else {
|
|
|
|
|
console.error(
|
2021-07-23 14:16:01 +00:00
|
|
|
`${nodeEnvPath} file not found to create database`,
|
2021-07-19 15:51:50 +00:00
|
|
|
);
|
|
|
|
|
}
|