11const readline = require ( 'readline' ) ;
2- const { promises : fs } = require ( "fs" ) ;
32const os = require ( 'os' ) ;
43const path = require ( 'path' ) ;
4+ const fs = require ( 'fs' ) ;
55
6-
7- module . exports = async function CoCreateConfig ( config = { } ) {
8-
6+ module . exports = async function CoCreateConfig ( items , processEnv = true , updateGlobal = true ) {
97 async function promptForInput ( question ) {
108 const rl = readline . createInterface ( {
119 input : process . stdin ,
@@ -20,47 +18,83 @@ module.exports = async function CoCreateConfig(config = {}) {
2018 } ) ;
2119 }
2220
23- // Check if the config file exists
24- const configFilePath = path . join ( os . homedir ( ) , 'CoCreateConfig.json' ) ;
25- try {
26- const configFileContent = await fs . readFile ( configFilePath , 'utf8' ) ;
27- config = JSON . parse ( configFileContent ) ;
28- } catch ( error ) {
29- // Ignore error if the file doesn't exist
30- }
21+ const filterEmptyValues = ( obj ) => {
22+ return Object . fromEntries (
23+ Object . entries ( obj ) . filter ( ( [ _ , value ] ) => {
24+ if ( typeof value === 'object' && ! Array . isArray ( value ) ) {
25+ return Object . keys ( value ) . length > 0 ;
26+ } else if ( Array . isArray ( value ) ) {
27+ return value . length > 0 ;
28+ } else {
29+ return value !== '' ;
30+ }
31+ } )
32+ ) ;
33+ } ;
3134
32- // Prompt user for organization ID if not already stored
33- if ( ! config . organization_id )
34- config . organization_id = await promptForInput ( 'Enter your organization_id: ' ) ;
35+ let config = { } ;
36+ let update = false ;
3537
36- if ( ! config . host )
37- config . host = await promptForInput ( 'Enter the host: ' ) ;
38+ async function getConfig ( items ) {
39+ if ( ! Array . isArray ( items ) ) {
40+ items = [ items ] ;
41+ }
42+ for ( let i = 0 ; i < items . length ; i ++ ) {
43+ const { key, prompt, choices } = items [ i ] ;
44+ if ( ! key ) {
45+ if ( ! prompt && prompt !== '' || ! choices ) continue ;
46+ const answer = await promptForInput ( prompt || `${ key } : ` ) ;
47+ const choice = choices [ answer ] ;
48+ if ( choice ) {
49+ await getConfig ( choice ) ;
50+ }
51+ } else {
3852
53+ if ( process . env [ key ] ) {
54+ config [ key ] = process . env [ key ] ;
55+ } else if ( localConfig [ key ] ) {
56+ config [ key ] = localConfig [ key ] ;
57+ } else if ( globalConfig [ key ] ) {
58+ config [ key ] = globalConfig [ key ] ;
59+ } else if ( prompt || prompt === '' ) {
60+ config [ key ] = await promptForInput ( prompt || `${ key } : ` ) ;
61+ if ( processEnv ) process . env [ key ] = config [ key ] ;
62+ if ( updateGlobal ) update = true ;
63+ }
64+ }
65+ }
66+ }
3967
40- async function promptForSignInOrKey ( ) {
41- const option = await promptForInput ( 'Choose an option:\n1. Sign In\n2. Enter Key\n' ) ;
68+ let localConfig = { } ;
69+ const localConfigPath = path . resolve ( process . cwd ( ) , 'CoCreate.config.js' ) ;
70+ if ( fs . existsSync ( localConfigPath ) ) {
71+ localConfig = require ( localConfigPath ) ;
72+ }
73+
74+ let globalConfig = { } ;
75+ const globalConfigPath = path . resolve ( os . homedir ( ) , 'CoCreate.config.js' ) ;
76+ if ( fs . existsSync ( globalConfigPath ) ) {
77+ globalConfig = require ( globalConfigPath ) ;
78+ }
4279
43- if ( option === '1' ) {
44- if ( ! config . email )
45- config . email = await promptForInput ( 'Enter your email: ' ) ;
80+ if ( items ) {
81+ await getConfig ( items ) ;
4682
47- if ( ! config . password )
48- config . password = await promptForInput ( 'Enter your password: ' ) ;
83+ if ( update ) {
84+ const updatedGlobalConfig = {
85+ ...filterEmptyValues ( globalConfig ) ,
86+ ...filterEmptyValues ( config )
87+ } ;
4988
50- } else if ( option === '2' ) {
51- if ( ! config . key )
52- config . key = await promptForInput ( 'Enter the key: ' ) ;
53- } else {
54- console . log ( 'Invalid option. Please try again.' ) ;
55- await promptForSignInOrKey ( ) ;
89+ const globalConfigString = `module.exports = ${ JSON . stringify ( updatedGlobalConfig , null , 2 ) } ;` ;
90+ fs . writeFileSync ( globalConfigPath , globalConfigString ) ;
5691 }
92+ } else {
93+ config = {
94+ ...filterEmptyValues ( globalConfig ) ,
95+ ...filterEmptyValues ( localConfig )
96+ } ;
5797 }
58- if ( ! config . key && ( ! config . email || ! config . password ) )
59- await promptForSignInOrKey ( ) ;
60-
61-
62- // Save the config to the file
63- await fs . writeFile ( configFilePath , JSON . stringify ( config , null , 2 ) ) ;
6498
65- return config
99+ return config ;
66100}
0 commit comments