Skip to content

Commit

Permalink
feat(cli): bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Jan 7, 2024
1 parent 9f60718 commit 474a099
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 101 deletions.
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.0

### Minor Changes

- introduce new add commands

## 1.0.1

### Patch 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.0.2",
"version": "1.1.0",
"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",
"magicast": "^0.3.2",
"msw": "^1.2.2",
"node-fetch": "^3.3.1",
"polka": "^0.5.2",
Expand Down
29 changes: 13 additions & 16 deletions packages/cli/src/add-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,40 @@ import { push } from "./push";
import { updateConfigFile } from "./update-config-file";

export async function addFlag(options: { apiKey: string; host?: string; configPath?: string }) {
const { config, configFilePath } = await loadLocalConfig(options.configPath);
const configFileContents = await fs.readFile(configFilePath, "utf-8");

const oldConfig = JSON.parse(JSON.stringify(config));
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 (!config.flags) {
config.flags = [];
if (!mutableConfig.flags) {
mutableConfig.flags = [];
}

if (config.flags.includes(flagName)) {
if (mutableConfig.flags.includes(flagName)) {
console.log(chalk.red("A flag with that name already exists!"));
return;
}

config.flags.push(flagName);

console.log(chalk.blue("Updating local config..."));
await updateConfigFile(config, configFileContents, configFilePath);
console.log(chalk.green("Local config updated successfully"));

console.log(chalk.blue("Updating remote config..."));
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 updateConfigFile(oldConfig, configFileContents, configFilePath);
await restoreConfig();

console.log(chalk.green("Restored old config file"));

// pass error to command handler
throw error;
}
Expand Down
31 changes: 15 additions & 16 deletions packages/cli/src/add-remote-config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import * as fs from "fs/promises";
import { loadLocalConfig } from "./util";
import chalk from "chalk";
import { push } from "./push";
import { default as prompts } from "prompts";
import { updateConfigFile } from "./update-config-file";
import { builders } from "magicast";

export async function addRemoteConfig(options: {
apiKey: string;
host?: string;
configPath?: string;
}) {
const { config, configFilePath } = await loadLocalConfig(options.configPath);
const configFileContents = await fs.readFile(configFilePath, "utf-8");

const oldConfig = JSON.parse(JSON.stringify(config));
const { mutableConfig, saveMutableConfig, restoreConfig } = await loadLocalConfig(
options.configPath
);

const { remoteConfigName, remoteConfigType } = await prompts([
{
Expand Down Expand Up @@ -42,27 +40,28 @@ export async function addRemoteConfig(options: {
},
]);

if (!config.remoteConfig) {
config.remoteConfig = {};
if (!mutableConfig.remoteConfig) {
mutableConfig.remoteConfig = builders.literal({});
}

if (remoteConfigName in config.remoteConfig) {
if (remoteConfigName in mutableConfig.remoteConfig!) {
console.log(chalk.red("A remote config with that name already exists!"));
return;
}

config.remoteConfig[remoteConfigName] = remoteConfigType;

console.log(chalk.blue("Updating local config..."));
await updateConfigFile(config, configFileContents, configFilePath);
console.log(chalk.green("Local config updated successfully"));
mutableConfig.remoteConfig![remoteConfigName] = remoteConfigType;

console.log(chalk.blue("Updating remote config..."));
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 updateConfigFile(oldConfig, configFileContents, configFilePath);
await restoreConfig();
console.log(chalk.green("Old config restored."));

// pass error to command handler
Expand Down
17 changes: 16 additions & 1 deletion packages/cli/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import portFinder from "portfinder";
import polka from "polka";
import { ABBY_BASE_URL } from "./consts";
import cors from "cors";
import fs from "fs/promises";
import { writeFile, loadFile, parseModule } from "magicast";

export async function loadLocalConfig(configPath?: string) {
loadEnv();
Expand Down Expand Up @@ -40,7 +42,20 @@ export async function loadLocalConfig(configPath?: string) {
console.error(result.error);
throw new Error("Invalid config file");
}
return { config: result.data, configFilePath: sources[0] };
const originalConfig = await fs.readFile(sources[0], "utf-8");
const mod = await loadFile(sources[0]);
if (mod.exports.default.$type !== "function-call") throw new Error("Invalid config file");

return {
config: result.data,
configFilePath: sources[0],
mutableConfig: mod.exports.default.$args[1],
saveMutableConfig: () => writeFile(mod, sources[0]),
restoreConfig: () => {
const mod = parseModule(originalConfig);
return writeFile(mod, sources[0]);
},
};
}

export function multiLineLog(...args: any[]) {
Expand Down
50 changes: 25 additions & 25 deletions packages/cli/tests/base.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PullAbbyConfigResponse, defineConfig } from "@tryabby/core";
import { HttpService } from "../src/http";
import { writeFile as mgWriteFile } from "magicast";
import { writeFile } from "fs/promises";
import prompts from "prompts";

Expand All @@ -14,7 +15,12 @@ vi.mock("prettier", () => ({
}));

vi.mock("fs/promises", async () => ({
...((await vi.importActual("fs/promises")) as object),
...(await vi.importActual<typeof import("fs/promises")>("fs/promises")),
writeFile: vi.fn(),
}));

vi.mock("magicast", async () => ({
...(await vi.importActual<typeof import("magicast")>("magicast")),
writeFile: vi.fn(),
}));

Expand Down Expand Up @@ -114,15 +120,12 @@ describe("Abby CLI", () => {

it("adds a flag and pushes it", async () => {
prompts.inject(["newFlag"]);
const spy = vi.spyOn(HttpService, "updateConfigOnServer");
const httpSpy = vi.spyOn(HttpService, "updateConfigOnServer");

await addFlag({ apiKey: API_KEY, configPath: __dirname + "/abby.config.stub.ts" });

expect(writeFile).toHaveBeenCalledWith(
__dirname + "/abby.config.stub.ts",
expect.stringContaining("newFlag")
);
expect(spy).toHaveBeenCalledOnce();
expect(mgWriteFile).toHaveBeenCalledWith(expect.anything(), __dirname + "/abby.config.stub.ts");
expect(httpSpy).toHaveBeenCalledOnce();
});

it("adds a remote config and pushes it", async () => {
Expand All @@ -131,10 +134,7 @@ describe("Abby CLI", () => {

await addRemoteConfig({ apiKey: API_KEY, configPath: __dirname + "/abby.config.stub.ts" });

expect(writeFile).toHaveBeenCalledWith(
__dirname + "/abby.config.stub.ts",
expect.stringContaining("newRemoteConfig")
);
expect(mgWriteFile).toHaveBeenCalledWith(expect.anything(), __dirname + "/abby.config.stub.ts");
expect(spy).toHaveBeenCalledOnce();
});

Expand All @@ -145,21 +145,21 @@ describe("Abby CLI", () => {
throw new Error("failed");
});

let errorCatched = false;
let errorCaught = false;
try {
await addFlag({ apiKey: API_KEY, configPath: __dirname + "/abby.config.stub.ts" });
} catch (error) {
expect(error).instanceof(Error);
expect((error as Error).message).toBe("failed");
errorCatched = true;
errorCaught = true;
}

expect(errorCatched).toBe(true);
expect(errorCaught).toBe(true);

expect(writeFile).toHaveBeenCalledTimes(2);
expect(writeFile).toHaveBeenLastCalledWith(
__dirname + "/abby.config.stub.ts",
expect.not.stringContaining("newFlag")
expect(mgWriteFile).toHaveBeenCalledTimes(2);
expect(mgWriteFile).toHaveBeenLastCalledWith(
expect.anything(),
__dirname + "/abby.config.stub.ts"
);
});

Expand All @@ -170,21 +170,21 @@ describe("Abby CLI", () => {
throw new Error("failed");
});

let errorCatched = false;
let errorCaught = false;
try {
await addRemoteConfig({ apiKey: API_KEY, configPath: __dirname + "/abby.config.stub.ts" });
} catch (error) {
expect(error).instanceof(Error);
expect((error as Error).message).toBe("failed");
errorCatched = true;
errorCaught = true;
}

expect(errorCatched).toBe(true);
expect(errorCaught).toBe(true);

expect(writeFile).toHaveBeenCalledTimes(2);
expect(writeFile).toHaveBeenLastCalledWith(
__dirname + "/abby.config.stub.ts",
expect.not.stringContaining("newRemoteConfig")
expect(mgWriteFile).toHaveBeenCalledTimes(2);
expect(mgWriteFile).toHaveBeenLastCalledWith(
expect.anything(),
__dirname + "/abby.config.stub.ts"
);
});
});
Loading

0 comments on commit 474a099

Please sign in to comment.