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 ( / V I T E _ W E B 3 A U T H _ C L I E N T _ I D = .* / 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