Skip to content

Commit 4b20fe8

Browse files
committed
add tooling to check port and write config
1 parent 08519d8 commit 4b20fe8

File tree

6 files changed

+175
-2
lines changed

6 files changed

+175
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ Thumbs.db
4444

4545
# Environment variables
4646
public/config.js
47+
.auth0.config.json
4748

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"version": "0.0.0",
44
"scripts": {
55
"ng": "ng",
6-
"start": "ng serve",
6+
"start": "node tools/check-port.cjs && node tools/serve.cjs",
77
"build": "ng build",
88
"watch": "ng build --watch --configuration development",
9-
"test": "ng test"
9+
"test": "ng test",
10+
"auth0-config": "node tools/auth0-config.cjs"
1011
},
1112
"prettier": {
1213
"printWidth": 100,
@@ -37,6 +38,7 @@
3738
"@angular/build": "^21.0.3",
3839
"@angular/cli": "^21.0.3",
3940
"@angular/compiler-cli": "^21.0.0",
41+
"detect-port": "^2.1.0",
4042
"jsdom": "^27.1.0",
4143
"typescript": "~5.9.2",
4244
"vitest": "^4.0.8"

tools/auth0-config.cjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
function parseArgs(args) {
5+
const params = {};
6+
for (let i = 2; i < args.length; i += 2) {
7+
const key = args[i].replace("--", "");
8+
const value = args[i + 1];
9+
params[key] = value;
10+
}
11+
return params;
12+
}
13+
14+
try {
15+
const args = parseArgs(process.argv);
16+
17+
// Validate required arguments
18+
if (!args.domain) {
19+
console.error("Error: --domain argument is required");
20+
console.error(
21+
"Usage: node auth0-config.cjs --domain <domain> --clientId <clientId> [--port <port>]",
22+
);
23+
process.exit(1);
24+
}
25+
26+
if (!args.clientId) {
27+
console.error("Error: --clientId argument is required");
28+
console.error(
29+
"Usage: node auth0-config.cjs --domain <domain> --clientId <clientId> [--port <port>]",
30+
);
31+
process.exit(1);
32+
}
33+
34+
// Validate port argument if provided
35+
if (args.port) {
36+
const portNumber = parseInt(args.port, 10);
37+
if (isNaN(portNumber) || portNumber.toString() !== args.port.toString()) {
38+
console.error("Error: --port argument must be a valid number");
39+
console.error(
40+
"Usage: node auth0-config.cjs --domain <domain> --clientId <clientId> [--port <port>]",
41+
);
42+
process.exit(1);
43+
}
44+
}
45+
46+
const port = args.port || "4200";
47+
48+
// Write public/config.js (browser config)
49+
const configPath = path.join(__dirname, "..", "public", "config.js");
50+
const configContent = `window.AUTH0_DOMAIN = '${args.domain}';
51+
window.AUTH0_CLIENT_ID = '${args.clientId}';
52+
`;
53+
fs.writeFileSync(configPath, configContent);
54+
55+
// Write .auth0.config.json (server config - port only)
56+
const auth0ConfigPath = path.join(__dirname, "..", ".auth0.config.json");
57+
const auth0Config = {
58+
port: parseInt(port, 10),
59+
};
60+
fs.writeFileSync(
61+
auth0ConfigPath,
62+
JSON.stringify(auth0Config, null, 2) + "\n",
63+
);
64+
65+
console.log(`Auth0 configuration has been written`);
66+
console.log("Config keys state:");
67+
console.log(` AUTH0_DOMAIN: ${args.domain}`);
68+
console.log(` AUTH0_CLIENT_ID: ${args.clientId}`);
69+
console.log(` PORT: ${port}`);
70+
} catch (e) {
71+
console.error("Error:", e.message);
72+
process.exit(1);
73+
}

tools/check-port.cjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const { detect } = require("detect-port");
4+
5+
// Read .auth0.config.json to get the configured port
6+
const configPath = path.join(__dirname, "..", ".auth0.config.json");
7+
8+
if (!fs.existsSync(configPath)) {
9+
console.error(`
10+
❌ Configuration file not found: .auth0.config.json
11+
12+
Please run the auth0-config script first:
13+
npm run auth0-config -- --domain <domain> --clientId <clientId> --port <port>
14+
`);
15+
process.exit(1);
16+
}
17+
18+
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
19+
const PORT = config.port || 4200;
20+
21+
detect(PORT)
22+
.then((port) => {
23+
if (port !== parseInt(PORT)) {
24+
console.error(`
25+
❌ The port ${PORT} that is configured in Auth0 is currently in use.
26+
27+
To resolve this issue:
28+
1. Free up port ${PORT} by stopping the application using it, OR
29+
2. Configure URLs with a new port in your Auth0 application settings:
30+
- Allowed Callback URLs
31+
- Allowed Logout URLs
32+
- Allowed Web Origins
33+
Then update the port by running:
34+
npm run auth0-config -- --domain <domain> --clientId <clientId> --port <new-port>
35+
`);
36+
process.exit(1);
37+
}
38+
console.log(`✅ Port ${PORT} is available.`);
39+
})
40+
.catch((err) => {
41+
console.error("Error checking port availability:", err);
42+
process.exit(1);
43+
});

tools/serve.cjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const { execSync } = require("child_process");
4+
5+
// Read .auth0.config.json to get the configured port
6+
const configPath = path.join(__dirname, "..", ".auth0.config.json");
7+
8+
if (!fs.existsSync(configPath)) {
9+
console.error(`
10+
❌ Configuration file not found: .auth0.config.json
11+
12+
Please run the auth0-config script first:
13+
npm run auth0-config -- --domain <domain> --clientId <clientId> --port <port>
14+
`);
15+
process.exit(1);
16+
}
17+
18+
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
19+
const PORT = config.port || 4200;
20+
21+
// Use npx to run local ng binary
22+
try {
23+
execSync(`npx ng serve --port ${PORT}`, { stdio: "inherit" });
24+
} catch (err) {
25+
process.exit(err.status || 1);
26+
}

0 commit comments

Comments
 (0)