From e95833d52b94738c04c201c9aa1a8849c98cd5c7 Mon Sep 17 00:00:00 2001 From: bencergazda Date: Thu, 9 Sep 2021 16:31:23 +0200 Subject: [PATCH] feat: Ignore files that are listed in the tsconfig `exclude` array --- README.md | 5 +++-- src/actions.ts | 11 +++++++++-- src/helpers.ts | 39 +++++++++++++++++++++++++++++++++++++-- src/types.ts | 2 ++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9a50161..7c83adf 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,9 @@ package.json .tscprc ```json5 { - "ignored_files": ['**/an_ignored_file.ext'], // files not to copy (defaults to ['node_modules']) - "compiled_files": [] // files compiled by TS (these also get ignored) (defaults to ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx']) + "ignored_files": ['**/an_ignored_file.ext'], // files not to copy (defaults to `['node_modules']`) + "compiled_files": [], // files compiled by TS (these also get ignored) (defaults to `['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx']`) + "use_ts_exclude": true, // ignore files that are listed in the tsconfig `exclude` array (defaults to `true`) } ``` diff --git a/src/actions.ts b/src/actions.ts index 2df3637..36c5761 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -6,6 +6,7 @@ import { console_colors, copy_file_or_directory, definitely_posix, + get_ignore_list, remove_file_or_directory, sleep, } from './helpers'; @@ -27,7 +28,7 @@ async function collect_projects_files(projects: TsProject[], config: Config): Pr cwd: project.root_dir, dot: true, onlyFiles: true, - ignore: config.ignored_files, + ignore: get_ignore_list(config, project), }); return { @@ -89,7 +90,13 @@ async function watch_files(projects: TsProject[], config: Config): Promise const globed_projects = await collect_projects_files(projects, config); const files_to_watch = globed_projects.map(({ root_dir }) => `${root_dir}/.`); - const ignore_glob = `{${config.ignored_files.map((rule) => `**/${rule}`).join(',')}}`; + const ignore_glob = `{${get_ignore_list(config, projects).map((rule) => { + if (!rule.startsWith('**/')) { + return `**/${rule}`; + } + + return rule; + }).join(',')}}`; const watcher = chokidar.watch(files_to_watch, { ignored: ignore_glob, diff --git a/src/helpers.ts b/src/helpers.ts index 42943bf..2402187 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -96,7 +96,7 @@ const options: CliOptions = program.opts(); * Returns the complete, resolved configuration object */ async function get_config(): Promise { - const cwd = process.cwd(); + const cwd = definitely_posix(process.cwd()); const ts_config = get_ts_config(cwd, options.project); @@ -108,10 +108,11 @@ async function get_config(): Promise { const default_config: Config = { cwd, cli_options: options, + ts_config, + use_ts_exclude: true, compiled_files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'], ignored_files: ['node_modules'], rules: [], - ts_config, }; const config = { @@ -150,6 +151,7 @@ function build_project_path(cwd: string, project_path: string, ts_config: Parsed const referenceName = path.relative(process.cwd(), cwd); const projectName = path.join(currentDirName, referenceName); + const { exclude } = ts_config.raw; const { rootDir, outDir } = ts_config.options; if (!rootDir) { @@ -166,6 +168,7 @@ function build_project_path(cwd: string, project_path: string, ts_config: Parsed ts_config_path: project_path, root_dir: rootDir, out_dir: outDir, + exclude, }; } @@ -207,6 +210,37 @@ function get_ts_projects_paths(options: Config): TsProject[] { }); } +/** + * Combines the exclude pattern in the tsconfig file and the TSCP `config.ignored_files` + * @param config + * @param projects + */ +function get_ignore_list(config: Config, projects: TsProject | TsProject[]): string[] { + const ignore_list: string[] = []; + + if (config.use_ts_exclude) { + const safe_projects = !Array.isArray(projects) ? [projects] : projects; + + const ts_exclude_list = safe_projects.map((project) => { + return project.exclude.map((rule) => { + // Handle if the exclude pattern contains the name of the root directory + const rootDirName = project.root_dir.replace(project.base_path + '/', '') + '/'; + if (rule.startsWith(rootDirName)) { + return rule.replace(rootDirName, ''); + } + + return rule; + }); + }).flat(); + + ignore_list.push(...ts_exclude_list) + } + + ignore_list.push(...config.ignored_files); + + return ignore_list; +} + /** * Displays a colored log in the stdout * @param msg @@ -414,6 +448,7 @@ export { get_ts_config, get_ts_project_paths, get_ts_projects_paths, + get_ignore_list, validate_path, get_file_stats, remove_file_or_directory, diff --git a/src/types.ts b/src/types.ts index 0e9105a..a4b2715 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,6 +10,7 @@ export type Config = { cwd: string; cli_options: CliOptions, ts_config: ParsedCommandLine, + use_ts_exclude: boolean; compiled_files: string[]; ignored_files: string[]; rules: Rule[]; @@ -60,6 +61,7 @@ export type TsProject = { ts_config_path: string; root_dir: string; out_dir: string; + exclude: string[]; } export type TsProjectWithFiles = TsProject & {