te6.in
Blog
글 코드 조각 태그
글
코드 조각
태그
  • iOS 7
  • 소셜 미디어 6
  • 생산성 5
  • 팁 5
  • 회고 5
  • Google 4
  • iPhone 4
  • macOS 4
  • 단축어 4
  • 자동화 4
  • 해커톤 4
  • 후기 4
  • AI 3
  • LLM 3
  • TypeScript 3
  • 뉴스레터 3
  • 대전 3
  • 영문학 3
  • 일상 3
  • GitHub 2
  • iCloud 2
  • Instagram 2
  • 군대 2
  • 디자인 시스템 2
  • 유틸리티 2
  • 카카오톡 2
  • 캘린더 2
  • AirDrop 1
  • Apps Script 1
  • Astro 1
  • BeReal 1
  • ChatGPT 1
  • Gemini 1
  • Git 1
  • Homebrew 1
  • iMessage 1
  • JavaScript 1
  • JSON 1
  • KAIST 1
  • KUCC 1
  • NameDrop 1
  • Next.js 1
  • Node.js 1
  • Perplexity 1
  • Raycast 1
  • SaaS 1
  • SPARCS 1
  • Spotlight 1
  • Swift 1
  • UX 1
  • UX 라이팅 1
  • 공군해커톤 1
  • 당근 1
  • 돈 1
  • 디지털 권리 1
  • 멀티모달 1
  • 번역 1
  • 법률 1
  • 브랜딩 1
  • 블로그 1
  • 스프레드시트 1
  • 신한 1
  • 절약 1
  • 텔레그램 1
  • 핀테크 1
  • 현지화 1

타입스크립트가 tsconfig 읽고 컴파일하는 대상 파일 알아내기

  • TypeScript 3
  • Node.js 1
  • 찬휘 te6
작성 2024. 10. 19.

Node.js 환경을 가정한 코드입니다. Deno나 Bun에서도 비슷하게 작동하겠지요 (안 해봄)

import ts from "typescript";
import path from "node:path";

export function getAllTypeScriptCompiledFilePaths({
  dirToFindTsconfig,
  excludeDTs,
}: {
  dirToFindTsconfig: string;
  excludeDTs?: boolean;
}) {
  const tsconfigPath = ts.findConfigFile(dirToFindTsconfig, ts.sys.fileExists);
  if (!tsconfigPath) throw new Error(`tsconfig not found`);

  const tsconfigFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);

  // TypeScript가 tsconfig의 include, files, exclude 등의 옵션을 바탕으로 계산한
  // 컴파일 대상 파일의 목록을 가져옵니다
  // extend로 다른 tsconfig를 참조하는 경우 해당 tsconfig의 옵션까지 반영합니다
  const { fileNames } = ts.parseJsonConfigFileContent(
    tsconfigFile.config,
    ts.sys,
    path.dirname(tsconfigPath),
  );

  return fileNames.filter((fileName) => !excludeDTs || !fileName.endsWith(".d.ts"));
}
try {
  const fileNames = getAllTypeScriptCompiledFilePaths({
    dirToFindTsconfig: process.cwd(),
    excludeDTs: true,
  });

  console.log(fileNames); // [ '/Users/te6/Projects/test/src/index.ts' ]
} catch (error) {
  console.error(error);
}

typescript 의존성을 피하려고 한다면 get-tsconfig 같은 라이브러리도 유용해 보입니다.

Next.js searchParams에 적절한 타입 주기
이전 글
마지막 글이에요.
모든 코드 조각 보기
블로그 마지막 업데이트 2025년 11월 10일 01:48

microsoft/fluentui-emoji is licensed under the MIT License.
Copyright (c) Microsoft Corporation.