-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(_gr.js): move logic into separate, imported functions
- Loading branch information
1 parent
82f66ee
commit 3f2f7cf
Showing
4 changed files
with
119 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// src/option_handlers/handleConfigOption.js | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import os from 'os'; | ||
import chalk from 'chalk'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
// Function to handle config option | ||
export default function handleConfigOption() { | ||
const configFilePath = path.join(os.homedir(), '.gimme_readme_config'); | ||
|
||
if (!fs.existsSync(configFilePath)) { | ||
// Construct the path to env.sample within the gimme_readme project directory | ||
const sampleFilePath = path.resolve(__dirname, '../../env.sample'); | ||
|
||
let sampleContent; | ||
try { | ||
sampleContent = fs.readFileSync(sampleFilePath, 'utf-8'); | ||
} catch (err) { | ||
console.error(`Could not find env.sample in the project directory: ${err.message}`); | ||
process.exit(1); | ||
} | ||
|
||
// Create a new config file with the sample content | ||
fs.writeFileSync(configFilePath, sampleContent); | ||
console.log(`Configuration file created at: ${chalk.blue(configFilePath)}`); | ||
} else { | ||
console.log(`Configuration file located at: ${chalk.blue(configFilePath)}`); | ||
} | ||
|
||
console.log( | ||
`Please refer to the ${chalk.blue('gimme_readme')} repository for examples on how to configure this file: ${chalk.blue('https://github.com/peterdanwan/gimme_readme')}` | ||
); | ||
|
||
process.exit(0); | ||
} |
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,65 @@ | ||
// src/option_handlers/handleFilesOption.js | ||
|
||
import path from 'path'; | ||
import os from 'os'; | ||
import dotenv from 'dotenv'; | ||
import chalk from 'chalk'; | ||
import ora from 'ora'; | ||
|
||
import getFileContent from '../file_functions/getFileContent.js'; | ||
import promptAI from '../ai.js'; | ||
import defaultPrompt from '../defaultPrompt.js'; | ||
|
||
// Attempt to load .gimme_readme_config's environment variables | ||
const configFilePath = path.join(os.homedir(), '.gimme_readme_config'); | ||
dotenv.config({ path: configFilePath }); | ||
|
||
export default async function handleFilesOption(files, options) { | ||
// Let the user specify their own prompt, or use the prompt that we have engineered | ||
let prompt = options.prompt || process.env.CUSTOM_PROMPT || defaultPrompt; | ||
const model = options.model || process.env.MODEL || 'gemini-1.5-flash'; | ||
const outputFile = options.outputFile || process.env.OUTPUT_FILE || null; // if no options are being specified, the option would be null, which means it will go out to terminal in the console | ||
const temperature = options.temperature || process.env.TEMPERATURE || 0.5; | ||
const needToken = options.token || false; | ||
|
||
const validFiles = []; | ||
|
||
// Check if files is an array and has at least one entry | ||
if (!Array.isArray(files)) { | ||
console.log("error: option '-f, --files [files...]' argument missing "); | ||
process.exit(1); | ||
} | ||
|
||
for (const file of files) { | ||
try { | ||
const content = getFileContent(file); | ||
prompt += content + '\n\n'; | ||
validFiles.push(file); | ||
} catch (error) { | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
if (validFiles.length > 1) { | ||
console.log(chalk.blue('Sending files:')); | ||
for (const validFile of validFiles) { | ||
console.log(`- ${validFile}`); | ||
} | ||
} else { | ||
console.log(`${chalk.blue('Sending file')}: ${validFiles[0]}`); | ||
} | ||
|
||
const spinner = ora(` Waiting for a response from the ${chalk.blue(model)} model...\n`).start(); | ||
|
||
try { | ||
await promptAI(prompt, model, temperature, outputFile, needToken); | ||
spinner.succeed(` Response received from ${chalk.blue(model)} model`); | ||
} catch (error) { | ||
spinner.fail(` Failed to receive response from ${chalk.red(model)} model`); | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
|
||
process.exit(0); | ||
} |
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,6 @@ | ||
// src/option_handlers/handleHelpOption.js | ||
|
||
export default function handleHelpOption(program) { | ||
// Implicit exit code of 0 | ||
program.help(); | ||
} |