11import { ExitPromptError } from '@inquirer/core'
22import * as inquirer from '@inquirer/prompts'
33import chalk from 'chalk'
4+ import * as fs from "../util/fs.js" ;
45import slugify from "@sindresorhus/slugify"
5- import * as fs from 'node:fs'
66import * as path from 'node:path'
77import { Interface } from 'node:readline'
88
@@ -88,24 +88,24 @@ export async function confirmOverwriteCiHypFile(): Promise<boolean> {
8888 } )
8989}
9090
91- export function confirmExistingProjectLink ( ) : Promise < boolean > {
91+ export async function confirmExistingProjectLink ( ) : Promise < boolean > {
9292 return inquirer . confirm ( {
9393 default : true ,
9494 message : 'You have existing projects with no linked repositories. Would you like to select from these projects?' ,
9595 } )
9696}
9797
98- export function getEnvDir ( ) : string {
98+ export function getSettingsDir ( ) : string {
9999 return path . join ( process . env . HOME || '' , '.hypermode' )
100100}
101101
102- export function getEnvFilePath ( ) : string {
103- const envDir = getEnvDir ( )
104- return path . join ( envDir , 'settings.json' )
102+ export function getSettingsFilePath ( ) : string {
103+ const settingsDir = getSettingsDir ( )
104+ return path . join ( settingsDir , 'settings.json' )
105105}
106106
107- export function fileExists ( filePath : string ) : boolean {
108- return fs . existsSync ( filePath )
107+ export async function fileExists ( filePath : string ) : Promise < boolean > {
108+ return fs . exists ( filePath )
109109}
110110
111111export function getGitDir ( ) : string {
@@ -124,8 +124,8 @@ export function getGitConfigFilePath(): string {
124124 return path . join ( getGitDir ( ) , 'config' )
125125}
126126
127- export function getGitRemoteUrl ( filePath : string ) : string {
128- const content = fs . readFileSync ( filePath , 'utf8' )
127+ export async function getGitRemoteUrl ( filePath : string ) : Promise < string > {
128+ const content = await fs . readFile ( filePath , 'utf8' )
129129 const remoteMatch = content . match ( / \[ r e m o t e " o r i g i n " ] \n \s + u r l = ( .* ) / )
130130 if ( ! remoteMatch ) {
131131 throw new Error ( chalk . red ( 'No remote origin found in .git/config, please set up a remote origin with `git remote add origin <url>`.' ) )
@@ -134,14 +134,8 @@ export function getGitRemoteUrl(filePath: string): string {
134134 return remoteMatch [ 1 ]
135135}
136136
137- export function readSettingsJson ( filePath : string ) : {
138- content : string
139- email : null | string
140- installationIds : { [ key : string ] : string } | null
141- jwt : null | string
142- orgId : null | string
143- } {
144- const content = fs . readFileSync ( filePath , 'utf8' )
137+ export async function readSettingsJson ( filePath : string ) : Promise < { content : string ; email : null | string ; installationIds : { [ key : string ] : string ; } | null ; jwt : null | string ; orgId : null | string ; } > {
138+ const content = await fs . readFile ( filePath , 'utf8' )
145139
146140 let email : null | string = null
147141 let jwt : null | string = null
@@ -162,3 +156,45 @@ export function readSettingsJson(filePath: string): {
162156 content, email, installationIds, jwt, orgId,
163157 }
164158}
159+
160+ export async function writeToSettingsFile ( jwt : string , email : string , orgId : string ) : Promise < void > {
161+ const settingsDir = getSettingsDir ( )
162+ const settingsFilePath = getSettingsFilePath ( )
163+
164+ // Create the directory if it doesn't exist
165+ if ( ! await fileExists ( settingsDir ) ) {
166+ await fs . mkdir ( settingsDir , { recursive : true } )
167+ }
168+
169+ const newSettingsContent : { HYP_EMAIL : string ; HYP_JWT : string ; HYP_ORG_ID : string ; INSTALLATION_IDS : { [ key : string ] : string } | null } = {
170+ HYP_EMAIL : email ,
171+ HYP_JWT : jwt ,
172+ HYP_ORG_ID : orgId ,
173+ INSTALLATION_IDS : null ,
174+ } ;
175+
176+ if ( await fileExists ( settingsFilePath ) ) {
177+ const settings = await readSettingsJson ( settingsFilePath )
178+ newSettingsContent . INSTALLATION_IDS = settings . installationIds
179+ }
180+
181+ // Write the new content to the file, replacing any existing content
182+ await fs . writeFile ( settingsFilePath , JSON . stringify ( newSettingsContent , null , 2 ) , { flag : 'w' } )
183+ }
184+
185+ export async function writeGithubInstallationIdToSettingsFile ( gitOwner : string , installationId : string ) : Promise < void > {
186+ const settingsFilePath = getSettingsFilePath ( )
187+ const settings = await readSettingsJson ( settingsFilePath )
188+
189+ settings . installationIds = settings . installationIds || { }
190+ settings . installationIds [ gitOwner ] = installationId
191+
192+ const newSettingsContent = {
193+ HYP_EMAIL : settings . email ,
194+ HYP_JWT : settings . jwt ,
195+ HYP_ORG_ID : settings . orgId ,
196+ INSTALLATION_IDS : settings . installationIds ,
197+ }
198+
199+ await fs . writeFile ( settingsFilePath , JSON . stringify ( newSettingsContent , null , 2 ) , { flag : 'w' } )
200+ }
0 commit comments