-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): implement add flag and config commands (#81)
* feat(cli): add command to create a flag via the cli * feat(cli): add command to create a remote config via the cli * refactor(cli): use prompts instead of promptly for prompts * test(cli): add tests for add flag and add config command * docs(cli): add docs for new add commands * feat(cli): do atomic update of config in add commands * feat(cli): rollback old config when push fails after adding flag/config * fix(cli): replace newlines only in the defineConfig part of a config file * feat(cli): bump version --------- Co-authored-by: Tim Raderschad <[email protected]>
- Loading branch information
1 parent
e86aca4
commit 0ca1bd4
Showing
14 changed files
with
401 additions
and
98 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
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# @tryabby/cli | ||
|
||
## 1.1.0 | ||
|
||
### Minor Changes | ||
|
||
- introduce new add commands | ||
|
||
## 1.0.1 | ||
|
||
### Patch Changes | ||
|
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,46 @@ | ||
import * as fs from "fs/promises"; | ||
import { default as prompts } from "prompts"; | ||
import { loadLocalConfig } from "./util"; | ||
import chalk from "chalk"; | ||
import { push } from "./push"; | ||
import { updateConfigFile } from "./update-config-file"; | ||
|
||
export async function addFlag(options: { apiKey: string; host?: string; configPath?: string }) { | ||
const { mutableConfig, saveMutableConfig, restoreConfig } = await loadLocalConfig( | ||
options.configPath | ||
); | ||
|
||
const { flagName } = await prompts({ | ||
type: "text", | ||
name: "flagName", | ||
message: "Type the name for your new flag: ", | ||
}); | ||
|
||
if (!mutableConfig.flags) { | ||
mutableConfig.flags = []; | ||
} | ||
|
||
if (mutableConfig.flags.includes(flagName)) { | ||
console.log(chalk.red("A flag with that name already exists!")); | ||
return; | ||
} | ||
|
||
mutableConfig.flags.push(flagName); | ||
|
||
try { | ||
console.log(chalk.blue("Updating local config...")); | ||
await saveMutableConfig(); | ||
console.log(chalk.green("Local config updated successfully")); | ||
|
||
console.log(chalk.blue("Updating remote config...")); | ||
await push({ apiKey: options.apiKey, configPath: options.configPath, apiUrl: options.host }); | ||
} catch (error) { | ||
console.log(chalk.red("Pushing flag failed, restoring old config file...")); | ||
|
||
await restoreConfig(); | ||
|
||
console.log(chalk.green("Restored old config file")); | ||
// pass error to command handler | ||
throw error; | ||
} | ||
} |
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,70 @@ | ||
import { loadLocalConfig } from "./util"; | ||
import chalk from "chalk"; | ||
import { push } from "./push"; | ||
import { default as prompts } from "prompts"; | ||
import { builders } from "magicast"; | ||
|
||
export async function addRemoteConfig(options: { | ||
apiKey: string; | ||
host?: string; | ||
configPath?: string; | ||
}) { | ||
const { mutableConfig, saveMutableConfig, restoreConfig } = await loadLocalConfig( | ||
options.configPath | ||
); | ||
|
||
const { remoteConfigName, remoteConfigType } = await prompts([ | ||
{ | ||
type: "text", | ||
name: "remoteConfigName", | ||
message: "Type the name for your new remote config: ", | ||
}, | ||
{ | ||
type: "select", | ||
name: "remoteConfigType", | ||
message: "Select the type for your new remote config: ", | ||
choices: [ | ||
{ | ||
title: "String", | ||
value: "String", | ||
}, | ||
{ | ||
title: "Number", | ||
value: "Number", | ||
}, | ||
{ | ||
title: "JSON", | ||
value: "JSON", | ||
}, | ||
], | ||
}, | ||
]); | ||
|
||
if (!mutableConfig.remoteConfig) { | ||
mutableConfig.remoteConfig = builders.literal({}); | ||
} | ||
|
||
if (remoteConfigName in mutableConfig.remoteConfig!) { | ||
console.log(chalk.red("A remote config with that name already exists!")); | ||
return; | ||
} | ||
|
||
mutableConfig.remoteConfig![remoteConfigName] = remoteConfigType; | ||
|
||
try { | ||
console.log(chalk.blue("Updating local config...")); | ||
await saveMutableConfig(); | ||
console.log(chalk.green("Local config updated successfully")); | ||
|
||
console.log(chalk.blue("Updating remote config...")); | ||
await push({ apiKey: options.apiKey, configPath: options.configPath, apiUrl: options.host }); | ||
console.log(chalk.green("Remote config updated successfully")); | ||
} catch (error) { | ||
console.log(chalk.red("Pushing the configuration failed. Restoring old config file...")); | ||
await restoreConfig(); | ||
console.log(chalk.green("Old config restored.")); | ||
|
||
// pass error to command handler | ||
throw error; | ||
} | ||
} |
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
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,3 @@ | ||
import { z } from "zod"; | ||
|
||
export const addCommandTypeSchema = z.enum(["flag", "config"]); |
Oops, something went wrong.
0ca1bd4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
abby-opensource – ./apps/web
abby-opensource.vercel.app
abby-opensource-git-main-dynabase.vercel.app
abby-opensource-dynabase.vercel.app
preview.tryabby.com
0ca1bd4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
abby-docs – ./apps/docs
abby-docs.vercel.app
docs.tryabby.dev
abby-docs-git-main-dynabase.vercel.app
docs.tryabby.com
abby-docs-dynabase.vercel.app