import chalk from 'chalk'; import fs from 'node:fs'; import path from 'node:path'; import { CollectionModuleConfig, isCollectionModuleConfig, } from '../domain/collection-modules/collection-module-config'; import { RootConfigWrite } from '../domain/root-config'; import { TopLevelFileName } from '../domain/file-names'; import { getErrorMessage } from './get-error-message'; const indentLines = (text: string): string[] => text.split('\n').map((line) => ` ${line}`); export const readAuthAndConfig = (directory?: string) => { let apiKey: string; let config: RootConfigWrite | CollectionModuleConfig; const moduleDirectory = path.join(directory || './'); // Check that .root-auth exists and parse the contents out const rootAuthPath = path.join(moduleDirectory, TopLevelFileName.RootAuth); try { const content = fs.readFileSync(rootAuthPath, { encoding: 'utf8' }); [, apiKey] = content.split('ROOT_API_KEY='); } catch (error) { throw new Error( [ `Could not read ${chalk.yellow(TopLevelFileName.RootAuth)}`, `File: ${chalk.yellow(rootAuthPath)}`, '', 'Underlying error:', ...indentLines(getErrorMessage(error)), '', `Tip: ensure this directory is a Root product module — run ${chalk.yellow( 'rp pull', )} from a directory containing a valid ${chalk.yellow(TopLevelFileName.RootAuth)} file.`, ].join('\n'), ); } // Check that .root-config.json exists and parse the contents out const configPath = path.join(moduleDirectory, TopLevelFileName.RootConfig); try { const content = fs.readFileSync(configPath, { encoding: 'utf8' }); config = JSON.parse(content); } catch (error) { throw new Error( [ `Could not read ${chalk.yellow(TopLevelFileName.RootConfig)}`, `File: ${chalk.yellow(configPath)}`, '', 'Underlying error:', ...indentLines(getErrorMessage(error)), '', `Tip: ensure this directory is a Root product module — run ${chalk.yellow( 'rp pull', )} from a directory containing a valid ${chalk.yellow(TopLevelFileName.RootConfig)} file.`, ].join('\n'), ); } // Naive test to check if the json file is valid if (!config.host || !config.organizationId) { throw new Error( [ `Invalid ${chalk.yellow(TopLevelFileName.RootConfig)}`, `File: ${chalk.yellow(configPath)}`, '', `Tip: the config must include both ${chalk.yellow('host')} and ${chalk.yellow( 'organizationId', )}. Run ${chalk.yellow('rp pull')} to refresh the file from the platform.`, ].join('\n'), ); } // Backwards compatibility for collection modules without manual transactions if (isCollectionModuleConfig(config) && !config.manualTransactions) { config.manualTransactions = []; } // Backwards compatibility for collection modules without settings if (!config.settings) { config.settings = {}; } if (!apiKey) { throw new Error( [ `Missing ${chalk.yellow('ROOT_API_KEY')} in ${chalk.yellow(TopLevelFileName.RootAuth)}`, `File: ${chalk.yellow(rootAuthPath)}`, '', `Tip: open ${chalk.yellow(rootAuthPath)} and confirm it contains a line like ${chalk.yellow( 'ROOT_API_KEY=', )}.`, ].join('\n'), ); } return { apiKey: apiKey.trim(), config }; };