| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 1 | import { ConfigVar, ConfigVarSemanticType } from "./analyze"; |
| 2 | |
| 3 | export function augmentConfigVar(cv: ConfigVar) { |
| 4 | if (cv.semanticType != null) { |
| 5 | return; |
| 6 | } |
| 7 | const name = cv.name.toLowerCase(); |
| 8 | const value = cv.defaultValue ? String(cv.defaultValue).toLowerCase() : ""; |
| 9 | if (name.includes("postgres") || name.includes("pg")) { |
| 10 | if (name.includes("url")) cv.semanticType = ConfigVarSemanticType.POSTGRES_URL; |
| 11 | if (name.includes("password") || name.includes("pass")) |
| 12 | cv.semanticType = ConfigVarSemanticType.POSTGRES_PASSWORD; |
| 13 | if (name.includes("user")) cv.semanticType = ConfigVarSemanticType.POSTGRES_USER; |
| 14 | if ( |
| 15 | name.includes("db_name") || |
| 16 | name.includes("database_name") || |
| 17 | (name.includes("db") && !name.includes("url")) |
| 18 | ) |
| 19 | cv.semanticType = ConfigVarSemanticType.POSTGRES_DB; |
| 20 | if (name.includes("port")) cv.semanticType = ConfigVarSemanticType.POSTGRES_PORT; |
| 21 | if (name.includes("host")) cv.semanticType = ConfigVarSemanticType.POSTGRES_HOST; |
| 22 | if (name.includes("sslmode") || name.includes("ssl")) cv.semanticType = ConfigVarSemanticType.POSTGRES_SSL; |
| 23 | } |
| 24 | if (name.includes("mongo")) { |
| 25 | if (name.includes("url") || name.includes("uri")) cv.semanticType = ConfigVarSemanticType.MONGO_URL; |
| 26 | if (name.includes("password") || name.includes("pass")) cv.semanticType = ConfigVarSemanticType.MONGO_PASSWORD; |
| 27 | if (name.includes("user")) cv.semanticType = ConfigVarSemanticType.MONGO_USER; |
| 28 | if ( |
| 29 | name.includes("db_name") || |
| 30 | name.includes("database_name") || |
| 31 | (name.includes("db") && !name.includes("url")) |
| 32 | ) |
| 33 | cv.semanticType = ConfigVarSemanticType.MONGO_DB; |
| 34 | if (name.includes("port")) cv.semanticType = ConfigVarSemanticType.MONGO_PORT; |
| 35 | if (name.includes("host")) cv.semanticType = ConfigVarSemanticType.MONGO_HOST; |
| 36 | if (name.includes("ssl")) cv.semanticType = ConfigVarSemanticType.MONGO_SSL; |
| 37 | } |
| 38 | if (name.includes("sqlite_path") || value.endsWith(".sqlite") || value.endsWith(".db3") || value.endsWith(".db")) { |
| 39 | cv.semanticType = ConfigVarSemanticType.SQLITE_PATH; |
| 40 | } |
| 41 | if (name.includes("sqlite") && (name.includes("url") || name.includes("uri") || name.includes("path"))) { |
| 42 | cv.semanticType = ConfigVarSemanticType.SQLITE_PATH; |
| 43 | } |
| 44 | if (name.includes("database_url") || name.includes("db_url")) { |
| 45 | cv.semanticType = ConfigVarSemanticType.DATABASE_URL; |
| 46 | } |
| 47 | if (name.includes("path") || name.includes("file_path") || name.includes("dir") || name.includes("directory")) { |
| 48 | if ( |
| 49 | !name.includes("url") && |
| 50 | (value.startsWith("./") || value.startsWith("../") || value.startsWith("/") || /^[a-z]:[\\/]/.test(value)) |
| 51 | ) { |
| 52 | cv.semanticType = ConfigVarSemanticType.FILESYSTEM_PATH; |
| 53 | } |
| 54 | } |
| 55 | if ( |
| 56 | !name.includes("url") && |
| 57 | (value.startsWith("./") || value.startsWith("../") || value.startsWith("/") || /^[a-z]:[\\/]/.test(value)) |
| 58 | ) { |
| 59 | cv.semanticType = ConfigVarSemanticType.FILESYSTEM_PATH; |
| 60 | } |
| 61 | if (name.includes("port")) { |
| 62 | cv.semanticType = ConfigVarSemanticType.PORT; |
| 63 | } |
| 64 | } |