Replace entire codebase with homarr-labs/homarr
This commit is contained in:
9
packages/cli/eslint.config.js
Normal file
9
packages/cli/eslint.config.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import baseConfig from "@homarr/eslint-config/base";
|
||||
|
||||
/** @type {import('typescript-eslint').Config} */
|
||||
export default [
|
||||
{
|
||||
ignores: [],
|
||||
},
|
||||
...baseConfig,
|
||||
];
|
||||
1
packages/cli/index.ts
Normal file
1
packages/cli/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./src";
|
||||
41
packages/cli/package.json
Normal file
41
packages/cli/package.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "@homarr/cli",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"license": "Apache-2.0",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": "./index.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"src/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "esbuild src/index.ts --bundle --platform=node --outfile=cli.cjs --external:bcrypt --external:cpu-features --loader:.html=text --loader:.node=text",
|
||||
"clean": "rm -rf .turbo node_modules",
|
||||
"format": "prettier --check . --ignore-path ../../.gitignore",
|
||||
"lint": "eslint",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@drizzle-team/brocli": "^0.11.0",
|
||||
"@homarr/auth": "workspace:^0.1.0",
|
||||
"@homarr/common": "workspace:^0.1.0",
|
||||
"@homarr/db": "workspace:^0.1.0",
|
||||
"@homarr/validation": "workspace:^0.1.0",
|
||||
"dotenv": "^17.2.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@homarr/eslint-config": "workspace:^0.2.0",
|
||||
"@homarr/prettier-config": "workspace:^0.1.0",
|
||||
"@homarr/tsconfig": "workspace:^0.1.0",
|
||||
"esbuild": "^0.27.2",
|
||||
"eslint": "^9.39.2",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
}
|
||||
36
packages/cli/src/commands/fix-usernames.ts
Normal file
36
packages/cli/src/commands/fix-usernames.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { command } from "@drizzle-team/brocli";
|
||||
|
||||
import { db, eq } from "@homarr/db";
|
||||
import { users } from "@homarr/db/schema";
|
||||
|
||||
export const fixUsernames = command({
|
||||
name: "fix-usernames",
|
||||
desc: "Changes all credentials usernames to lowercase",
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
handler: async () => {
|
||||
if (!process.env.AUTH_PROVIDERS?.toLowerCase().includes("credentials")) {
|
||||
console.error("Credentials provider is not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
const credentialUsers = await db.query.users.findMany({
|
||||
where: eq(users.provider, "credentials"),
|
||||
});
|
||||
|
||||
for (const user of credentialUsers) {
|
||||
if (!user.name) continue;
|
||||
if (user.name === user.name.toLowerCase()) continue;
|
||||
|
||||
await db
|
||||
.update(users)
|
||||
.set({
|
||||
name: user.name.toLowerCase(),
|
||||
})
|
||||
.where(eq(users.id, user.id));
|
||||
|
||||
console.log(`Changed username from ${user.name} to ${user.name.toLowerCase()}`);
|
||||
}
|
||||
|
||||
console.log("All usernames have been fixed");
|
||||
},
|
||||
});
|
||||
96
packages/cli/src/commands/recreate-admin.ts
Normal file
96
packages/cli/src/commands/recreate-admin.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { command, string } from "@drizzle-team/brocli";
|
||||
|
||||
import { createSaltAsync, hashPasswordAsync } from "@homarr/auth";
|
||||
import { createId } from "@homarr/common";
|
||||
import { generateSecureRandomToken } from "@homarr/common/server";
|
||||
import { and, count, db, eq } from "@homarr/db";
|
||||
import { getMaxGroupPositionAsync } from "@homarr/db/queries";
|
||||
import { groupMembers, groupPermissions, groups, users } from "@homarr/db/schema";
|
||||
import { usernameSchema } from "@homarr/validation/user";
|
||||
|
||||
export const recreateAdmin = command({
|
||||
name: "recreate-admin",
|
||||
desc: "Recreate credentials admin user if none exists anymore",
|
||||
options: {
|
||||
username: string("username").required().alias("u").desc("Name of the admin"),
|
||||
},
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
handler: async (options) => {
|
||||
if (!process.env.AUTH_PROVIDERS?.toLowerCase().includes("credentials")) {
|
||||
console.error("Credentials provider is not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await usernameSchema.safeParseAsync(options.username);
|
||||
|
||||
if (!result.success) {
|
||||
console.error("Invalid username:");
|
||||
console.error(result.error.issues.map((error) => `- ${error.message}`).join("\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
const totalCount = await db
|
||||
.select({
|
||||
count: count(),
|
||||
})
|
||||
.from(groupPermissions)
|
||||
.leftJoin(groupMembers, eq(groupMembers.groupId, groupPermissions.groupId))
|
||||
.leftJoin(users, eq(users.id, groupMembers.userId))
|
||||
.where(and(eq(groupPermissions.permission, "admin"), eq(users.provider, "credentials")))
|
||||
.then((rows) => rows.at(0)?.count ?? 0);
|
||||
|
||||
if (totalCount > 0) {
|
||||
console.error("Credentials admin user exists");
|
||||
return;
|
||||
}
|
||||
|
||||
const existingUser = await db.query.users.findFirst({
|
||||
where: eq(users.name, result.data),
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
console.error("User with this name already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
const temporaryGroupId = createId();
|
||||
|
||||
const maxPosition = await getMaxGroupPositionAsync(db);
|
||||
await db.insert(groups).values({
|
||||
id: temporaryGroupId,
|
||||
name: temporaryGroupId,
|
||||
position: maxPosition + 1,
|
||||
});
|
||||
|
||||
await db.insert(groupPermissions).values({
|
||||
groupId: temporaryGroupId,
|
||||
permission: "admin",
|
||||
});
|
||||
|
||||
const salt = await createSaltAsync();
|
||||
const password = generateSecureRandomToken(24);
|
||||
const hashedPassword = await hashPasswordAsync(password, salt);
|
||||
|
||||
const userId = createId();
|
||||
await db.insert(users).values({
|
||||
id: userId,
|
||||
name: result.data,
|
||||
provider: "credentials",
|
||||
password: hashedPassword,
|
||||
salt,
|
||||
});
|
||||
|
||||
await db.insert(groupMembers).values({
|
||||
groupId: temporaryGroupId,
|
||||
userId,
|
||||
});
|
||||
|
||||
console.log(
|
||||
"We created a new admin user for you. Please keep in mind, that the admin group of it has a temporary name. You should change it to something more meaningful.",
|
||||
);
|
||||
console.log(`\tUsername: ${result.data}`);
|
||||
console.log(`\tPassword: ${password}`);
|
||||
console.log(`\tGroup: ${temporaryGroupId}`);
|
||||
console.log(""); // Empty line for better readability
|
||||
},
|
||||
});
|
||||
46
packages/cli/src/commands/reset-password.ts
Normal file
46
packages/cli/src/commands/reset-password.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { command, string } from "@drizzle-team/brocli";
|
||||
|
||||
import { hashPasswordAsync } from "@homarr/auth";
|
||||
import { generateSecureRandomToken } from "@homarr/common/server";
|
||||
import { and, db, eq } from "@homarr/db";
|
||||
import { sessions, users } from "@homarr/db/schema";
|
||||
|
||||
export const resetPassword = command({
|
||||
name: "reset-password",
|
||||
desc: "Reset password for a user",
|
||||
options: {
|
||||
username: string("username").required().alias("u").desc("Name of the user"),
|
||||
},
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
handler: async (options) => {
|
||||
if (!process.env.AUTH_PROVIDERS?.toLowerCase().includes("credentials")) {
|
||||
console.error("Credentials provider is not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await db.query.users.findFirst({
|
||||
where: and(eq(users.name, options.username), eq(users.provider, "credentials")),
|
||||
});
|
||||
|
||||
if (!user?.salt) {
|
||||
console.error(`User ${options.username} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Generates a new password with 48 characters
|
||||
const newPassword = generateSecureRandomToken(24);
|
||||
|
||||
await db
|
||||
.update(users)
|
||||
.set({
|
||||
password: await hashPasswordAsync(newPassword, user.salt),
|
||||
})
|
||||
.where(eq(users.id, user.id));
|
||||
|
||||
await db.delete(sessions).where(eq(sessions.userId, user.id));
|
||||
console.log(`All sessions for user ${options.username} have been deleted`);
|
||||
|
||||
console.log("You can now login with the new password");
|
||||
console.log(`New password for user ${options.username}: ${newPassword}`);
|
||||
},
|
||||
});
|
||||
12
packages/cli/src/index.ts
Normal file
12
packages/cli/src/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { run } from "@drizzle-team/brocli";
|
||||
|
||||
import { fixUsernames } from "./commands/fix-usernames";
|
||||
import { recreateAdmin } from "./commands/recreate-admin";
|
||||
import { resetPassword } from "./commands/reset-password";
|
||||
|
||||
const commands = [resetPassword, fixUsernames, recreateAdmin];
|
||||
|
||||
void run(commands, {
|
||||
name: "homarr-cli",
|
||||
version: "1.0.0",
|
||||
});
|
||||
8
packages/cli/tsconfig.json
Normal file
8
packages/cli/tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@homarr/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
|
||||
},
|
||||
"include": ["*.ts", "src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user