1+ const fs = require ( 'fs' ) ;
2+ const readline = require ( 'readline' ) ;
3+ const chalk = require ( 'chalk' ) ;
4+ const path = require ( 'path' ) ;
5+
6+ function setupNode ( ) {
7+ const rl = readline . createInterface ( {
8+ input : process . stdin ,
9+ output : process . stdout
10+ } ) ;
11+
12+ console . log ( chalk . green ( '\nLapsus Node Configuration\n' ) ) ;
13+ console . log ( chalk . yellow ( 'This will configure node settings at line 146 in settings.json\n' ) ) ;
14+
15+ const settingsPath = path . join ( __dirname , '../settings.json' ) ;
16+
17+ // Read the entire file as lines
18+ let settingsLines ;
19+ try {
20+ settingsLines = fs . readFileSync ( settingsPath , 'utf8' ) . split ( '\n' ) ;
21+ } catch ( err ) {
22+ console . error ( chalk . red ( 'Error reading settings.json:' ) , err ) ;
23+ process . exit ( 1 ) ;
24+ }
25+
26+ // Find the locations line (around line 146)
27+ let locationsIndex = - 1 ;
28+ for ( let i = 0 ; i < settingsLines . length ; i ++ ) {
29+ if ( settingsLines [ i ] . includes ( '"locations":' ) ) {
30+ locationsIndex = i ;
31+ break ;
32+ }
33+ }
34+
35+ // If locations not found, create it at line 146 (145 in 0-based index)
36+ if ( locationsIndex === - 1 ) {
37+ locationsIndex = 145 ;
38+ // Ensure we have enough lines (pad if necessary)
39+ while ( settingsLines . length <= locationsIndex ) {
40+ settingsLines . push ( '' ) ;
41+ }
42+ settingsLines . splice ( locationsIndex , 0 , ' "locations": {},' ) ;
43+ }
44+
45+ // Extract the current locations content
46+ let locations = { } ;
47+ try {
48+ const lineContent = settingsLines [ locationsIndex ] ;
49+ const match = lineContent . match ( / " l o c a t i o n s " : \s * ( { [ ^ } ] * } ) / ) ;
50+ if ( match && match [ 1 ] ) {
51+ locations = JSON . parse ( match [ 1 ] ) ;
52+ }
53+ } catch ( err ) {
54+ console . log ( chalk . yellow ( 'Starting with empty locations (could not parse existing)' ) ) ;
55+ }
56+
57+ const askQuestions = async ( ) => {
58+ const nodeId = await new Promise ( resolve => {
59+ rl . question ( chalk . blue ( '\nEnter Node ID (e.g., "2"): ' ) , ( input ) => {
60+ resolve ( input . trim ( ) ) ;
61+ } ) ;
62+ } ) ;
63+
64+ const nodeName = await new Promise ( resolve => {
65+ rl . question ( chalk . blue ( 'Enter Node Name (e.g., "Germany"): ' ) , ( input ) => {
66+ resolve ( input . trim ( ) ) ;
67+ } ) ;
68+ } ) ;
69+
70+ const city = await new Promise ( resolve => {
71+ rl . question ( chalk . blue ( 'Enter City (e.g., "Berlin"): ' ) , ( input ) => {
72+ resolve ( input . trim ( ) ) ;
73+ } ) ;
74+ } ) ;
75+
76+ console . log ( chalk . yellow ( '\nISO Code is a 2-letter country code (e.g., "de" for Germany)' ) ) ;
77+ const iso = await new Promise ( resolve => {
78+ rl . question ( chalk . blue ( 'Enter ISO Country Code: ' ) , ( input ) => {
79+ resolve ( input . trim ( ) . toLowerCase ( ) ) ;
80+ } ) ;
81+ } ) ;
82+
83+ const pkg = await new Promise ( resolve => {
84+ rl . question ( chalk . blue ( 'Enter Package (leave empty for null): ' ) , ( input ) => {
85+ resolve ( input . trim ( ) || null ) ;
86+ } ) ;
87+ } ) ;
88+
89+ // Update locations
90+ locations [ nodeId ] = {
91+ name : nodeName ,
92+ city : city ,
93+ ISO : iso ,
94+ package : pkg
95+ } ;
96+
97+ // Create the new locations line with proper indentation
98+ const locationsString = JSON . stringify ( locations , null , 4 )
99+ . split ( '\n' )
100+ . map ( ( line , i ) => i === 0 ? line : ' ' + line )
101+ . join ( '\n' ) ;
102+
103+ // Update the specific line while preserving the original indentation
104+ const originalIndent = settingsLines [ locationsIndex ] . match ( / ^ \s * / ) [ 0 ] ;
105+ settingsLines [ locationsIndex ] = originalIndent + '"locations": ' + locationsString + ',' ;
106+
107+ // Write back to file
108+ try {
109+ fs . writeFileSync ( settingsPath , settingsLines . join ( '\n' ) , 'utf8' ) ;
110+ console . log ( chalk . green ( '\nSuccessfully updated node at line ' + ( locationsIndex + 1 ) + ' in settings.json!' ) ) ;
111+ console . log ( chalk . gray ( '\nCurrent node configuration:' ) ) ;
112+ console . log ( chalk . gray ( JSON . stringify ( locations , null , 2 ) ) ) ;
113+ } catch ( err ) {
114+ console . error ( chalk . red ( 'Error writing settings.json:' ) , err ) ;
115+ process . exit ( 1 ) ;
116+ }
117+
118+ rl . close ( ) ;
119+ } ;
120+
121+ askQuestions ( ) ;
122+ }
123+
124+ setupNode ( ) ;
0 commit comments