diff --git a/src/args.ts b/src/args.ts index 749582a..72a847c 100644 --- a/src/args.ts +++ b/src/args.ts @@ -1,4 +1,6 @@ import { CAC } from 'cac' +import { defaultConfig } from './config' +import { omit } from './helpers/omit' import { OptionalConfig } from './types' export function parseCliOptionsToGitArgs( @@ -8,9 +10,17 @@ export function parseCliOptionsToGitArgs( const args = [] + if (options.shallow) { + args.push('--depth', 1) + } + + // clean + options = omit(options, Object.keys(defaultConfig)) + for (const [key, value] of Object.entries(options)) { args.push(`${key.length !== 1 ? '-' : ''}-${key}`) + // drop `true` and `false` if (typeof value !== 'boolean') { args.push(value) } diff --git a/src/commands/get.ts b/src/commands/get.ts index cecc67b..a077e76 100644 --- a/src/commands/get.ts +++ b/src/commands/get.ts @@ -7,9 +7,11 @@ export const get: PluginApi = { api.cli .command('get [repo]', 'Clone/sync with a remote repository') .alias('clone') + .option('--shallow', 'Shallow clone, alias to `--depth 1`') .example('ghq get 2nthony/ghq') .example('ghq get github.com/2nthony/ghq') .example('ghq get https://github.com/2nthony/ghq') + .example('ghq clone 2nthony/ghq') .allowUnknownOptions() .action((repo, options) => { if (!repo) { diff --git a/src/config.ts b/src/config.ts index 874ccdb..f2cde92 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,8 +6,9 @@ import { expandTildePath, join } from './path' export const ghqConfigFileName = '.ghqrc' export const userConfigFilePath = join(homedir(), ghqConfigFileName) -const defaultConfig: Config = { +export const defaultConfig: Config = { root: '~/ghq', + shallow: false, } export async function resolveConfig() { diff --git a/src/helpers/omit.ts b/src/helpers/omit.ts new file mode 100644 index 0000000..16e4d4b --- /dev/null +++ b/src/helpers/omit.ts @@ -0,0 +1,10 @@ +export function omit( + obj: T, + keys: K[], +): Omit { + const newObj = { ...obj } + + keys.forEach((k) => delete newObj[k]) + + return newObj +} diff --git a/src/types.ts b/src/types.ts index 242629e..28a9307 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,6 +16,7 @@ export type Repo = { export type Config = { root: string + shallow: boolean } export type OptionalConfig = Partial diff --git a/test/args.test.ts b/test/args.test.ts new file mode 100644 index 0000000..e053af7 --- /dev/null +++ b/test/args.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, test } from 'vitest' +import { parseCliOptionsToGitArgs } from '../src/args' +import { defaultConfig } from '../src/config' + +describe('parse cli options to args', () => { + test('empty', () => { + const args = parseCliOptionsToGitArgs(defaultConfig) + expect(args).toEqual([]) + }) + + test('shallow', () => { + const args = parseCliOptionsToGitArgs({ ...defaultConfig, shallow: true }) + expect(args).toEqual(['--depth', 1]) + }) +})