export async function getTsConfigAliasPrefix(cwd: string) { const tsConfig = await loadConfig(cwd) if (tsConfig?.resultType === "failed" || !tsConfig?.paths) { return null } // This assume that the first alias is the prefix. for (const [alias, paths] of Object.entries(tsConfig.paths)) { if (paths.includes("./*") || paths.includes("./src/*")) { return alias.at(0) } } return null}
This function loads the tsconfig.json or jsconfig.json. It will start searching from the specified cwd directory. Passing the tsconfig.json or jsconfig.json file directly instead of a directory also works.
// This assume that the first alias is the prefix.for (const [alias, paths] of Object.entries(tsConfig.paths)) { if (paths.includes("./*") || paths.includes("./src/*")) { return alias.at(0) }}
From the docs, the at() method is equivalent to the bracket notation when index is non-negative. For example, array[0] and array.at(0) both return the first item. However, when counting elements from the end of the array, you cannot use array[-1] like you may in Python or R, because all values inside the square brackets are treated literally as string properties, so you will end up reading array["-1"], which is just a normal string property instead of an array index.
The usual practice is to access length and calculate the index from that — for example, array[array.length - 1]. The at() method allows relative indexing, so this can be shortened to array.at(-1).
Judging by this function name “getTsConfigAliasAsPrefix” in shadcn-ui CLI related source code, it is obvious that this function returns the alias prefix from your project based on what you provided in your project’s tsconfig.json.
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.