UNPKG

@terasky/backstage-plugin-devpod

Version:

Automatically launch Devpod workspaces for your Backstage services

47 lines (44 loc) 1.67 kB
import { useEntity } from '@backstage/plugin-catalog-react'; import { useState } from 'react'; import { useDevpodConfig } from '../components/DevpodProvider/DevpodProvider.esm.js'; const useDevpod = () => { const { entity } = useEntity(); const { defaultIde } = useDevpodConfig(); const [selectedIde, setSelectedIde] = useState(defaultIde); const sourceAnnotation = entity.metadata?.annotations?.["backstage.io/source-location"] || ""; let gitUrl = sourceAnnotation.startsWith("url:") ? sourceAnnotation.substring(4) : void 0; if (gitUrl) { gitUrl = gitUrl.replace(/\/+$/, ""); const treeMatch = gitUrl.match(/^(.+)\/tree\/([^/]+)(?:\/(.+))?$/); const blobMatch = gitUrl.match(/^(.+)\/blob\/([^/]+)(?:\/(.+))?$/); if (treeMatch) { const [_, baseUrl, branch, subPath] = treeMatch; if (branch === "main" && subPath) { gitUrl = `${baseUrl}@subpath:${subPath}`; } else { gitUrl = `${baseUrl}@${branch}`; } } if (blobMatch) { const [_, baseUrl, branch, subPath] = blobMatch; if (branch === "main" && subPath) { gitUrl = `${baseUrl}@subpath:${subPath}`; } else { gitUrl = `${baseUrl}@${branch}`; } } } const componentName = entity.metadata.name; const uuid = Math.random().toString(36).substring(2, 6); const devpodUrl = gitUrl ? `https://devpod.sh/open#${encodeURIComponent(gitUrl)}&workspace=${componentName}-${uuid}&ide=${selectedIde}` : ""; return { gitUrl, hasGitUrl: Boolean(gitUrl), componentName, selectedIde, setSelectedIde, devpodUrl }; }; export { useDevpod }; //# sourceMappingURL=useDevpod.esm.js.map