Skip to content

Commit

Permalink
fix: default git repository value if exist
Browse files Browse the repository at this point in the history
  • Loading branch information
milaninfy committed Dec 2, 2024
1 parent eab6037 commit 0884298
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/default-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,17 @@ if (!package.scripts) {

if (!package.repository) {
exports.repository = async () => {
const gconf = await fs.readFile('.git/config', 'utf8').catch(() => '')
const gitConfigPath = path.resolve(dirname, '.git', 'config')
const gconf = await fs.readFile(gitConfigPath, 'utf8').catch(() => '')
const lines = gconf.split(/\r?\n/)

let url
const i = lines.indexOf('[remote "origin"]')

if (i !== -1) {
url = gconf[i + 1]
url = lines[i + 1]
if (!url.match(/^\s*url =/)) {
url = gconf[i + 2]
url = lines[i + 2]
}
if (!url.match(/^\s*url =/)) {
url = null
Expand Down
31 changes: 31 additions & 0 deletions test/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,34 @@ t.test('license', async (t) => {
}
t.has(data, wanted)
})

t.test('repository from git config', async (t) => {
const testdir = {
'.git': {
config: `
[remote "origin"]
url = https://github.com/npm/cli.git`,
} }

const { data } = await setup(t, __filename, {
inputs: [
'the-name', // package name
'', // version
'', // description
'', // entry point
'', // test
'', // git repo (emoty, will be filled in by git config)
'', // keywords
'', // author
'', // license
'yes', // about to write
],
testdir,
})

const wanted = {
type: 'git',
url: 'git+https://github.com/npm/cli.git',
}
t.has(data.repository, wanted)
})

0 comments on commit 0884298

Please sign in to comment.