Skip to content

Commit de1cbd4

Browse files
aidenybaiamiagent
andcommitted
add --fix flag to scan and open Ami to auto-fix issues
Checks if Ami is installed first, installs if not, then deep-links with the fix prompt. Works as a flag (npx react-doctor . --fix) or as subcommands (npx react-doctor fix / install-ami). Generated with [Ami](https://ami.dev) Co-Authored-By: Ami <noreply@ami.dev>
1 parent b1f1abc commit de1cbd4

File tree

1 file changed

+40
-27
lines changed
  • packages/react-doctor/src

1 file changed

+40
-27
lines changed

packages/react-doctor/src/cli.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface CliFlags {
1515
deadCode: boolean;
1616
verbose: boolean;
1717
score: boolean;
18+
fix: boolean;
1819
yes: boolean;
1920
project?: string;
2021
}
@@ -33,6 +34,7 @@ const program = new Command()
3334
.option("--score", "output only the score")
3435
.option("-y, --yes", "skip prompts, scan all workspace projects")
3536
.option("--project <name>", "select workspace project (comma-separated for multiple)")
37+
.option("--fix", "open Ami to auto-fix all issues")
3638
.action(async (directory: string, flags: CliFlags) => {
3739
try {
3840
const resolvedDirectory = path.resolve(directory);
@@ -72,6 +74,10 @@ const program = new Command()
7274
logger.break();
7375
}
7476
}
77+
78+
if (flags.fix) {
79+
openAmiToFix(resolvedDirectory);
80+
}
7581
} catch (error) {
7682
handleError(error);
7783
}
@@ -88,64 +94,71 @@ const AMI_INSTALL_URL = "https://ami.dev/install.sh";
8894
const AMI_FIX_PROMPT =
8995
"Run npx -y react-doctor@latest . --verbose, read every diagnostic, then fix all issues one by one. After fixing, re-run react-doctor to verify the score improved.";
9096

91-
const encodeDeeplinkParameter = (value: string): string =>
92-
encodeURIComponent(value).replace(/%20/g, "%20");
93-
9497
const buildAmiDeeplink = (projectDirectory: string): string => {
95-
const encodedDirectory = encodeDeeplinkParameter(projectDirectory);
96-
const encodedPrompt = encodeDeeplinkParameter(AMI_FIX_PROMPT);
98+
const encodedDirectory = encodeURIComponent(projectDirectory);
99+
const encodedPrompt = encodeURIComponent(AMI_FIX_PROMPT);
97100
return `ami://new-chat?cwd=${encodedDirectory}&prompt=${encodedPrompt}&mode=agent`;
98101
};
99102

100-
const installAndOpenAmi = async (directory: string): Promise<void> => {
101-
const resolvedDirectory = path.resolve(directory);
103+
const isAmiInstalled = (): boolean => {
104+
try {
105+
execSync("ls /Applications/Ami.app", { stdio: "ignore" });
106+
return true;
107+
} catch {
108+
return false;
109+
}
110+
};
102111

103-
logger.log("Installing Ami...");
112+
const installAmi = (): void => {
113+
logger.log("Ami not found. Installing...");
104114
logger.break();
105-
106115
try {
107116
execSync(`curl -fsSL ${AMI_INSTALL_URL} | bash`, { stdio: "inherit" });
108117
} catch {
109118
logger.error("Failed to install Ami. Visit https://ami.dev to install manually.");
110119
process.exit(1);
111120
}
112-
113121
logger.break();
122+
};
123+
124+
const openAmiToFix = (directory: string): void => {
125+
const resolvedDirectory = path.resolve(directory);
126+
127+
if (!isAmiInstalled()) {
128+
installAmi();
129+
}
130+
114131
logger.log("Opening Ami to fix react-doctor issues...");
115132

116133
const deeplink = buildAmiDeeplink(resolvedDirectory);
117134

118135
try {
119136
execSync(`open "${deeplink}"`, { stdio: "ignore" });
120-
logger.success(`Opened Ami with react-doctor fix prompt.`);
137+
logger.success("Opened Ami with react-doctor fix prompt.");
121138
} catch {
122139
logger.break();
123140
logger.dim("Could not open Ami automatically. Open this URL manually:");
124141
logger.info(deeplink);
125142
}
126143
};
127144

145+
const fixAction = (directory: string) => {
146+
try {
147+
openAmiToFix(directory);
148+
} catch (error) {
149+
handleError(error);
150+
}
151+
};
152+
128153
const fixCommand = new Command("fix")
129-
.description("Install Ami and auto-fix react-doctor issues")
154+
.description("Open Ami to auto-fix react-doctor issues")
130155
.argument("[directory]", "project directory", ".")
131-
.action(async (directory: string) => {
132-
try {
133-
await installAndOpenAmi(directory);
134-
} catch (error) {
135-
handleError(error);
136-
}
137-
});
156+
.action(fixAction);
138157

139158
const installAmiCommand = new Command("install-ami")
140-
.description("Install Ami and auto-fix react-doctor issues")
159+
.description("Open Ami to auto-fix react-doctor issues")
141160
.argument("[directory]", "project directory", ".")
142-
.action(async (directory: string) => {
143-
try {
144-
await installAndOpenAmi(directory);
145-
} catch (error) {
146-
handleError(error);
147-
}
148-
});
161+
.action(fixAction);
149162

150163
program.addCommand(fixCommand);
151164
program.addCommand(installAmiCommand);

0 commit comments

Comments
 (0)