mirror of
https://github.com/directus/directus.git
synced 2026-02-16 00:15:35 -05:00
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { drivers } from '../drivers';
|
|
import { Credentials } from '../create-db-connection';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { nanoid } from 'nanoid';
|
|
import { Liquid } from 'liquidjs';
|
|
import fs from 'fs';
|
|
import { promisify } from 'util';
|
|
import path from 'path';
|
|
|
|
const readFile = promisify(fs.readFile);
|
|
const writeFile = promisify(fs.writeFile);
|
|
|
|
const liquidEngine = new Liquid({
|
|
extname: '.liquid',
|
|
});
|
|
|
|
const defaults = {
|
|
security: {
|
|
KEY: uuidv4(),
|
|
SECRET: nanoid(32),
|
|
},
|
|
};
|
|
|
|
export default async function createEnv(client: keyof typeof drivers, credentials: Credentials, directory: string) {
|
|
const config: Record<string, any> = {
|
|
...defaults,
|
|
database: {
|
|
DB_CLIENT: client,
|
|
},
|
|
};
|
|
|
|
for (const [key, value] of Object.entries(credentials)) {
|
|
config.database[`DB_${key.toUpperCase()}`] = value;
|
|
}
|
|
|
|
const configAsStrings: any = {};
|
|
|
|
for (const [key, value] of Object.entries(config)) {
|
|
configAsStrings[key] = '';
|
|
|
|
for (const [envKey, envValue] of Object.entries(value)) {
|
|
configAsStrings[key] += `${envKey}="${envValue}"\n`;
|
|
}
|
|
}
|
|
|
|
const templateString = await readFile(path.join(__dirname, 'env-stub.liquid'), 'utf8');
|
|
const text = await liquidEngine.parseAndRender(templateString, configAsStrings);
|
|
await writeFile(path.join(directory, '.env'), text);
|
|
}
|