Skip to content

Commit 4c5cc6c

Browse files
committed
fix: now openPackageRepository command is using almost 1:1 npm repo implementation. The only difference is that it will use master instead of HEAD in url branch.
fix: disable `codeAction.resolveBranchName` by default (performance-first)
1 parent 9ffa0f5 commit 4c5cc6c

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

src/commands/npmOpenRepository.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import hostedGitInfo from 'hosted-git-info'
2+
3+
export const getPackageRepositoryUrl = (spec, { repository }, handleDirectory) => {
4+
const r = repository
5+
const rurl = r ? (typeof r === 'string' ? r : typeof r === 'object' && typeof r.url === 'string' ? r.url : null) : null
6+
7+
if (!rurl)
8+
throw Object.assign(new Error('no repository'), {
9+
pkgid: spec,
10+
})
11+
12+
const info = hostedFromMani({ repository })
13+
const url = info ? info.browse(handleDirectory ? repository.directory : undefined) : unknownHostedUrl(rurl)
14+
15+
if (!url)
16+
throw Object.assign(new Error('no repository: could not get url'), {
17+
pkgid: spec,
18+
})
19+
20+
return url
21+
}
22+
23+
const hostedFromMani = ({ repository }) => {
24+
const r = repository
25+
const rurl = r ? (typeof r === 'string' ? r : typeof r === 'object' && typeof r.url === 'string' ? r.url : null) : null
26+
27+
// hgi returns undefined sometimes, but let's always return null here
28+
return (rurl && hostedGitInfo.fromUrl(rurl.replace(/^git\+/, ''))) || null
29+
}
30+
31+
const unknownHostedUrl = url => {
32+
try {
33+
const { protocol, hostname, pathname } = new URL(url)
34+
35+
/* istanbul ignore next - URL ctor should prevent this */
36+
if (!protocol || !hostname) return null
37+
38+
const proto = /(git\+)http:$/.test(protocol) ? 'http:' : 'https:'
39+
const path = pathname.replace(/\.git$/, '')
40+
return `${proto}//${hostname}${path}`
41+
} catch {
42+
return null
43+
}
44+
}

src/commands/openPackageAt.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as vscode from 'vscode'
22
import defaultBranch from 'default-branch'
3-
import { fromUrl } from 'hosted-git-info'
43
import { getExtensionCommandId, getExtensionSetting, registerExtensionCommand, RegularCommands, showQuickPick } from 'vscode-framework'
54
import { getCurrentWorkspaceRoot } from '@zardoy/vscode-utils/build/fs'
65
import { noCase } from 'change-case'
76
import { findUpNodeModules, pickInstalledDeps, readDirPackageJson } from '../commands-core/packageJson'
87
import { joinPackageJson, supportedFileSchemes } from '../commands-core/util'
8+
import { getPackageRepositoryUrl } from './npmOpenRepository'
99

1010
/** get module dir URI from closest node_modules */
1111
const getClosestModulePath = async (module: string, path = '') => {
@@ -53,38 +53,34 @@ export const registerOpenPackageAtCommands = () => {
5353
if (!module) module = await pickInstalledDeps({ commandId, multiple: false, flatTypes: false })
5454
if (module === undefined) return
5555
const cwd = await getClosestModulePath(module)
56-
let { repository } = await readDirPackageJson(cwd)
56+
const manifest = await readDirPackageJson(cwd)
57+
const { repository } = manifest
5758
if (!repository) {
5859
// TODO try to resolve url automatically
5960
const action = await vscode.window.showWarningMessage(`${module} doesn't have repository field`, 'Open on NPM')
6061
if (action === 'Open on NPM') await vscode.commands.executeCommand(getExtensionCommandId('openOnNpm'), module)
6162
return
6263
}
6364

64-
// TODO use lib from vsce
65-
// TODO support just url
66-
let repoDir: string | undefined
67-
if (typeof repository === 'object') {
68-
repoDir = repository.directory
69-
repository = repository.url
70-
}
71-
72-
const repo = fromUrl(repository)!
73-
let urlPath = ''
74-
if (repo.domain === 'github.com' && repoDir) {
75-
let branchName: string
65+
const handlePathManually = typeof repository === 'object' && repository.url.startsWith('https://github.com/') && repository.directory
66+
let openPath = ''
67+
if (handlePathManually) {
68+
let branchName = 'master'
7669
if (getExtensionSetting('codeAction.resolveBranchName')) {
77-
console.time('get branch')
78-
branchName = await defaultBranch(`${repo.user}/${repo.project}`)
79-
console.timeEnd('get branch')
80-
} else {
81-
branchName = 'master'
70+
console.time('get repo github branch')
71+
branchName = await defaultBranch(repository.url)
72+
console.timeEnd('get repo github branch')
8273
}
8374

84-
urlPath = `/tree/${branchName}/${repoDir}`
75+
openPath = `/tree/${branchName}/${repository.directory!.replace(/^\//, '')}`
8576
}
8677

87-
await vscode.env.openExternal((repo.browse() + urlPath) as any)
78+
try {
79+
const url: string = getPackageRepositoryUrl(module, { repository }, !handlePathManually)
80+
await vscode.commands.executeCommand('vscode.open', url.replace(/\/$/, '') + openPath)
81+
} catch (err) {
82+
await vscode.window.showWarningMessage(err.message ?? err.stack)
83+
}
8884
})
8985

9086
registerExtensionCommand('revealInExplorer', async ({ command: commandId }, module?: string) => {

src/configurationType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export type Configuration = {
212212
'yarnBerry.workspaceAutoInstall': boolean
213213
/**
214214
* Disable if action open repository takes a lot of time, in this case it would use `master` branch in url
215-
* @default true
215+
* @default false
216216
*/
217217
'codeAction.resolveBranchName': boolean
218218
runOnSave: RunOnSaveRule[]

src/configurationTypeCache.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// GENERATED. DON'T EDIT MANUALLY
2-
// md5hash: b133959f2da203c7532b20a88fa054bf
2+
// md5hash: a615e5beebf47dae76bfdc05804435e9
33
{
44
"type": "object",
55
"properties": {
@@ -191,7 +191,7 @@
191191
},
192192
"codeAction.resolveBranchName": {
193193
"description": "Disable if action open repository takes a lot of time, in this case it would use `master` branch in url",
194-
"default": true,
194+
"default": false,
195195
"type": "boolean"
196196
},
197197
"runOnSave": {

0 commit comments

Comments
 (0)