UNPKG

git-command-helper

Version:
48 lines (44 loc) 1.93 kB
// git-command-helper 2.1.0 by Dimas Lanjaka <dimaslanjaka@gmail.com> (https://www.webmanajemen.com) 'use strict'; var sbgUtility = require('sbg-utility'); var safeUrl = require('../utils/safe-url.js'); var getGithubCurrentBranch = require('./getGithubCurrentBranch.js'); var getGithubRemote = require('./getGithubRemote.js'); var getGithubRootDir = require('./getGithubRootDir.js'); /** * Get GitHub URL for a single file or folder in a repository. * * @param repositoryPath - Absolute or relative path to the file or folder. * @param opt - Options for git operations. If not provided or missing `cwd`, defaults to `repositoryPath`. * @param remoteUrl - Optional remote URL to override the git config remote. Useful for testing or custom remotes. * @returns An object containing `remoteURL` (tree view) and `rawURL` (raw file view) for the file/folder on GitHub. */ async function getGithubRepoUrl(repositoryPath, opt, remoteUrl) { repositoryPath = sbgUtility.trueCasePathSync(repositoryPath); // Default cwd to repositoryPath if not set if (!opt || !opt.cwd) { opt = { ...(opt || {}), cwd: repositoryPath }; } const root = sbgUtility.trueCasePathSync((await getGithubRootDir.getGithubRootDir(opt)) || ""); const remote = (remoteUrl || (await getGithubRemote.getGithubRemote(null, opt)) || "").replace(/(.git|\/)$/i, ""); let url = new URL(remote); url.pathname += "/tree/" + (await getGithubCurrentBranch.getGithubCurrentBranch(opt)) + repositoryPath.replace(root, ""); /** * url from repository url */ const remoteURL = safeUrl.safeURL(url.toString()); url = new URL(remote); url.pathname += "/raw/" + (await getGithubCurrentBranch.getGithubCurrentBranch(opt)) + repositoryPath.replace(root, ""); /** * url raw file */ const rawURL = safeUrl.safeURL(url.toString()); return { remoteURL, rawURL }; } exports.getGithubRepoUrl = getGithubRepoUrl;