Skip to content

Commit 887abf4

Browse files
committed
Add automation to set client id
1 parent c6ad057 commit 887abf4

File tree

3 files changed

+85
-99
lines changed

3 files changed

+85
-99
lines changed

README_TEMPLATE.md

Lines changed: 0 additions & 98 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"update-web3auth": "lerna exec --concurrency 1 -- 'ncu -u @web3auth/* && npm install'",
66
"update-web3auth-alpha": "lerna exec --concurrency 1 -- 'ncu -u @web3auth/* --target @next'",
77
"install-all": "lerna exec --concurrency 1 -- 'npm install'",
8-
"lint-all": "lerna exec --concurrency 1 -- 'npm run lint'"
8+
"lint-all": "lerna exec --concurrency 1 -- 'npm run lint'",
9+
"set-client-id": "node set-client-id.js"
910
},
1011
"devDependencies": {
1112
"lerna": "^6.4.1",

set-client-id.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const readline = require('readline');
4+
const { execSync } = require('child_process');
5+
6+
const rl = readline.createInterface({
7+
input: process.stdin,
8+
output: process.stdout
9+
});
10+
11+
// Get all projects directories recursively (excluding node_modules)
12+
function findProjectDirs(dir, excludes = ['node_modules', '.git']) {
13+
let results = [];
14+
const list = fs.readdirSync(dir);
15+
16+
list.forEach(file => {
17+
const fullPath = path.join(dir, file);
18+
const stat = fs.statSync(fullPath);
19+
20+
if (stat && stat.isDirectory()) {
21+
if (excludes.some(exclude => file === exclude)) {
22+
return;
23+
}
24+
25+
// Check if the directory has a package.json (considered a project)
26+
const packageJsonPath = path.join(fullPath, 'package.json');
27+
if (fs.existsSync(packageJsonPath)) {
28+
results.push(fullPath);
29+
}
30+
31+
// Continue recursion
32+
results = results.concat(findProjectDirs(fullPath, excludes));
33+
}
34+
});
35+
36+
return results;
37+
}
38+
39+
// Update .env file in the given directory with the client ID
40+
function updateEnvFile(dir, clientId) {
41+
const envPath = path.join(dir, '.env');
42+
let envContent = '';
43+
44+
// Read existing .env file or create a new one
45+
if (fs.existsSync(envPath)) {
46+
envContent = fs.readFileSync(envPath, 'utf8');
47+
}
48+
49+
// Update or add the client ID
50+
const updated = envContent.includes('VITE_WEB3AUTH_CLIENT_ID=')
51+
? envContent.replace(/VITE_WEB3AUTH_CLIENT_ID=.*/g, `VITE_WEB3AUTH_CLIENT_ID=${clientId}`)
52+
: envContent + `\nVITE_WEB3AUTH_CLIENT_ID=${clientId}\n`;
53+
54+
// Write back to the file
55+
fs.writeFileSync(envPath, updated);
56+
console.log(`Updated .env with Web3Auth client ID in ${dir}`);
57+
}
58+
59+
// Main execution
60+
rl.question('Enter your Web3Auth Client ID: ', (clientId) => {
61+
if (!clientId.trim()) {
62+
console.error('Client ID cannot be empty');
63+
rl.close();
64+
return;
65+
}
66+
67+
try {
68+
const rootDir = process.cwd();
69+
const projectDirs = findProjectDirs(rootDir);
70+
71+
console.log(`Found ${projectDirs.length} projects to update\n`);
72+
73+
projectDirs.forEach(dir => {
74+
updateEnvFile(dir, clientId);
75+
});
76+
77+
console.log('\nClient ID set successfully in all examples!');
78+
} catch (error) {
79+
console.error('Error updating client ID:', error);
80+
} finally {
81+
rl.close();
82+
}
83+
});

0 commit comments

Comments
 (0)