Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add flag ai removal command #151

Merged
merged 1 commit into from
Aug 2, 2024
Merged
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
7 changes: 7 additions & 0 deletions apps/angular-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# angular-example

## 0.0.19

### Patch Changes

- @tryabby/angular@2.0.9
- @tryabby/devtools@5.0.1

## 0.0.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/angular-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-example",
"version": "0.0.18",
"version": "0.0.19",
"private": true,
"scripts": {
"ng": "ng",
Expand Down
9 changes: 9 additions & 0 deletions apps/web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# web

## 0.2.37

### Patch Changes

- Updated dependencies [d05cb9a]
- @tryabby/core@5.3.1
- @tryabby/devtools@5.0.1
- @tryabby/next@5.1.2

## 0.2.36

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web",
"version": "0.2.36",
"version": "0.2.37",
"private": true,
"scripts": {
"build": "next build",
Expand Down
7 changes: 7 additions & 0 deletions packages/angular/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/angular

## 2.0.9

### Patch Changes

- Updated dependencies [d05cb9a]
- @tryabby/core@5.3.1

## 2.0.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/angular",
"version": "2.0.8",
"version": "2.0.9",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/cli

## 1.1.1

### Patch Changes

- d05cb9a: add feature flag removal command

## 1.1.0

### Minor Changes
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/cli",
"version": "1.1.0",
"version": "1.1.1",
"private": false,
"main": "./dist/index.js",
"bin": {
Expand All @@ -27,6 +27,7 @@
"dotenv": "^16.0.3",
"esbuild": "0.18.17",
"figlet": "^1.6.0",
"globby": "^14.0.2",
"magicast": "^0.3.2",
"msw": "^1.2.2",
"node-fetch": "^3.3.1",
Expand Down
58 changes: 58 additions & 0 deletions packages/cli/src/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { loadLocalConfig } from "./util";
import { globby } from "globby";
import { getUseFeatureFlagRegex } from "@tryabby/core";
import { readFile } from "fs/promises";
import chalk from "chalk";
import { HttpService } from "./http";

export async function removeFlagInstance(options: {
flagName: string;
apiKey: string;
path: string;
host?: string;
configPath?: string;
}) {
const files = await globby("**/*.tsx", {
cwd: options.path ?? process.cwd(),
absolute: true,
gitignore: true,
onlyFiles: true,
});

const regex = getUseFeatureFlagRegex(options.flagName);

const filesToUse = (
await Promise.all(
files.flatMap(async (filePath) => {
const content = await readFile(filePath, "utf-8").then((content) => {
const matches = content.match(regex);
return matches ? content : null;
});
if (!content) return [];

return {
filePath,
fileContent: content,
};
})
)
).flat();
Comment on lines +24 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling for file reading.

The file reading and regex matching are correctly implemented, but consider adding error handling for potential file read errors.

-      files.flatMap(async (filePath) => {
+      files.flatMap(async (filePath) => {
+        try {
           const content = await readFile(filePath, "utf-8").then((content) => {
             const matches = content.match(regex);
             return matches ? content : null;
           });
           if (!content) return [];
           return {
             filePath,
             fileContent: content,
           };
+        } catch (error) {
+          console.error(`Error reading file ${filePath}:`, error);
+          return [];
+        }
       })
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const filesToUse = (
await Promise.all(
files.flatMap(async (filePath) => {
const content = await readFile(filePath, "utf-8").then((content) => {
const matches = content.match(regex);
return matches ? content : null;
});
if (!content) return [];
return {
filePath,
fileContent: content,
};
})
)
).flat();
const filesToUse = (
await Promise.all(
files.flatMap(async (filePath) => {
try {
const content = await readFile(filePath, "utf-8").then((content) => {
const matches = content.match(regex);
return matches ? content : null;
});
if (!content) return [];
return {
filePath,
fileContent: content,
};
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
return [];
}
})
)
).flat();


await HttpService.getFilesWithFlagsRemoved({
apiKey: options.apiKey,
files: filesToUse,
flagName: options.flagName,
apiUrl: options.host,
});

try {
const { mutableConfig, saveMutableConfig } = await loadLocalConfig(
options.configPath
);
mutableConfig.flags = mutableConfig.flags.filter((flag: string) => flag !== options.flagName);
await saveMutableConfig();
} catch (e) {
// fail silently
}
Comment on lines +48 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle specific exceptions for configuration update.

The local configuration update is correctly implemented, but consider handling specific exceptions to provide more informative error messages.

-  } catch (e) {
-    // fail silently
+  } catch (error) {
+    console.error(`Error updating configuration:`, error);
   }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
const { mutableConfig, saveMutableConfig } = await loadLocalConfig(
options.configPath
);
mutableConfig.flags = mutableConfig.flags.filter((flag: string) => flag !== options.flagName);
await saveMutableConfig();
} catch (e) {
// fail silently
}
try {
const { mutableConfig, saveMutableConfig } = await loadLocalConfig(
options.configPath
);
mutableConfig.flags = mutableConfig.flags.filter((flag: string) => flag !== options.flagName);
await saveMutableConfig();
} catch (error) {
console.error(`Error updating configuration:`, error);
}

console.log(chalk.green("Flag removed successfully"));
}
50 changes: 50 additions & 0 deletions packages/cli/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ABBY_BASE_URL } from "./consts";
import fetch from "node-fetch";
import { multiLineLog } from "./util";
import chalk from "chalk";
import { writeFile } from "fs/promises";

export abstract class HttpService {
static async getConfigFromServer({
Expand Down Expand Up @@ -69,4 +70,53 @@ export abstract class HttpService {
throw e;
}
}

static async getFilesWithFlagsRemoved({
apiKey,
files,
flagName,
apiUrl,
}: {
apiKey: string;
files: Array<{ filePath: string; fileContent: string }>;
flagName: string;
apiUrl?: string;
}) {
const url = apiUrl ?? ABBY_BASE_URL;

try {
const response = await fetch(`${url}/api/ee/v1/abby-ai/flag-removal`, {
method: "POST",
headers: {
Authorization: "Bearer " + apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify({
flagName,
files,
}),
});

const status = response.status;

if (status === 200) {
const res = await response.json();
if (!Array.isArray(res)) {
throw new Error("Invalid response from server");
}
console.log({ res, files });
await Promise.all(res.map((file) => writeFile(file.filePath, file.fileContent)));
console.log(chalk.green("All files have been updated successfully"));
} else if (status === 500) {
throw new Error("Internal server error trying to update files");
} else if (status === 401) {
throw new Error("Invalid API Key");
} else {
throw new Error("Unable to update files");
}
} catch (e) {
console.log(chalk.red(multiLineLog("Error: " + e)));
throw e;
}
}
}
21 changes: 21 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { initAbbyConfig } from "./init";
import { addCommandTypeSchema } from "./schemas";
import { addFlag } from "./add-flag";
import { addRemoteConfig } from "./add-remote-config";
import { removeFlagInstance } from "./ai";

const program = new Command();

Expand Down Expand Up @@ -184,4 +185,24 @@ program
}
});

const aiCommand = program.command("ai").description("Abby AI helpers");

aiCommand
.command("remove")
.description("remove a flag from your code")
.argument("<dir>", "The directory to scan for")
.argument("<flag>", "The flag name to remove")
.addOption(ConfigOption)
.addOption(HostOption)
.action(async (dir: string, flagName: string, options: { config?: string; host?: string }) => {
const files = await removeFlagInstance({
apiKey: await getToken(),
flagName,
path: dir,
configPath: options.config,
host: options.host,
});
console.log(files);
});

program.parse(process.argv);
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/core

## 5.3.1

### Patch Changes

- d05cb9a: add feature flag removal command

## 5.3.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/core",
"version": "5.3.0",
"version": "5.3.1",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ export function stringifyRemoteConfigValue(value: RemoteConfigValue) {
assertUnreachable(value);
}
}

