Skip to content

Commit ffbe805

Browse files
committedNov 11, 2023
Use a single centralized isGitIgnored fn
1 parent df4c020 commit ffbe805

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed
 

‎src/PrincipalFactory.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import ts from 'typescript';
22
import { ProjectPrincipal } from './ProjectPrincipal.js';
33
import { toAbsolute } from './util/path.js';
44
import type { SyncCompilers, AsyncCompilers } from './types/compilers.js';
5+
import type { GlobbyFilterFunction } from 'globby';
56

67
type Paths = ts.CompilerOptions['paths'];
78

89
type Principal = { principal: ProjectPrincipal; cwds: Set<string>; pathKeys: Set<string>; pkgNames: Set<string> };
910
type Principals = Set<Principal>;
1011

11-
type Options = {
12+
export type PrincipalOptions = {
1213
cwd: string;
1314
compilerOptions: ts.CompilerOptions;
1415
paths: Paths;
1516
compilers: [SyncCompilers, AsyncCompilers];
1617
pkgName: string;
18+
isGitIgnored: GlobbyFilterFunction;
1719
};
1820

1921
const mergePaths = (cwd: string, compilerOptions: ts.CompilerOptions, paths: Paths = {}) => {
@@ -33,7 +35,7 @@ const mergePaths = (cwd: string, compilerOptions: ts.CompilerOptions, paths: Pat
3335
export class PrincipalFactory {
3436
principals: Principals = new Set();
3537

36-
public getPrincipal(options: Options) {
38+
public getPrincipal(options: PrincipalOptions) {
3739
const { cwd, compilerOptions, paths, pkgName } = options;
3840
options.compilerOptions = mergePaths(cwd, compilerOptions, paths);
3941
const principal = this.findReusablePrincipal(compilerOptions);
@@ -69,7 +71,7 @@ export class PrincipalFactory {
6971
principal.pkgNames.add(pkgName);
7072
}
7173

72-
private addNewPrincipal(options: Options) {
74+
private addNewPrincipal(options: PrincipalOptions) {
7375
const { cwd, compilerOptions, pkgName } = options;
7476
const pathKeys = new Set(Object.keys(compilerOptions?.paths ?? {}));
7577
const principal = new ProjectPrincipal(options);

‎src/ProjectPrincipal.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isGitIgnoredSync } from 'globby';
21
import ts from 'typescript';
32
import { DEFAULT_EXTENSIONS } from './constants.js';
43
import { IGNORED_FILE_EXTENSIONS } from './constants.js';
@@ -10,14 +9,10 @@ import { SourceFileManager } from './typescript/SourceFileManager.js';
109
import { isMaybePackageName, sanitizeSpecifier } from './util/modules.js';
1110
import { dirname, extname, isInNodeModules, join } from './util/path.js';
1211
import { timerify } from './util/Performance.js';
12+
import type { PrincipalOptions } from './PrincipalFactory.js';
1313
import type { SyncCompilers, AsyncCompilers } from './types/compilers.js';
1414
import type { ExportItem, ExportItemMember } from './types/exports.js';
15-
16-
type ProjectPrincipalOptions = {
17-
compilerOptions: ts.CompilerOptions;
18-
cwd: string;
19-
compilers: [SyncCompilers, AsyncCompilers];
20-
};
15+
import type { GlobbyFilterFunction } from 'globby';
2116

2217
// These compiler options override local options
2318
const baseCompilerOptions = {
@@ -54,7 +49,7 @@ export class ProjectPrincipal {
5449
// We don't want to report unused exports of config/plugin entry files
5550
skipExportsAnalysis: Set<string> = new Set();
5651

57-
isGitIgnored: ReturnType<typeof isGitIgnoredSync>;
52+
isGitIgnored: GlobbyFilterFunction;
5853
cwd: string;
5954
compilerOptions: ts.CompilerOptions;
6055
extensions: Set<string>;
@@ -69,11 +64,10 @@ export class ProjectPrincipal {
6964
program?: ts.Program;
7065
};
7166

72-
constructor({ compilerOptions, cwd, compilers }: ProjectPrincipalOptions) {
67+
constructor({ compilerOptions, cwd, compilers, isGitIgnored }: PrincipalOptions) {
7368
this.cwd = cwd;
7469

75-
// Provide `cwd`, otherwise defaults to process.cwd() w/ incompatible slashes in Windows
76-
this.isGitIgnored = isGitIgnoredSync({ cwd });
70+
this.isGitIgnored = isGitIgnored;
7771

7872
this.compilerOptions = {
7973
...compilerOptions,

‎src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isGitIgnoredSync } from 'globby';
12
import micromatch from 'micromatch';
23
import { _getDependenciesFromScripts } from './binaries/index.js';
34
import { ConfigurationChief } from './ConfigurationChief.js';
@@ -53,6 +54,10 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
5354
const factory = new PrincipalFactory();
5455
const streamer = new ConsoleStreamer({ isEnabled: isShowProgress });
5556

57+
// Central function, to prevent `Path is not in cwd` errors from `globby`
58+
// Provide `cwd`, otherwise defaults to `process.cwd()` w/ incompatible slashes in Windows
59+
const isGitIgnored = gitignore ? isGitIgnoredSync({ cwd }) : () => false;
60+
5661
streamer.cast('Reading workspace configuration(s)...');
5762

5863
await chief.init();
@@ -133,7 +138,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
133138

134139
const { compilerOptions, definitionPaths } = await loadTSConfig(join(dir, tsConfigFile ?? 'tsconfig.json'));
135140

136-
const principal = factory.getPrincipal({ cwd: dir, paths, compilerOptions, compilers, pkgName });
141+
const principal = factory.getPrincipal({ cwd: dir, paths, compilerOptions, compilers, pkgName, isGitIgnored });
137142

138143
const worker = new WorkspaceWorker({
139144
name,

0 commit comments

Comments
 (0)
Please sign in to comment.