Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ this is a web based integration for communities who love police roleplaying and
- [Local9](https://github.com/Local9)
- [jxckUK](https://github.com/jxckUK)
- [Creizy](https://github.com/Creizy)
- [Whitigol](https://github.com/WhitigolProd)
- [Whitigol](https://github.com/whitigol)
- [LazyCalypso](https://github.com/LazyCalypso)
- [Northern524](https://github.com/Northern524)
- [Sharki](https://github.com/subtosharki)
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/utils/validate-requirements.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// credits to Whitigol
// https://github.com/WhitigolProd/SnailyCAD-Manager/blob/main/src/scripts/utils/requirements.ts
// https://github.com/whitigol/SnailyCAD-Manager/blob/main/src/scripts/utils/requirements.ts
import commandExists from "command-exists";
import { $log, drawTable } from "@tsed/logger";

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"start": "pnpm concurrently \"pnpm --filter \"@snailycad/client\" start\" \"pnpm --filter \"@snailycad/api\" generate && pnpm --filter \"@snailycad/api\" start\"",
"full-start": "git stash && git pull && pnpm install && node scripts/copy-env.mjs --client --api && pnpm run build && pnpm run start",
"build": "pnpm turbo run build --filter=\"{packages/*}\" && pnpm turbo run build --filter=\"{apps/*}\"",
"build": "node scripts/create-images-domain.mjs && pnpm turbo run build --filter=\"{packages/*}\" && pnpm turbo run build --filter=\"{apps/*}\"",
"dev": "docker compose up -d && pnpm turbo run watch --parallel --concurrency 11",
"format": "prettier --write \"./(packages|apps)/**/**/*.{js,jsx,ts,mjs,tsx,md,css,json}\" --ignore-path .gitignore",
"lint": "pnpm eslint . --ext .ts,.js,.tsx,.jsx,.mjs",
Expand Down
62 changes: 44 additions & 18 deletions scripts/create-images-domain.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ function getNextConfigPath() {

async function loadNextConfig() {
const configFilePath = getNextConfigPath();

const text = await readFile(configFilePath, "utf8");

return { text };
}

Expand All @@ -47,35 +45,63 @@ async function writeNextConfig(data) {
);
}

function urlToDomain(fullUrl) {
function getHostnameAndProtocol(fullUrl) {
try {
const url = new URL(fullUrl);

if (url.hostname === "api") {
return "localhost";
return { hostname: "localhost", protocol: "http" };
}

return `${url.hostname}`;
const protocol = url.protocol === "https:" ? "https" : "http";
return { hostname: url.hostname, protocol };
} catch {
return "localhost";
return { hostname: "localhost", protocol: "http" };
}
}

const domain = urlToDomain(process.env.NEXT_PUBLIC_PROD_ORIGIN);
const { hostname, protocol } = getHostnameAndProtocol(process.env.NEXT_PUBLIC_PROD_ORIGIN);
const { text } = await loadNextConfig();

const stringArray = text.split("\n");
const lines = text.split("\n");
const imagesStart = lines.findIndex((line) => line.includes("images: { // start images"));
const imagesEnd = lines.findIndex((line) => line.includes("}, // end images"));

const imagesIndex = stringArray.findIndex((line) => line.includes("images: { // start images"));
const imagesEndIndex = stringArray.findIndex((line) => line.includes("}, // end images"));
if (imagesStart === -1 || imagesEnd === -1) {
console.warn("Could not find images block in next.config.mjs");
process.exit(0);
}

let imagesData = stringArray.slice(imagesIndex, imagesEndIndex).join("\n");
imagesData = imagesData.replace(/, "localhost"/, `, "localhost", "${domain}"`);
const imagesBlock = lines.slice(imagesStart, imagesEnd + 1).join("\n");
const hostnameAlreadyPresent = imagesBlock.includes(`hostname: "${hostname}"`);

stringArray.splice(imagesIndex, imagesEndIndex - imagesIndex, imagesData);
if (hostnameAlreadyPresent) {
console.log("Image remotePatterns already include hostname:", hostname);
process.exit(0);
}

const config = stringArray.join("\n");
// Find the line that closes remotePatterns (whitespace + ])
let insertAtIndex = -1;
for (let i = imagesStart; i <= imagesEnd; i++) {
const trimmed = lines[i].trim();
if (trimmed === "]") {
insertAtIndex = i;
break;
}
}

await writeNextConfig(config);
if (insertAtIndex === -1) {
console.warn("Could not find remotePatterns closing bracket");
process.exit(0);
}

console.log("Image domain added to next.config.mjs");
const newEntry = [
" {",
` protocol: "${protocol}",`,
` hostname: "${hostname}",`,
// eslint-disable-next-line quotes
` pathname: "**"`,
" },",
];
lines.splice(insertAtIndex, 0, ...newEntry);

await writeNextConfig(lines.join("\n"));
console.log("Image remotePatterns updated in next.config.mjs");