diff --git a/src/cli/replicate.ts b/src/cli/replicate.ts index 2b59316..0ef616a 100644 --- a/src/cli/replicate.ts +++ b/src/cli/replicate.ts @@ -2,7 +2,7 @@ import { Command, flags } from '@oclif/command'; import assert from 'assert'; import Copykitku from '../copykitku'; import { ENTITY_TYPE } from '../types'; -import { parsePath } from '../utils'; +import { getProjectConfig, parsePath } from '../utils'; export default class Replicate extends Command { static description = 'Replicate issues/MRs between repositories'; @@ -35,18 +35,31 @@ export default class Replicate extends Command { }, { name: 'dest', - required: true, - description: 'where you want the thing to be replicated', + required: false, + description: + 'where you want the thing to be replicated [required in either cli arg or .copykitkurc.toml file]', }, ]; async run() { const { flags, args } = this.parse(Replicate); - const { destBranch, doNotCommit, remote, targetBranch } = flags; + const proj = getProjectConfig(); - const sourcePath = parsePath(args.source); - const destPath = parsePath(args.dest); + const { destBranch, doNotCommit, remote, targetBranch } = { ...proj, ...flags }; + + const { source, dest } = { + ...proj, + ...(args as { + source: string; + dest?: string; + }), + }; + + assert(typeof dest === 'string', 'Destination repository must be defined'); + + const sourcePath = parsePath(source); + const destPath = parsePath(dest); assert(sourcePath.entityID, 'Source must be a repo element, not a repo itself'); diff --git a/src/types.ts b/src/types.ts index 68d0078..8fe3bbe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -160,3 +160,8 @@ export interface Actor { display_name?: string | null; vendor: Vendor; } + +export type CopykitkuProjectConfig = Partial<{ + /** destination repository url (like "https://git.sakamoto.pl/laudompat/copykitku") */ + dest: string; +}>; diff --git a/src/utils.ts b/src/utils.ts index c61fe11..63e6507 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,7 +9,8 @@ import { readFileSync, writeFileSync } from 'fs-extra'; import toml from '@iarna/toml'; import appdataPath from 'appdata-path'; -import { CopykitkuConfig, ENTITY_TYPE } from './types'; +import path from 'path'; +import { CopykitkuConfig, CopykitkuProjectConfig, ENTITY_TYPE } from './types'; export const DEFAULT_CONFIG: CopykitkuConfig = { vendorConfigs: [], @@ -34,6 +35,22 @@ export const setConfig = (config: CopykitkuConfig) => encoding: 'utf-8', }); +export const getProjectConfigPath = () => path.join(process.cwd(), '.copykitkurc.toml'); + +export const getProjectConfig = () => { + try { + const file = readFileSync(getProjectConfigPath()); + return (toml.parse(file.toString('utf-8')) as unknown) as CopykitkuProjectConfig; + } catch (err) { + if (err.code === 'ENOENT') { + return {}; + } else { + // project config file exists, but cannot be accessed/parsed for some reason + throw err; + } + } +}; + export type Path = | { domain: string;