Skip to content

Commit 138fd15

Browse files
authored
fix(create-turbo): support renamed repositories (#8993)
### Description This bug broke create-turbo when we renamed the repo. This bug is explained in the comments but the tl;dr is that downloading the repo works because the download URL automatically redirects from the old name to the new name. However the download is always put into a directory of the name {repoName}-{branch} where the repoName is the name of the repo at the time and NOT always the name used to download it.
1 parent f260480 commit 138fd15

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

packages/turbo-utils/src/examples.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Stream } from "node:stream";
22
import { promisify } from "node:util";
3-
import { join } from "node:path";
3+
import { join, sep } from "node:path";
44
import { tmpdir } from "node:os";
55
import { createWriteStream, promises as fs } from "node:fs";
66
import { x as extract } from "tar";
@@ -113,16 +113,21 @@ export async function downloadAndExtractRepo(
113113
`turbo-ct-example`
114114
);
115115

116+
let rootPath: string | null = null;
116117
await extract({
117118
file: tempFile,
118119
cwd: root,
119120
strip: filePath ? filePath.split("/").length + 1 : 1,
120-
filter: (p: string) =>
121-
p.startsWith(
122-
`${name}-${branch.replace(/\//g, "-")}${
123-
filePath ? `/${filePath}/` : "/"
124-
}`
125-
),
121+
filter: (p: string) => {
122+
// Determine the unpacked root path dynamically instead of hardcoding to the fetched repo's name. This avoids the condition when the repository has been renamed, and the
123+
// old repository name is used to fetch the example. The tar download will work as it is redirected automatically, but the root directory of the extracted
124+
// example will be the new, renamed name instead of the name used to fetch the example.
125+
if (rootPath === null) {
126+
const pathSegments = p.split(sep);
127+
rootPath = pathSegments.length ? pathSegments[0] : null;
128+
}
129+
return p.startsWith(`${rootPath}${filePath ? `/${filePath}/` : "/"}`);
130+
},
126131
});
127132

128133
await fs.unlink(tempFile);
@@ -134,11 +139,22 @@ export async function downloadAndExtractExample(root: string, name: string) {
134139
`turbo-ct-example`
135140
);
136141

142+
let rootPath: string | null = null;
137143
await extract({
138144
file: tempFile,
139145
cwd: root,
140146
strip: 2 + name.split("/").length,
141-
filter: (p: string) => p.includes(`turbo-main/examples/${name}/`),
147+
filter: (p: string) => {
148+
// Determine the unpacked root path dynamically instead of hardcoding. This avoids the condition when the repository has been renamed, and the
149+
// old repository name is used to fetch the example. The tar download will work as it is redirected automatically, but the root directory of the extracted
150+
// example will be the new, renamed name instead of the name used to fetch the example.
151+
if (rootPath === null) {
152+
const pathSegments = p.split(sep);
153+
rootPath = pathSegments.length ? pathSegments[0] : null;
154+
}
155+
156+
return p.includes(`${rootPath}/examples/${name}/`);
157+
},
142158
});
143159

144160
await fs.unlink(tempFile);

0 commit comments

Comments
 (0)