project configuration file support

This commit is contained in:
Laura Liberda 2021-02-23 23:19:52 +01:00
parent 532097c779
commit d6f34a7547
3 changed files with 42 additions and 7 deletions

View file

@ -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');

View file

@ -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;
}>;

View file

@ -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;