-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add flag ai removal command
- Loading branch information
Showing
7 changed files
with
191 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@tryabby/core": patch | ||
"@tryabby/cli": patch | ||
--- | ||
|
||
add feature flag removal command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
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 | ||
} | ||
console.log(chalk.green("Flag removed successfully")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.