Skip to content

Commit

Permalink
update github repo url
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthkp committed Mar 23, 2022
1 parent a96fad2 commit 68b3952
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
54 changes: 38 additions & 16 deletions packages/babel-plugin-open-source/babel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('fs');
const ini = require('ini');
const path = require('path');
const { declare } = require('@babel/helper-plugin-utils');
const { types: t } = require('@babel/core');
const dotenv = require('dotenv');
Expand Down Expand Up @@ -69,25 +72,10 @@ module.exports = declare((api) => {
} else if (editor === 'vscode-insiders') {
url = `vscode-insiders://file/${state.filename}:${location.start.line}`;
} else if (editor === 'github') {
const gitconfig = require('gitconfiglocal');
const findGitRoot = require('find-git-root');

gitconfig('./', function (err, config) {
if (err) {
console.log('could not parse remote origin');
return;
}
const repo = config.remote.origin.url.replace('[email protected]:', '').replace('.git', '');
const gitRoot = findGitRoot(state.filename).replace('.git', '');
const filePath = state.filename.replace(gitRoot, '');
const branchName = process.env.GITHUB_HEAD_REF || 'main';

url = `https://github.com/${repo}/blob/${branchName}/${filePath}`;
});
url = getGitHubUrl(state.filename, location.start.line);
} else {
url = `vscode://file/${state.filename}:${location.start.line}`;
}

const sourceData = JSON.stringify({ url });

path.container.openingElement.attributes.push(
Expand All @@ -101,3 +89,37 @@ module.exports = declare((api) => {
visitor
};
});

const getGitHubUrl = (localFilePath, lineNumber) => {
const findGitRoot = require('find-git-root');

const repo = getRepositoryPath();
const gitRoot = findGitRoot(localFilePath).replace('.git', '');
const filePath = localFilePath.replace(gitRoot, '');
const branchName = process.env.GITHUB_HEAD_REF || 'main';

return `https://github.com/${repo}/blob/${branchName}/${filePath}#L${lineNumber}`;
};

function findGit() {
var directory = path.resolve('./', '.git', 'config');
const exists = fs.existsSync(directory);
if (exists) return directory;

if ('./' === path.resolve('./', '..')) return false;
findGit();
}

const getRepositoryPath = () => {
const gitPath = findGit();

if (!gitPath) {
console.log('Could not find .git');
return;
}

const result = fs.readFileSync(gitPath, 'utf8');
const config = ini.parse(result);

return config['remote "origin"'].url.replace('[email protected]:', '').replace('.git', '');
};
2 changes: 1 addition & 1 deletion packages/babel-plugin-open-source/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-open-source",
"version": "1.1.1",
"version": "1.2.2",
"description": "Alt + Click on rendered JSX to open it's source code in VSCode",
"main": "babel.js",
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-open-source/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (typeof document !== 'undefined') {
});

document.addEventListener('mouseover', (event) => {
if (modifier) event.target.setAttribute('data-hovering', 'true');
if (modifier && event.target.dataset.source) event.target.setAttribute('data-hovering', 'true');
});
document.addEventListener('mouseout', (event) => {
if (modifier) event.target.removeAttribute('data-hovering');
Expand Down

0 comments on commit 68b3952

Please sign in to comment.