2022-07-08 18:17:56 +00:00
import { existsSync , unlinkSync , appendFileSync } from "fs" ;
import { createInterface } from "readline" ;
2024-09-14 05:06:36 +00:00
import { Cyan , Bright } from "../misc/console-text.js" ;
import { loadJSON } from "../misc/load-from-fs.js" ;
2022-07-08 18:17:56 +00:00
import { execSync } from "child_process" ;
2024-01-19 22:40:25 +00:00
const { version } = loadJSON ( "./package.json" ) ;
2023-08-04 18:43:12 +00:00
2022-07-08 18:17:56 +00:00
let envPath = './.env' ;
let q = ` ${ Cyan ( '?' ) } \x 1b[1m ` ;
2023-06-05 06:43:04 +00:00
let ob = { } ;
2022-08-16 07:14:19 +00:00
let rl = createInterface ( { input : process . stdin , output : process . stdout } ) ;
2022-07-08 18:17:56 +00:00
2022-09-01 13:51:18 +00:00
let final = ( ) => {
2023-06-05 06:43:04 +00:00
if ( existsSync ( envPath ) ) unlinkSync ( envPath ) ;
2022-09-01 13:51:18 +00:00
for ( let i in ob ) {
appendFileSync ( envPath , ` ${ i } = ${ ob [ i ] } \n ` )
}
2023-06-05 06:43:04 +00:00
console . log ( Bright ( "\nAwesome! I've created a fresh .env file for you." ) ) ;
console . log ( ` ${ Bright ( "Now I'll run" ) } ${ Cyan ( "npm install" ) } ${ Bright ( "to install all dependencies. It shouldn't take long.\n\n" ) } ` ) ;
2022-09-01 13:51:18 +00:00
execSync ( 'npm install' , { stdio : [ 0 , 1 , 2 ] } ) ;
2023-06-05 06:43:04 +00:00
console . log ( ` \n \n ${ Cyan ( "All done!\n" ) } ` ) ;
console . log ( Bright ( "You can re-run this script at any time to update the configuration." ) ) ;
console . log ( Bright ( "\nYou're now ready to start cobalt. Simply run " ) + Cyan ( "npm start" ) + Bright ( '!\nHave fun :)' ) ) ;
2022-09-01 13:51:18 +00:00
rl . close ( )
}
2022-07-08 18:17:56 +00:00
console . log (
2023-08-04 18:43:12 +00:00
` ${ Cyan ( ` Hey, this is cobalt v. ${ version } ! ` ) } \n ${ Bright ( "Let's start by creating a new " ) } ${ Cyan ( ".env" ) } ${ Bright ( " file. You can always change it later." ) } `
2022-07-08 18:17:56 +00:00
)
2023-06-05 06:43:04 +00:00
function setup ( ) {
console . log ( Bright ( "\nWhat kind of server will this instance be?\nOptions: api, web." ) ) ;
rl . question ( q , r1 => {
switch ( r1 . toLowerCase ( ) ) {
case 'api' :
2024-05-22 01:54:49 +00:00
console . log ( Bright ( "\nCool! What's the domain this API instance will be running on? (localhost)\nExample: api.cobalt.tools" ) ) ;
2023-06-05 06:43:04 +00:00
rl . question ( q , apiURL => {
2024-03-05 17:51:25 +00:00
ob . API _URL = ` http://localhost:9000/ ` ;
ob . API _PORT = 9000 ;
if ( apiURL && apiURL !== "localhost" ) ob . API _URL = ` https:// ${ apiURL . toLowerCase ( ) } / ` ;
2022-07-08 18:17:56 +00:00
2023-06-05 06:43:04 +00:00
console . log ( Bright ( "\nGreat! Now, what port will it be running on? (9000)" ) ) ;
2023-02-09 14:45:17 +00:00
2023-06-05 06:43:04 +00:00
rl . question ( q , apiPort => {
2024-03-05 17:51:25 +00:00
if ( apiPort ) ob . API _PORT = apiPort ;
if ( apiPort && ( apiURL === "localhost" || ! apiURL ) ) ob . API _URL = ` http://localhost: ${ apiPort } / ` ;
2023-02-09 14:45:17 +00:00
2023-06-05 06:43:04 +00:00
console . log ( Bright ( "\nWhat will your instance's name be? Usually it's something like eu-nl aka region-country. (local)" ) ) ;
2023-04-09 04:47:17 +00:00
2023-06-05 06:43:04 +00:00
rl . question ( q , apiName => {
2024-03-05 17:51:25 +00:00
ob . API _NAME = apiName . toLowerCase ( ) ;
if ( ! apiName || apiName === "local" ) ob . API _NAME = "local" ;
2023-04-09 04:47:17 +00:00
2023-06-05 06:43:04 +00:00
console . log ( Bright ( "\nOne last thing: would you like to enable CORS? It allows other websites and extensions to use your instance's API.\ny/n (n)" ) ) ;
rl . question ( q , apiCors => {
2023-08-13 18:09:50 +00:00
let answCors = apiCors . toLowerCase ( ) . trim ( ) ;
2024-03-05 17:51:25 +00:00
if ( answCors !== "y" && answCors !== "yes" ) ob . CORS _WILDCARD = '0'
2023-06-05 06:43:04 +00:00
final ( )
} )
} )
} ) ;
} )
break ;
case 'web' :
2023-09-09 16:31:24 +00:00
console . log ( Bright ( "\nAwesome! What's the domain this web app instance will be running on? (localhost)\nExample: cobalt.tools" ) ) ;
2023-06-05 06:43:04 +00:00
rl . question ( q , webURL => {
2024-03-05 17:51:25 +00:00
ob . WEB _URL = ` http://localhost:9001/ ` ;
ob . WEB _PORT = 9001 ;
if ( webURL && webURL !== "localhost" ) ob . WEB _URL = ` https:// ${ webURL . toLowerCase ( ) } / ` ;
2023-06-05 06:43:04 +00:00
console . log (
Bright ( "\nGreat! Now, what port will it be running on? (9001)" )
)
rl . question ( q , webPort => {
2024-03-05 17:51:25 +00:00
if ( webPort ) ob . WEB _PORT = webPort ;
if ( webPort && ( webURL === "localhost" || ! webURL ) ) ob . WEB _URL = ` http://localhost: ${ webPort } / ` ;
2023-06-05 06:43:04 +00:00
console . log (
2024-05-22 01:54:49 +00:00
Bright ( "\nOne last thing: what default API domain should be used? (api.cobalt.tools)\nIf it's hosted locally, make sure to include the port:" ) + Cyan ( " localhost:9000" )
2023-06-05 06:43:04 +00:00
) ;
rl . question ( q , apiURL => {
2024-03-05 17:51:25 +00:00
ob . API _URL = ` https:// ${ apiURL . toLowerCase ( ) } / ` ;
if ( apiURL . includes ( ':' ) ) ob . API _URL = ` http:// ${ apiURL . toLowerCase ( ) } / ` ;
2024-05-22 01:54:49 +00:00
if ( ! apiURL ) ob . API _URL = "https://api.cobalt.tools/" ;
2023-06-05 06:43:04 +00:00
final ( )
} )
} ) ;
} ) ;
break ;
default :
console . log ( Bright ( "\nThis is not an option. Try again." ) ) ;
setup ( )
}
} )
}
setup ( )