LoginSignup
4
3

More than 3 years have passed since last update.

tsconfigで定義されたソースコード一覧の取得方法

Posted at

ほぼ自分の備忘録用のTypeScript小ネタ。

TSのAPI使ったツール作ってるときに「ユーザーのtsconfig.jsonの内容に従って、プロジェクトのファイル一覧となる.tsのファイル名一覧がほしい」となったとする。要するに、tscコマンドを叩いたときにトランスパイルされるファイル群ってことなんだけど、この結果って rootDirfiles, exclude などなどの値全部で決まるものなので、自前で頑張ると大体失敗する。

TSのCLIが使ってるのと同じAPI使えばいいわけで、下記のようにするとファイルの絶対パス一覧が取得できる。

import ts from 'typescript';

const res = ts.getParsedCommandLineOfConfigFile(
  'tsconfig.json',
  { },
  ts.sys as any,
);

console.log(res!.fileNames);

res.options とかすればparseされたCompile Optionsも取得できる。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3