Skip to content

Commit

Permalink
refactor: move exec inside cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 4, 2024
1 parent 3f7f5e0 commit 0a07b83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
18 changes: 15 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
#!/usr/bin/env node

const { execSync } = require('child_process')
const mri = require('mri')

const { _, ...flags } = mri(process.argv.slice(2), {
default: {
token: process.env.GH_TOKEN || process.env.GITHUB_TOKEN
token: process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
url: execSync('git remote get-url origin').toString().trim(),
tagName: execSync('git tag --sort=-creatordate')
.toString()
.trim()
.split('\n')[0]
}
})

console.log('DEBUG url', flags.url)

if (flags.help) {
console.log(require('fs').readFileSync('./help.txt', 'utf8'))
process.exit(0)
}

if (!flags.token) {
console.log('\nA GitHub token with `public_repo` permission needs to be provided.')
console.log('It should be exposed via `process.env.GH_TOKEN` or `process.env.GITHUB_TOKEN`.')
console.log(
'\nA GitHub token with `public_repo` permission needs to be provided.'
)
console.log(
'It should be exposed via `process.env.GH_TOKEN` or `process.env.GITHUB_TOKEN`.'
)
}

Promise.resolve(require('..')(flags))
Expand Down
12 changes: 4 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
'use strict'

const { execSync } = require('child_process')

const latestTag = () =>
execSync('git tag --sort=-creatordate').toString().trim().split('\n')[0]

const gitDetails = opts => {
if (opts.owner && opts.repo) return opts
const gitUrl = execSync('git remote get-url origin').toString().trim()
const regex = /github\.com[:/](.*?)\/(.*?)\.git/
const [, owner, repo] = gitUrl.match(regex)
const [, owner, repo] = opts.url.match(regex)
return { owner, repo }
}

Expand Down Expand Up @@ -53,11 +47,13 @@ module.exports = async ({
method: 'POST',
body: JSON.stringify({
name: name ?? undefined,
tag_name: tagName ?? latestTag(),
tag_name: tagName,
generate_release_notes: true,
prerelease,
draft,
body
})
})
}

module.exports.gitDetails = gitDetails

0 comments on commit 0a07b83

Please sign in to comment.