Skip to content

Commit

Permalink
Release: v0.11.0 #42
Browse files Browse the repository at this point in the history
Release: v0.11.0
  • Loading branch information
mnrendra authored Jan 24, 2023
2 parents fadec09 + 790d2ab commit d9952d4
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gitidy",
"version": "0.10.0",
"version": "0.11.0",
"description": "A tool for tidying Git flow.",
"private": false,
"main": "dist",
Expand Down
14 changes: 12 additions & 2 deletions src/core/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
gitVersion,
gitClone
gitClone,
gitCheckout
} from '@git'

import {
ghVersion,
ghAuthStatus,
ghRepoCreate
ghRepoCreate,
ghAPIs
} from '@gh'

import {
Expand All @@ -33,11 +35,19 @@ const main = async (args?: string[]) => {

// create Github's repo
const { owner, name } = await ghRepoCreate(repoName, { verbose: true })
// create Github's repo branches
const refs = await ghAPIs.refs({ owner, repo: name })
const main = await refs.get('main', { verbose: true })
const dev = await refs.post(main.object.sha, 'dev', { verbose: true })
const feat = await refs.post(dev.object.sha, 'feat/init_project', { verbose: true })

// clone and restore backup source codes
await gitClone(`${owner}/${name}`, { verbose: true })
await restoreRepo(repoName, { verbose: true })

// checkout feat/init_project
await gitCheckout('feat/init_project', { verbose: true })

log(c.greenBright(`Done!`))
log(c.green(`New ${c.greenBright('git')} and ${c.greenBright('GitHub')} repository successfully created!`))
log(c.green(`GitHub repository: ${c.greenBright(`https://github.com/${owner}/${name}`)}`))
Expand Down
7 changes: 7 additions & 0 deletions src/gh/ghAPIs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import refs, { getRefs, postRefs } from './refs'

export {
refs,
getRefs,
postRefs
}
28 changes: 28 additions & 0 deletions src/gh/ghAPIs/refs/deleteRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { exec } from '@cp'
import log, { c } from '@clog'
import { Options } from './types'

const deleteRefs = async (owner: string, repo: string, branch: string, {
verbose
}: Options = {}) => new Promise<string>((resolve, reject) => {
try {
const args = ['api']

args.push('--method DELETE')
args.push('-H "Accept: application/vnd.github.v3+json"')
args.push(`/repos/${owner}/${repo}/git/refs/heads/${branch}`)

const cli = ['gh', ...args].join(' ')

verbose && log(c.blue(`• ${cli}`))

exec(cli).then(({ stdall }) => {
verbose && log(c.grey(` branch: ${branch}`))
resolve(stdall)
}).catch(reject)
} catch (err) {
reject(err)
}
})

export default deleteRefs
33 changes: 33 additions & 0 deletions src/gh/ghAPIs/refs/getRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { exec } from '@cp'
import log, { c } from '@clog'
import { Data, Options } from './types'

const getRefs = async (owner: string, repo: string, branch: string, {
verbose
}: Options = {}) => new Promise<Data>((resolve, reject) => {
try {
const args = ['api']

args.push('--method GET')
args.push('-H "Accept: application/vnd.github.v3+json"')
args.push(`/repos/${owner}/${repo}/git/refs/heads/${branch}`)

const cli = ['gh', ...args].join(' ')

verbose && log(c.blue(`• ${cli}`))

exec(cli).then(({ stdall }) => {
try {
const data: Data = JSON.parse(stdall)
verbose && log(c.grey(` sha: ${data && data.object && data.object.sha ? data.object.sha : '-'}`))
resolve(data)
} catch (err) {
reject(err)
}
}).catch(reject)
} catch (err) {
reject(err)
}
})

export default getRefs
12 changes: 12 additions & 0 deletions src/gh/ghAPIs/refs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getRefs from './getRefs'
import postRefs from './postRefs'
import deleteRefs from './deleteRefs'
import refs from './refs'

export {
getRefs,
postRefs,
deleteRefs
}

export default refs
35 changes: 35 additions & 0 deletions src/gh/ghAPIs/refs/postRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { exec } from '@cp'
import log, { c } from '@clog'
import { Data, Options } from './types'

const postRefs = async (owner: string, repo: string, sha: string, branch: string, {
verbose
}: Options = {}) => new Promise<Data>((resolve, reject) => {
try {
const args = ['api']

args.push('--method POST')
args.push('-H "Accept: application/vnd.github.v3+json"')
args.push(`/repos/${owner}/${repo}/git/refs`)
args.push(`-f ref="refs/heads/${branch}"`)
args.push(`-f sha="${sha}"`)

const cli = ['gh', ...args].join(' ')

verbose && log(c.blue(`• ${cli}`))

exec(cli).then(({ stdall }) => {
try {
const data: Data = JSON.parse(stdall)
verbose && log(c.grey(` sha: ${data && data.object && data.object.sha ? data.object.sha : '-'}, branch: ${branch}`))
resolve(data)
} catch (err) {
reject(err)
}
}).catch(reject)
} catch (err) {
reject(err)
}
})

export default postRefs
18 changes: 18 additions & 0 deletions src/gh/ghAPIs/refs/refs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getRefs from './getRefs'
import postRefs from './postRefs'
import deleteRefs from './deleteRefs'
import { RefsProps, Options } from './types'

const refs = async ({ owner, repo }: RefsProps) => {
const get = async (branch: string, opt: Options) => await getRefs(owner, repo, branch, opt)
const post = async (sha: string, branch: string, opt: Options) => await postRefs(owner, repo, sha, branch, opt)
const del = async (branch: string, opt: Options) => await deleteRefs(owner, repo, branch, opt)

return {
get,
post,
delete: del
}
}

export default refs
21 changes: 21 additions & 0 deletions src/gh/ghAPIs/refs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type Object = {
sha: string,
type: string,
url: string
}

export type Data = {
ref: string,
node_id: string,
url: string,
object: Object,
}

export type RefsProps = {
owner: string,
repo: string
}

export type Options = {
verbose?: boolean,
}
4 changes: 3 additions & 1 deletion src/gh/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import ghVersion from './ghVersion'
import ghAuthStatus from './ghAuthStatus'
import ghRepoCreate from './ghRepoCreate'
import * as ghAPIs from './ghAPIs'

export {
ghVersion,
ghAuthStatus,
ghRepoCreate
ghRepoCreate,
ghAPIs
}
25 changes: 25 additions & 0 deletions src/git/gitCheckout/gitCheckout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { exec } from '@cp'
import log, { c } from '@clog'
import { Options } from './types'

const gitCheckout = async (branch: string, {
verbose
}: Options = {}) => {
const cmd = `git checkout ${branch}`

verbose && log(c.blue(`• ${cmd}`))

const { stdall } = await exec(cmd)

if (stdall.includes('command not found')) {
log(c.red(`You don't have ${c.bold('Git')}.`))
log(c.red(`Please follow ${c.underline('https://git-scm.com/downloads')} to install ${c.bold('git')} command line.`))
process.exit()
}

verbose && log(c.grey(` ${stdall}`))

return stdall
}

export default gitCheckout
3 changes: 3 additions & 0 deletions src/git/gitCheckout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import gitCheckout from './gitCheckout'

export default gitCheckout
3 changes: 3 additions & 0 deletions src/git/gitCheckout/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Options = {
verbose?: boolean,
}
4 changes: 3 additions & 1 deletion src/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import gitVersion from './gitVersion'
import gitClone from './gitClone'
import gitCheckout from './gitCheckout'

export {
gitVersion,
gitClone
gitClone,
gitCheckout
}

0 comments on commit d9952d4

Please sign in to comment.