Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
update api url and support use extension on specific file suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
sinchang committed Dec 6, 2019
1 parent e10a63f commit 0801d30
Show file tree
Hide file tree
Showing 3 changed files with 842 additions and 1,519 deletions.
44 changes: 27 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@
"categories": [
"Other"
],
"keywords": ["github", "PHP", "Python", "Ruby", "Node"],
"keywords": [
"github",
"PHP",
"Python",
"Ruby",
"Node"
],
"activationEvents": [
"onCommand:extension.goToGitHub"
],
"main": "./out/src/extension",
"contributes": {
"commands": [{
"command": "extension.goToGitHub",
"title": "Go to GitHub"
}],
"menus": {
"editor/context": [{
"when": "",
"commands": [
{
"command": "extension.goToGitHub",
"group": "navigation"
}]
"title": "Go to GitHub"
}
],
"menus": {
"editor/context": [
{
"when": "",
"command": "extension.goToGitHub",
"group": "navigation"
}
]
}
},
"scripts": {
Expand All @@ -36,14 +46,14 @@
"test": "node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"@types/mocha": "^2.2.32",
"@types/node": "^6.0.40",
"mocha": "^2.3.3",
"typescript": "^2.0.3",
"vscode": "^1.1.0"
"@types/mocha": "^5.2.7",
"@types/node": "^12.12.14",
"mocha": "^6.2.2",
"typescript": "^3.7.3",
"vscode": "^1.1.36"
},
"dependencies": {
"axios": "^0.16.1",
"opn": "^5.0.0"
"axios": "^0.19.0",
"opn": "^6.0.0"
}
}
101 changes: 63 additions & 38 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,78 @@ import * as vscode from 'vscode'
import axios from 'axios'
const opn = require('opn')

const fileTypes = [{
fileName: 'package.json',
registry: 'npm'
}, {
fileName: 'requirements.txt',
registry: 'pypi'
}, {
fileName: 'composer.json',
registry: 'composer'
}, {
fileName: 'Gemfile',
registry: 'rubygems'
}]
const fileTypes = [
{
fileName: 'package.json',
suffixs: ['ts', 'tsx', 'js', 'html', 'css', 'less', 'sass', 'scss'],
registry: 'npm'
},
{
fileName: 'requirements.txt',
suffixs: ['py'],
registry: 'pypi'
},
{
fileName: 'composer.json',
suffixs: ['php'],
registry: 'composer'
},
{
fileName: 'Gemfile',
suffixs: ['rb'],
registry: 'rubygems'
}
]

function getFileExtension(filename) {
return filename.split('.').pop()
}

export function activate(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand('extension.goToGitHub', () => {
const editor = vscode.window.activeTextEditor
const disposable = vscode.commands.registerCommand(
'extension.goToGitHub',
() => {
const editor = vscode.window.activeTextEditor

if (!editor) {
return
}
if (!editor) {
return
}

const selection = editor.selection
const text = editor.document.getText(selection)
const currentlyOpenTabfilePath = editor.document.fileName
let split = currentlyOpenTabfilePath.split('/')
if (split[0] === currentlyOpenTabfilePath) // Windows
split = currentlyOpenTabfilePath.split('\\')
const currentFileName = split[split.length - 1]
const selection = editor.selection
const text = editor.document.getText(selection)
const currentlyOpenTabfilePath = editor.document.fileName
let split = currentlyOpenTabfilePath.split('/')
if (split[0] === currentlyOpenTabfilePath)
// Windows
split = currentlyOpenTabfilePath.split('\\')
const currentFileName = split[split.length - 1]
const suffix = getFileExtension(currentFileName)

let registry = ''
let registry = ''

fileTypes.forEach(item => {
if (item.fileName === currentFileName) {
registry = item.registry
}
})
fileTypes.forEach(item => {
if (
item.fileName === currentFileName ||
item.suffixs.includes(suffix)
) {
registry = item.registry
}
})

if (!registry || !text) return
if (!registry || !text) return

axios.get(`https://githublinker.herokuapp.com/q/${registry}/${text}`)
.then(res => opn(res.data.url))
.catch(error => vscode.window.showInformationMessage('Something Error, Please Try Again!'))
})
axios
.get(`https://octolinker-api.now.sh?${registry}=${text}`)
.then(res => {
if (res.data.result && res.data.result.length > 0) {
opn(res.data.result[0].result)
}
})
.catch(error => vscode.window.showInformationMessage(error.message))
}
)

context.subscriptions.push(disposable)
}

export function deactivate() {
}
export function deactivate() {}
Loading

0 comments on commit 0801d30

Please sign in to comment.