Parse compilerOptions.paths and compilerOptions.baseUrl into an alias map. * dir_prefix is the directory of the config file relative to the repo root * (e.g. "apps/manager", or "" for repo root). Returns NULL if the file is * missing, malformed, or has neither a usable paths block nor a baseUrl. */
| 172 | * (e.g. "apps/manager", or "" for repo root). Returns NULL if the file is |
| 173 | * missing, malformed, or has neither a usable paths block nor a baseUrl. */ |
| 174 | static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char *dir_prefix) { |
| 175 | FILE *f = cbm_fopen(abs_path, "r"); |
| 176 | if (!f) { |
| 177 | return NULL; |
| 178 | } |
| 179 | fseek(f, 0, SEEK_END); |
| 180 | long len = ftell(f); |
| 181 | fseek(f, 0, SEEK_SET); |
| 182 | if (len <= 0 || len > CBM_PATH_ALIAS_MAX_FILE_BYTES) { |
| 183 | fclose(f); |
| 184 | return NULL; |
| 185 | } |
| 186 | char *buf = malloc((size_t)len + 1); |
| 187 | if (!buf) { |
| 188 | fclose(f); |
| 189 | return NULL; |
| 190 | } |
| 191 | size_t nread = fread(buf, 1, (size_t)len, f); |
| 192 | fclose(f); |
| 193 | buf[nread] = '\0'; |
| 194 | |
| 195 | yyjson_read_flag flg = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS; |
| 196 | yyjson_doc *doc = yyjson_read(buf, nread, flg); |
| 197 | free(buf); |
| 198 | if (!doc) { |
| 199 | return NULL; |
| 200 | } |
| 201 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 202 | yyjson_val *compiler_opts = yyjson_obj_get(root, "compilerOptions"); |
| 203 | if (!compiler_opts) { |
| 204 | yyjson_doc_free(doc); |
| 205 | return NULL; |
| 206 | } |
| 207 | yyjson_val *base_url_val = yyjson_obj_get(compiler_opts, "baseUrl"); |
| 208 | const char *base_url_str = base_url_val ? yyjson_get_str(base_url_val) : NULL; |
| 209 | yyjson_val *paths_obj = yyjson_obj_get(compiler_opts, "paths"); |
| 210 | if (!paths_obj && !base_url_str) { |
| 211 | yyjson_doc_free(doc); |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | cbm_path_alias_map_t *map = calloc(1, sizeof(*map)); |
| 216 | if (!map) { |
| 217 | yyjson_doc_free(doc); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | if (base_url_str && base_url_str[0] != '\0' && strcmp(base_url_str, ".") != 0) { |
| 222 | map->base_url = resolve_target_relative(dir_prefix, base_url_str); |
| 223 | } else if (base_url_str && strcmp(base_url_str, ".") == 0 && dir_prefix && |
| 224 | dir_prefix[0] != '\0') { |
| 225 | map->base_url = strdup(dir_prefix); |
| 226 | } |
| 227 | |
| 228 | if (paths_obj && yyjson_is_obj(paths_obj)) { |
| 229 | size_t obj_size = yyjson_obj_size(paths_obj); |
| 230 | bool capped = obj_size > CBM_PATH_ALIAS_MAX_ENTRIES; |
| 231 | int capacity = (int)(capped ? (size_t)CBM_PATH_ALIAS_MAX_ENTRIES : obj_size); |
no test coverage detected