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';
|
2021-07-30 14:58:36 +00:00
|
|
|
import * as Joi from 'joi';
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2021-07-30 14:58:36 +00:00
|
|
|
function validateDatabaseConfig(): Joi.ValidationResult {
|
|
|
|
|
const envVarsSchema = Joi.object()
|
|
|
|
|
.keys({
|
|
|
|
|
PG_HOST: Joi.string().required(),
|
|
|
|
|
PG_PORT: Joi.number().positive().default(5432),
|
|
|
|
|
PG_PASS: Joi.string().default(''),
|
|
|
|
|
PG_USER: Joi.string().required(),
|
|
|
|
|
PG_DB: Joi.string().required(),
|
|
|
|
|
})
|
|
|
|
|
.unknown();
|
|
|
|
|
|
|
|
|
|
return envVarsSchema.validate(process.env);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dropDatabaseFromFile(envPath: string): void {
|
2021-07-19 15:51:50 +00:00
|
|
|
const result = dotenv.config({ path: envPath });
|
|
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
|
throw result.error;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 14:58:36 +00:00
|
|
|
dropDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dropDatabase(): void {
|
|
|
|
|
const { value: envVars, error } = validateDatabaseConfig();
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw new Error(`Config validation error: ${error.message}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exec('command -v dropdb', (err, _stdout, _stderr) => {
|
2021-07-19 15:51:50 +00:00
|
|
|
if (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dropdb =
|
2021-07-30 14:58:36 +00:00
|
|
|
`PGPASSWORD=${envVars.PG_PASS} dropdb ` +
|
|
|
|
|
`-h ${envVars.PG_HOST} ` +
|
|
|
|
|
`-p ${envVars.PG_PORT} ` +
|
|
|
|
|
`-U ${envVars.PG_USER} ` +
|
2021-07-19 17:08:59 +00:00
|
|
|
process.env.PG_DB;
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2021-07-30 14:58:36 +00:00
|
|
|
exec(dropdb, (err, _stdout, _stderr) => {
|
2021-07-19 15:51:50 +00:00
|
|
|
if (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 14:58:36 +00:00
|
|
|
console.log(`Dropped database ${envVars.PG_DB}`);
|
2021-07-19 15:51:50 +00:00
|
|
|
});
|
|
|
|
|
});
|
2021-07-30 14:58:36 +00:00
|
|
|
}
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2021-07-20 09:47:16 +00:00
|
|
|
const nodeEnvPath = path.resolve(
|
|
|
|
|
process.cwd(),
|
2021-07-23 17:05:36 +00:00
|
|
|
process.env.NODE_ENV === 'test' ? '../.env.test' : '../.env',
|
2021-07-20 09:47:16 +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)) {
|
2021-07-30 14:58:36 +00:00
|
|
|
dropDatabaseFromFile(nodeEnvPath);
|
2021-07-19 15:51:50 +00:00
|
|
|
} else if (fs.existsSync(fallbackPath)) {
|
2021-07-30 14:58:36 +00:00
|
|
|
dropDatabaseFromFile(fallbackPath);
|
2021-07-19 15:51:50 +00:00
|
|
|
} else {
|
2021-07-30 14:58:36 +00:00
|
|
|
console.log(
|
|
|
|
|
`${nodeEnvPath} file not found to drop database\n` +
|
|
|
|
|
'Inferring config from the environment',
|
|
|
|
|
);
|
|
|
|
|
dropDatabase();
|
2021-07-19 15:51:50 +00:00
|
|
|
}
|