Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/react-doctor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ Usage: react-doctor [directory] [options]

Options:
-v, --version display the version number
--lint run linting
--no-lint skip linting
--dead-code run dead code detection
--no-dead-code skip dead code detection
--verbose show file details per rule
--score output only the score
-y, --yes skip prompts, scan all workspace projects
--project <name> select workspace project (comma-separated for multiple)
--diff [base] scan only files changed vs base branch
--offline skip telemetry (anonymous, not stored, only used to calculate score)
--no-ami skip Ami-related prompts
--fix open Ami to auto-fix all issues
-h, --help display help for command
Expand Down
19 changes: 16 additions & 3 deletions packages/react-doctor/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ const program = new Command()
.version(VERSION, "-v, --version", "display the version number")
.argument("[directory]", "project directory to scan", ".")
.option("--no-lint", "skip linting")
.option("--lint", "run linting")
.option("--no-dead-code", "skip dead code detection")
.option("--dead-code", "run dead code detection")
.option("--verbose", "show file details per rule")
.option("--score", "output only the score")
.option("-y, --yes", "skip prompts, scan all workspace projects")
Expand Down Expand Up @@ -179,7 +181,12 @@ const program = new Command()
logger.dim(`Scanning ${projectDirectory}...`);
logger.break();
}
const scanResult = await scan(projectDirectory, { ...scanOptions, includePaths });
const projectUserConfig = projectDirectory === resolvedDirectory ? userConfig : undefined;
const scanResult = await scan(projectDirectory, {
...scanOptions,
includePaths,
userConfig: projectUserConfig,
});
allDiagnostics.push(...scanResult.diagnostics);
if (!isScoreOnly) {
logger.break();
Expand Down Expand Up @@ -286,14 +293,20 @@ const buildWebDeeplink = (directory: string): string =>
`${OPEN_BASE_URL}?${buildDeeplinkParams(directory).toString()}`;

const openAmiToFix = (directory: string): void => {
const isInstalled = isAmiInstalled();
let isInstalled = isAmiInstalled();
const deeplink = buildDeeplink(directory);
const webDeeplink = buildWebDeeplink(directory);

if (!isInstalled) {
if (process.platform === "darwin") {
installAmi();
logger.success("Ami installed successfully.");
isInstalled = isAmiInstalled();
if (isInstalled) {
logger.success("Ami installed successfully.");
} else {
logger.error("Ami installation completed, but Ami was not detected.");
logger.dim(`Download at ${highlighter.info(AMI_RELEASES_URL)}`);
}
} else {
logger.error("Ami is not installed.");
logger.dim(`Download at ${highlighter.info(AMI_RELEASES_URL)}`);
Expand Down
3 changes: 2 additions & 1 deletion packages/react-doctor/src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ export const scan = async (
): Promise<ScanResult> => {
const startTime = performance.now();
const projectInfo = discoverProject(directory);
const userConfig = loadConfig(directory);
const userConfig =
inputOptions.userConfig === undefined ? loadConfig(directory) : inputOptions.userConfig;

const options = {
lint: inputOptions.lint ?? userConfig?.lint ?? true,
Expand Down
1 change: 1 addition & 0 deletions packages/react-doctor/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export interface ScanOptions {
scoreOnly?: boolean;
offline?: boolean;
includePaths?: string[];
userConfig?: ReactDoctorConfig | null;
}

export interface DiffInfo {
Expand Down
38 changes: 37 additions & 1 deletion packages/react-doctor/src/utils/discover-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ const FRAMEWORK_DISPLAY_NAMES: Record<Framework, string> = {
export const formatFrameworkName = (framework: Framework): string =>
FRAMEWORK_DISPLAY_NAMES[framework];

const SOURCE_FILE_FALLBACK_IGNORED_DIRECTORIES = new Set([
".git",
".next",
".turbo",
"build",
"dist",
"node_modules",
]);

const countSourceFilesByDirectoryWalk = (rootDirectory: string): number => {
const stack = [rootDirectory];
let fileCount = 0;

while (stack.length > 0) {
const currentDirectory = stack.pop();
if (!currentDirectory) continue;

const entries = fs.readdirSync(currentDirectory, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
if (SOURCE_FILE_FALLBACK_IGNORED_DIRECTORIES.has(entry.name)) {
continue;
}
stack.push(path.join(currentDirectory, entry.name));
continue;
}

if (entry.isFile() && SOURCE_FILE_PATTERN.test(entry.name)) {
fileCount += 1;
}
}
}

return fileCount;
};

const countSourceFiles = (rootDirectory: string): number => {
const result = spawnSync("git", ["ls-files", "--cached", "--others", "--exclude-standard"], {
cwd: rootDirectory,
Expand All @@ -72,7 +108,7 @@ const countSourceFiles = (rootDirectory: string): number => {
});

if (result.error || result.status !== 0) {
return 0;
return countSourceFilesByDirectoryWalk(rootDirectory);
}

return result.stdout
Expand Down
5 changes: 2 additions & 3 deletions packages/react-doctor/src/utils/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ export const loadConfig = (rootDirectory: string): ReactDoctorConfig | null => {
const parsed: unknown = JSON.parse(fileContent);
if (!isPlainObject(parsed)) {
console.warn(`Warning: ${CONFIG_FILENAME} must be a JSON object, ignoring.`);
return null;
} else {
return parsed as ReactDoctorConfig;
}
return parsed as ReactDoctorConfig;
} catch (error) {
console.warn(
`Warning: Failed to parse ${CONFIG_FILENAME}: ${error instanceof Error ? error.message : String(error)}`,
);
return null;
}
}

Expand Down