babel-plugin-open-source
Advanced tools
| const fs = require('fs'); | ||
| const ini = require('ini'); | ||
| const getGitHubUrl = (localFilePath, lineNumber) => { | ||
| if (process.env.VERCEL) { | ||
| // TODO: I bet I'll regret hardcoding this | ||
| const gitRoot = '/vercel/path0/'; | ||
| const repo = process.env.VERCEL_GIT_REPO_OWNER + '/' + process.env.VERCEL_GIT_REPO_SLUG; | ||
| const filePath = localFilePath.replace(gitRoot, ''); | ||
| // TODO: replace with ci-env | ||
| const branchName = process.env.VERCEL_GIT_COMMIT_REF || 'main'; | ||
| return `https://github.com/${repo}/blob/${branchName}/${filePath}#L${lineNumber}`; | ||
| } else { | ||
| try { | ||
| const findGitRoot = require('find-git-root'); | ||
| const gitRoot = findGitRoot(localFilePath).replace('.git', ''); | ||
| const repo = getRepository(gitRoot); | ||
| const filePath = localFilePath.replace(gitRoot, ''); | ||
| // TODO: replace with ci-env | ||
| const branchName = process.env.GITHUB_HEAD_REF || 'main'; | ||
| return `https://github.com/${repo}/blob/${branchName}/${filePath}#L${lineNumber}`; | ||
| } catch (error) { | ||
| console.log('Could not find .git root, skipping plugin'); | ||
| } | ||
| } | ||
| }; | ||
| const getRepository = (gitRoot) => { | ||
| const result = fs.readFileSync(gitRoot + '/.git/config', 'utf8'); | ||
| const config = ini.parse(result); | ||
| return config['remote "origin"'].url.replace('git@github.com:', '').replace('.git', ''); | ||
| }; | ||
| module.exports = getGitHubUrl; |
+16
-46
@@ -1,10 +0,11 @@ | ||
| 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'); | ||
| const getGitHubUrl = require('./github-url.js'); | ||
| const scriptLocation = 'babel-plugin-open-source/script.js'; | ||
| // picks root directory's .env file | ||
| const dotenvConfig = dotenv.config(); | ||
| module.exports = declare((api) => { | ||
@@ -17,2 +18,11 @@ api.assertVersion(7); | ||
| state.file.set('hasJSX', false); | ||
| let editor = state.opts && state.opts.editor ? state.opts.editor.toLowerCase() : 'vscode'; | ||
| const editorInENV = dotenvConfig && dotenvConfig.parsed && dotenvConfig.parsed.BABEL_OPEN_SOURCE_EDITOR; | ||
| if (editorInENV) editor = editorInENV; | ||
| state.file.set('editor', editor); | ||
| if (process.env.NODE_ENV === 'development' || editor === 'github'); | ||
| else state.file.set('skip', true); | ||
| }, | ||
@@ -32,8 +42,7 @@ exit: (path, state) => { | ||
| JSXOpeningElement(path, state) { | ||
| if (state.file.get('skip')) return; | ||
| const location = path.container.openingElement.loc; | ||
| let url = null; | ||
| let editor = state.opts && state.opts.editor ? state.opts.editor.toLowerCase() : 'vscode'; | ||
| if (editor !== 'github' && process.env.NODE_ENV !== 'development') return; | ||
| // the element was generated and doesn't have location information | ||
@@ -57,8 +66,3 @@ if (!location) return; | ||
| // picks root directory's .env file | ||
| const dotenvConfig = dotenv.config(); | ||
| const editorInENV = dotenvConfig && dotenvConfig.parsed && dotenvConfig.parsed.BABEL_OPEN_SOURCE_EDITOR; | ||
| if (editorInENV) { | ||
| editor = editorInENV; | ||
| } | ||
| const editor = state.file.get('editor'); | ||
@@ -95,35 +99,1 @@ if (editor === 'sublime') { | ||
| }); | ||
| 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('git@github.com:', '').replace('.git', ''); | ||
| }; |
+1
-1
| { | ||
| "name": "babel-plugin-open-source", | ||
| "version": "1.2.4", | ||
| "version": "1.3.0", | ||
| "description": "Alt + Click on rendered JSX to open it's source code in VSCode", | ||
@@ -5,0 +5,0 @@ "main": "babel.js", |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 6 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
7582
7.42%5
25%150
4.17%9
80%