-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: migrate init to clack prompts * fix * fix empty input bug * refactor
- Loading branch information
1 parent
4b04eaf
commit cf6f100
Showing
24 changed files
with
984 additions
and
1,720 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import readline from 'readline'; | ||
import { texts } from './texts.js'; | ||
|
||
let rl: readline.promises.Interface; | ||
|
||
export function initInput() { | ||
rl = readline.promises.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
rl.on('SIGINT', () => { | ||
if (interruptHandler) { | ||
interruptHandler(); | ||
} | ||
|
||
rl.close(); | ||
}); | ||
} | ||
|
||
export function closeInput() { | ||
readline.clearLine(process.stdout, 0); | ||
readline.cursorTo(process.stdout, 0); | ||
rl.close(); | ||
} | ||
|
||
export async function readUserInput(): Promise<string> { | ||
let answer = ''; | ||
while (!answer.trim()) { | ||
answer = await rl.question(`${texts.userLabel} `); | ||
} | ||
|
||
return answer; | ||
} | ||
|
||
let interruptHandler: (() => void) | undefined; | ||
|
||
export function setInterruptHandler(handler: () => void) { | ||
interruptHandler = handler; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { intro, confirm, log, outro, select, isCancel, text, note } from '@clack/prompts'; | ||
import type { CommandModule } from 'yargs'; | ||
import { checkIfConfigExists, writeConfigFile } from '../config-file.js'; | ||
import { ProviderName, providers, providersMap } from '../engine/providers/provider.js'; | ||
import { outputError } from '../output.js'; | ||
|
||
export const command: CommandModule<{}> = { | ||
command: ['init'], | ||
describe: 'Basic configuration of AI CLI.', | ||
handler: () => run(), | ||
}; | ||
|
||
export async function run() { | ||
try { | ||
intro('Welcome to AI CLI!'); | ||
|
||
const hasConfig = checkIfConfigExists(); | ||
if (hasConfig) { | ||
const overwrite = await confirm({ | ||
message: 'Existing "~/.airc.json" file found, do you want to overwrite it?', | ||
initialValue: false, | ||
}); | ||
if (isCancel(overwrite)) return; | ||
|
||
if (!overwrite) { | ||
outro('Nothing to do. Exiting.'); | ||
return; | ||
} | ||
} | ||
|
||
log.message("Let's set you up quickly."); | ||
|
||
const provider = await select({ | ||
message: 'Which AI provider would you like to use:', | ||
options: providers.map((p) => ({ label: p.label, value: p.name })), | ||
}); | ||
if (isCancel(provider)) return; | ||
|
||
const apiKey = await text({ | ||
message: `Paste ${providersMap[provider].label} API key here:`, | ||
placeholder: `Get your key at ${providersMap[provider].apiKeyUrl}`, | ||
validate: (value) => { | ||
if (!value) { | ||
return 'API key is required.'; | ||
} | ||
}, | ||
}); | ||
if (isCancel(apiKey)) return; | ||
|
||
writeConfig(provider, apiKey); | ||
log.step('Written your settings into "~/.airc.json" file.'); | ||
|
||
note('$ ai "Tell me a useful productivity hack"', 'You can now use AI CLI'); | ||
|
||
outro('Done!'); | ||
} catch (error) { | ||
outputError(error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
function writeConfig(provider: ProviderName, apiKey: string) { | ||
writeConfigFile({ | ||
providers: { | ||
[provider]: { apiKey }, | ||
}, | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.