dsql-admin/dsql-app/docker-root-user-init.ts

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-01-13 08:00:21 +00:00
import path from "path";
2024-11-05 11:12:42 +00:00
require("dotenv").config({ path: path.resolve(__dirname, ".env") });
2025-01-13 08:00:21 +00:00
import fs from "fs";
import { execSync } from "child_process";
2024-11-05 11:12:42 +00:00
/**
* # Create Extra users
* @description Create `READ_ONLY` and `FULL_ACCESS` users
*/
async function initRootUser() {
/**
* Grab Schema
*
* @description Grab Schema
*/
const first_name = process.env.DSQL_LOCAL_SU_FIRST_NAME;
const last_name = process.env.DSQL_LOCAL_SU_LAST_NAME;
const email = process.env.DSQL_LOCAL_SU_EMAIL;
const username = process.env.DSQL_LOCAL_SU_USERNAME;
const password = process.env.DSQL_LOCAL_SU_PASSWORD;
if (!password) {
throw new Error("Please enter a root admin user password");
}
/** @type {any} */
const userObject = {
id: 1,
first_name,
last_name,
username,
email,
2024-11-05 14:18:40 +00:00
password,
2024-11-05 11:12:42 +00:00
verification_status: 1,
user_priviledge: 7,
};
2024-11-05 14:18:40 +00:00
try {
const tmpDir = "/app/.tmp";
2024-11-05 15:05:56 +00:00
if (!fs.existsSync(tmpDir)) {
2024-11-05 14:18:40 +00:00
fs.mkdirSync(tmpDir, { recursive: true });
}
const tmpFilePath = `${tmpDir}/new-user.json`;
fs.writeFileSync(tmpFilePath, JSON.stringify(userObject));
2025-01-13 08:00:21 +00:00
const execStr = `bun /app/package-shared/shell/mariadb-users/users/create-user.ts ${tmpFilePath}`;
2024-11-05 14:18:40 +00:00
execSync(execStr, { stdio: "inherit" });
2025-01-13 08:00:21 +00:00
} catch (error: any) {
console.log(`docker-root-user-init.ts ERROR: ${error.message}`);
2024-11-05 11:12:42 +00:00
}
process.exit();
}
initRootUser();