|
1 |
| -import { DEFAULT_VAL_TYPE, type ProjectItemType } from "~/consts.ts"; |
| 1 | +import { |
| 2 | + DEFAULT_VAL_TYPE, |
| 3 | + type ProjectItemType, |
| 4 | + RECENT_VERSION_COUNT, |
| 5 | +} from "~/consts.ts"; |
2 | 6 | import { filePathToFile } from "~/sdk.ts";
|
3 | 7 | import { compile as compileGitignore } from "gitignore-parser";
|
4 | 8 |
|
@@ -27,67 +31,41 @@ async function getProjectItemType(
|
27 | 31 | version: number,
|
28 | 32 | filePath: string,
|
29 | 33 | ): Promise<ProjectItemType> {
|
30 |
| - try { |
31 |
| - // Try up to 5 previous versions to determine the type |
32 |
| - for (let i = 0; i < 5; i++) { |
33 |
| - try { |
34 |
| - // Try to get the file at the current version min, ensuring version |
35 |
| - // doesn't go below 1 |
36 |
| - const versionToCheck = Math.max(1, version - i); |
37 |
| - const result = await filePathToFile( |
38 |
| - projectId, |
39 |
| - branchId, |
40 |
| - versionToCheck, |
41 |
| - filePath, |
42 |
| - ) |
43 |
| - .then((resp) => resp.type); |
44 |
| - return result; |
45 |
| - } catch (e) { |
46 |
| - // If not found and we haven't tried all versions yet, continue to the |
47 |
| - // next version |
48 |
| - if (e instanceof Deno.errors.NotFound && i < 4) { |
49 |
| - continue; |
50 |
| - } else if (!(e instanceof Deno.errors.NotFound) || i >= 4) { |
51 |
| - // If it's not a NotFound error or we've tried all versions, handle |
52 |
| - // it in the outer catch |
53 |
| - throw e; |
54 |
| - } |
55 |
| - } |
| 34 | + for (let i = version; i > version - RECENT_VERSION_COUNT; i--) { |
| 35 | + try { |
| 36 | + return await filePathToFile(projectId, branchId, version, filePath) |
| 37 | + .then((resp) => resp.type); |
| 38 | + } catch (e) { |
| 39 | + if (e instanceof Deno.errors.NotFound) { |
| 40 | + continue; |
| 41 | + } else throw e; |
56 | 42 | }
|
| 43 | + } |
57 | 44 |
|
58 |
| - // If we get here, we couldn't find the file in any of the 5 versions |
59 |
| - throw new Deno.errors.NotFound(); |
60 |
| - } catch (e) { |
61 |
| - // Otherwise, if it ends in .ts, .js, .tsx, or .jsx, it is a val |
62 |
| - if (e instanceof Deno.errors.NotFound) { |
63 |
| - if (/\.(ts|tsx|js|jsx)$/.test(filePath)) { |
64 |
| - const isCron = filePath.includes("cron"); |
65 |
| - const isHttp = filePath.includes("http"); |
66 |
| - const isEmail = filePath.includes("email"); |
67 |
| - |
68 |
| - // If it's ambiguous then it is a script val by default |
69 |
| - if ([isCron, isHttp, isEmail].filter(Boolean).length > 1) { |
70 |
| - return DEFAULT_VAL_TYPE; |
71 |
| - } |
| 45 | + // Otherwise, if it ends in .ts, .js, .tsx, or .jsx, it is a val |
| 46 | + if (/\.(ts|tsx|js|jsx)$/.test(filePath)) { |
| 47 | + const isCron = filePath.includes("cron"); |
| 48 | + const isHttp = filePath.includes("http"); |
| 49 | + const isEmail = filePath.includes("email"); |
72 | 50 |
|
73 |
| - // But otherwise look at the file name and try to figure out what type |
74 |
| - // of val it is based on whether the file name contains a pattern like |
75 |
| - // "cron," etc |
76 |
| - if (isCron) return "interval"; |
77 |
| - if (isHttp) return "http"; |
78 |
| - if (isEmail) return "email"; |
| 51 | + // If it's ambiguous then it is a script val by default |
| 52 | + if ([isCron, isHttp, isEmail].filter(Boolean).length > 1) { |
| 53 | + return DEFAULT_VAL_TYPE; |
| 54 | + } |
79 | 55 |
|
80 |
| - // If we can't figure it out, default to script |
81 |
| - return DEFAULT_VAL_TYPE; |
82 |
| - } |
| 56 | + // But otherwise look at the file name and try to figure out what type |
| 57 | + // of val it is based on whether the file name contains a pattern like |
| 58 | + // "cron," etc |
| 59 | + if (isCron) return "interval"; |
| 60 | + if (isHttp) return "http"; |
| 61 | + if (isEmail) return "email"; |
83 | 62 |
|
84 |
| - // Otherwise, it's just a plain old file val |
85 |
| - return "file"; |
86 |
| - } else { |
87 |
| - // Re-throw any other errors |
88 |
| - throw e; |
89 |
| - } |
| 63 | + // If we can't figure it out, default to script |
| 64 | + return DEFAULT_VAL_TYPE; |
90 | 65 | }
|
| 66 | + |
| 67 | + // Otherwise, it's just a plain old file val |
| 68 | + return "file"; |
91 | 69 | }
|
92 | 70 |
|
93 | 71 | /**
|
|
0 commit comments