Skip to content

Commit

Permalink
feat: get --shallow (#15)
Browse files Browse the repository at this point in the history
* feat: omit helper

* feat: shallow type

* feat: expose default config

* feat: clean args

* test: args

* feat(command): add shallow option

* chore: misc
  • Loading branch information
2nthony authored Jul 13, 2022
1 parent ef74ee3 commit af11ded
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CAC } from 'cac'
import { defaultConfig } from './config'
import { omit } from './helpers/omit'
import { OptionalConfig } from './types'

export function parseCliOptionsToGitArgs(
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions src/commands/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/omit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function omit<T extends object, K extends keyof T>(
obj: T,
keys: K[],
): Omit<T, K> {
const newObj = { ...obj }

keys.forEach((k) => delete newObj[k])

return newObj
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type Repo = {

export type Config = {
root: string
shallow: boolean
}

export type OptionalConfig = Partial<Config>
15 changes: 15 additions & 0 deletions test/args.test.ts
Original file line number Diff line number Diff line change
@@ -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])
})
})

0 comments on commit af11ded

Please sign in to comment.