@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 79 lines 2.51 KB
Raw
1 import { isAbsolute, sep } from "node:path";
2 // Alchemy modifications are licensed under Apache-2.0.
3 // This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
4 import ignore from "@alchemy.run/node-utils/ignore";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import mime from "mime";
9 import {
10 CF_ASSETS_IGNORE_FILENAME,
11 HEADERS_FILENAME,
12 REDIRECTS_FILENAME,
13 } from "../shared/constants.ts";
14
15 /** normalises sep for windows and prefix with `/` */
16 export const normalizeFilePath = (relativeFilepath: string) => {
17 if (isAbsolute(relativeFilepath)) {
18 throw new Error(`Expected relative path`);
19 }
20 return "/" + relativeFilepath.split(sep).join("/");
21 };
22
23 export const getContentType = (absFilePath: string) => {
24 let contentType = mime.getType(absFilePath);
25 if (contentType && contentType.startsWith("text/") && !contentType.includes("charset")) {
26 contentType = `${contentType}; charset=utf-8`;
27 }
28 return contentType;
29 };
30
31 /**
32 * Generate a function that can match relative filepaths against a list of gitignore formatted patterns.
33 */
34 export function createPatternMatcher(
35 patterns: Array<string>,
36 exclude: boolean,
37 ): (filePath: string) => boolean {
38 if (patterns.length === 0) {
39 return (_filePath) => !exclude;
40 } else {
41 const ignorer = ignore().add(patterns);
42 return (filePath) => ignorer.test(filePath).ignored;
43 }
44 }
45
46 /**
47 * Create a function for filtering out ignored assets.
48 *
49 * The generated function takes an asset path, relative to the asset directory,
50 * and returns true if the asset should not be ignored.
51 */
52 export const createAssetsIgnoreFunction = Effect.fn(function* (dir: string) {
53 const fs = yield* FileSystem.FileSystem;
54 const path = yield* Path.Path;
55 const cfAssetIgnorePath = path.join(dir, CF_ASSETS_IGNORE_FILENAME);
56
57 const ignorePatterns = [
58 // Ignore the `.assetsignore` file and other metafiles by default.
59 // The ignore lib expects unix-style paths for its patterns
60 `/${CF_ASSETS_IGNORE_FILENAME}`,
61 `/${REDIRECTS_FILENAME}`,
62 `/${HEADERS_FILENAME}`,
63 ];
64
65 const assetsIgnore = yield* fs.readFileString(cfAssetIgnorePath).pipe(
66 Effect.catchIf(
67 (error) => error.reason._tag === "NotFound",
68 () => Effect.undefined,
69 ),
70 );
71 if (assetsIgnore !== undefined) {
72 ignorePatterns.push(...assetsIgnore.split("\n"));
73 }
74
75 return {
76 assetsIgnoreFunction: createPatternMatcher(ignorePatterns, true),
77 assetsIgnoreFilePresent: assetsIgnore !== undefined,
78 };
79 });