Skip to content

Commit 482179a

Browse files
CompuIvesSaraVieira
authored andcommitted
Add support for GitHub URLs as versions (codesandbox#2417)
* Add support for GitHub URLs as versions * Add one more nullcheck
1 parent dd6eea0 commit 482179a

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

packages/app/src/sandbox/eval/npm/fetch-npm-module.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ function normalizeJSDelivr(files: any, fileObject: Meta = {}, rootPath) {
5656
return fileObject;
5757
}
5858

59+
/**
60+
* Converts urls like "https://github.com/user/repo.git" to "user/repo".
61+
*/
62+
const convertGitHubURLToVersion = (version: string) => {
63+
const result = version.match(/https:\/\/github\.com\/(.*)$/);
64+
if (result && result[1]) {
65+
const repo = result[1];
66+
return repo.replace(/\.git$/, '');
67+
}
68+
69+
return version;
70+
};
71+
5972
const urlProtocols = {
6073
csbGH: {
6174
file: async (name: string, version: string, path: string) =>
@@ -80,16 +93,22 @@ const urlProtocols = {
8093
},
8194
jsDelivrGH: {
8295
file: async (name: string, version: string, path: string) =>
83-
`https://cdn.jsdelivr.net/gh/${version}${path}`,
96+
`https://cdn.jsdelivr.net/gh/${convertGitHubURLToVersion(
97+
version
98+
)}${path}`,
8499
meta: async (name: string, version: string) => {
85100
// First get latest sha from GitHub API
86101
const sha = await fetch(
87-
`https://api.github.com/repos/${version}/commits/master`
102+
`https://api.github.com/repos/${convertGitHubURLToVersion(
103+
version
104+
)}/commits/master`
88105
)
89106
.then(x => x.json())
90107
.then(x => x.sha);
91108

92-
return `https://data.jsdelivr.com/v1/package/gh/${version}@${sha}/flat`;
109+
return `https://data.jsdelivr.com/v1/package/gh/${convertGitHubURLToVersion(
110+
version
111+
)}@${sha}/flat`;
93112
},
94113
normalizeMeta: normalizeJSDelivr,
95114
},

0 commit comments

Comments
 (0)