From bcc501e52f1d52b3bdeba44d8deb5d841c07a1f9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 17 Feb 2026 18:54:33 +0000 Subject: [PATCH] Refactor cmdInit in src/ops.mjs to improve modularity Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- src/ops.mjs | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/ops.mjs b/src/ops.mjs index d8d4b38..c2076a4 100644 --- a/src/ops.mjs +++ b/src/ops.mjs @@ -152,20 +152,14 @@ async function interactiveInit(cwd) { } } -export async function cmdInit(force = false, cwd = process.cwd(), mockAnswers = null) { - // Use interactive mode to get configuration unless mock answers are provided - let config; +async function resolveConfiguration(cwd, mockAnswers) { if (mockAnswers) { - config = mockAnswers; - } else { - config = await interactiveInit(cwd); + return mockAnswers; } - const { installDir, namespace, relays } = config; + return await interactiveInit(cwd); +} - const paths = getPaths(cwd, installDir); - console.log(`\nInitializing torch in ${paths.torchDir}...`); - - // Check if directory exists and is not empty (unless force is used or we are installing to .) +function ensureInstallDirectory(paths, force, installDir) { if (fs.existsSync(paths.torchDir) && !force) { const entries = fs.readdirSync(paths.torchDir); if (entries.length > 0 && installDir !== '.') { @@ -175,13 +169,17 @@ export async function cmdInit(force = false, cwd = process.cwd(), mockAnswers = ensureDir(paths.torchDir); ensureDir(paths.promptsDir); +} +function installAppAssets(torchDir, installDir) { // 1. Copy App Directories - syncAppDirectories(paths.torchDir, 'Copied'); + syncAppDirectories(torchDir, 'Copied'); // 2. Copy App Files - syncAppFiles(paths.torchDir, installDir, 'Copied'); + syncAppFiles(torchDir, installDir, 'Copied'); +} +function installTorchAssets(paths, installDir) { // 3. Copy Roster const srcRoster = path.join(SRC_PROMPTS_DIR, 'roster.json'); if (fs.existsSync(srcRoster)) { @@ -215,7 +213,9 @@ export async function cmdInit(force = false, cwd = process.cwd(), mockAnswers = console.log(`Created ${files.length} files in ${path.relative(paths.root, destDir)}/`); } } +} +function configureTorch(cwd, paths, installDir, namespace, relays) { // 6. Create/Update torch-config.json const configPath = path.join(paths.root, 'torch-config.json'); @@ -268,12 +268,33 @@ export async function cmdInit(force = false, cwd = process.cwd(), mockAnswers = fs.writeFileSync(configPath, JSON.stringify(configData, null, 2), 'utf8'); console.log(`Saved configuration to ${path.relative(cwd, configPath)}`); +} +function injectHostScriptsIfNeeded(paths, installDir) { // 7. Inject Scripts into Host Package.json // If we are NOT installing to '.', the host package.json is in paths.root if (installDir !== '.') { injectScriptsIntoHost(paths.root, installDir); } +} + +export async function cmdInit(force = false, cwd = process.cwd(), mockAnswers = null) { + // Use interactive mode to get configuration unless mock answers are provided + const config = await resolveConfiguration(cwd, mockAnswers); + const { installDir, namespace, relays } = config; + + const paths = getPaths(cwd, installDir); + console.log(`\nInitializing torch in ${paths.torchDir}...`); + + ensureInstallDirectory(paths, force, installDir); + + installAppAssets(paths.torchDir, installDir); + + installTorchAssets(paths, installDir); + + configureTorch(cwd, paths, installDir, namespace, relays); + + injectHostScriptsIfNeeded(paths, installDir); console.log('\nInitialization complete.'); console.log('You can now customize the files in ' + path.relative(cwd, paths.torchDir) + '/');