export const getUseFeatureFlagRegex = (flagName: string) =>
new RegExp(`useFeatureFlag\\s*\\(\\s*['"\`]${flagName}['"\`]\\s*\\)`);
6 changes: 6 additions & 0 deletions packages/next/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/next

## 5.1.2

### Patch Changes

- @tryabby/react@5.2.1

## 5.1.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/next",
"version": "5.1.1",
"version": "5.1.2",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
7 changes: 7 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/node

## 5.1.7

### Patch Changes

- Updated dependencies [d05cb9a]
- @tryabby/core@5.3.1

## 5.1.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/node",
"version": "5.1.6",
"version": "5.1.7",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
7 changes: 7 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/react

## 5.2.1

### Patch Changes

- Updated dependencies [d05cb9a]
- @tryabby/core@5.3.1

## 5.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/react",
"version": "5.2.0",
"version": "5.2.1",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
6 changes: 6 additions & 0 deletions packages/remix/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/next

## 1.0.3

### Patch Changes

- @tryabby/react@5.2.1

## 1.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/remix/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/remix",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
7 changes: 7 additions & 0 deletions packages/svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/svelte

## 2.2.1

### Patch Changes

- Updated dependencies [d05cb9a]
- @tryabby/core@5.3.1

## 2.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/svelte",
"version": "2.2.0",
"version": "2.2.1",
"main": "dist/index.umd.cjs",
"homepage": "https://docs.tryabby.dev",
"type": "module",
Expand Down
Loading
Loading