Skip to content

Commit

Permalink
feat: Ignore files that are listed in the tsconfig exclude array
Browse files Browse the repository at this point in the history
  • Loading branch information
bencergazda committed Sep 9, 2021
1 parent 1b59226 commit e95833d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}
```

Expand Down
11 changes: 9 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
console_colors,
copy_file_or_directory,
definitely_posix,
get_ignore_list,
remove_file_or_directory,
sleep,
} from './helpers';
Expand All @@ -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 {
Expand Down Expand Up @@ -89,7 +90,13 @@ async function watch_files(projects: TsProject[], config: Config): Promise<void>
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,
Expand Down
39 changes: 37 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const options: CliOptions = program.opts();
* Returns the complete, resolved configuration object
*/
async function get_config(): Promise<Config> {
const cwd = process.cwd();
const cwd = definitely_posix(process.cwd());

const ts_config = get_ts_config(cwd, options.project);

Expand All @@ -108,10 +108,11 @@ async function get_config(): Promise<Config> {
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 = {
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
};
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -60,6 +61,7 @@ export type TsProject = {
ts_config_path: string;
root_dir: string;
out_dir: string;
exclude: string[];
}

export type TsProjectWithFiles = TsProject & {
Expand Down

0 comments on commit e95833d

Please sign in to comment.