Skip to content

Commit

Permalink
update cli
Browse files Browse the repository at this point in the history
  • Loading branch information
trishouser committed Jul 31, 2023
1 parent 012317e commit 1bcca55
Show file tree
Hide file tree
Showing 19 changed files with 1,195 additions and 252 deletions.
14 changes: 7 additions & 7 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
},
"scripts": {
"start": "pnpm run build && ./dist/index.js",
"start2": "ts-node src/index.ts",
"start:windows": "nodemon --watch 'src/**/*.ts' --exec \"npx ts-node\" src/index.ts",
"create": "npm run build && npm run test",
"build": "esbuild src/index.ts --bundle --target=node10.4 --bundle --platform=node --format=cjs --outfile=dist/index.js --external:figlet",
"build:prod": "tsc --noEmit && pnpm run build",
"buildv2": "npx tsc",
"local": "sudo npm i -g && pizza",
"refresh": "rm -rf ./node_modules ./package-lock.json && npm install"
"refresh": "rm -rf ./node_modules ./package-lock.json && npm install",
"test": "vitest"
},
"dependencies": {
"chalk": "^5.2.0",
"clear": "^0.1.0",
"commander": "^10.0.1",
"figlet": "^1.6.0",
"msw": "^1.2.2",
"node-fetch": "^3.3.1",
"path": "^0.12.7",
"stringify-object": "^5.0.0",
"tsup": "^6.5.0"
"tsup": "^6.5.0",
"vite": "^4.4.2"
},
"devDependencies": {
"@tryabby/core": "workspace:^",
Expand All @@ -35,6 +34,7 @@
"nodemon": "^2.0.22",
"ts-node": "^10.9.1",
"tsconfig": "workspace:*",
"typescript": "^5.1.3"
"typescript": "^5.1.3",
"vitest": "^0.33.0"
}
}
15 changes: 0 additions & 15 deletions packages/cli/src/abby.ts

This file was deleted.

53 changes: 53 additions & 0 deletions packages/cli/src/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { HttpService } from "./http";
import { getConfigFromFileString, loadLocalConfig } from "./util";
import { AbbyConfig } from "@tryabby/core";

export async function check(filepath: string, apiKey: string, localhost?: boolean): Promise<boolean> {
const configFileString: string = await loadLocalConfig(filepath);
const configFromFile: AbbyConfig = getConfigFromFileString(configFileString);

const configFromAbby = await HttpService.getConfigFromServer(configFromFile.projectId, apiKey, localhost) as AbbyConfig;

let testsUpToDate = true;
let flagsUpToDate = true;

let output = false;

if (configFromAbby && configFromFile) {
if (configFromAbby.tests && configFromFile.tests) {
for (let serverTest in configFromAbby.tests) {
if (!(serverTest in configFromFile.tests)) {
testsUpToDate = false;
}
}
for (let localTest in configFromFile.tests) {
if (!(localTest in configFromAbby.tests)) {
testsUpToDate = false;
}
}

} else if (configFromAbby.tests || configFromFile.tests) testsUpToDate = false

if (configFromAbby.flags && configFromFile.flags) {
if (!(configFromAbby.flags != configFromFile.flags)) flagsUpToDate = false
} else if (configFromAbby.flags || configFromFile.flags) flagsUpToDate = false

if (testsUpToDate && flagsUpToDate) {
output = true;
console.log("all tests are up to date")
} else {
if (!testsUpToDate) {
output = false;
console.log("tests are not up to date")
}
if (!flagsUpToDate) {
output = false;
console.log("flags are not up to date")
}
}
return output
} else {
console.log("Something went wrong. Please check your login");
return false;
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "path";
import os from "os";

export const ABBY_BASE_URL = "http://www.tryabby.com";
export const LOCAL_BASE_URL = "http://localhost:3000";
export const ABBY_BASE_URL = "https://www.tryabby.com/";
export const LOCAL_BASE_URL = "http://localhost:3000/";

export const getTokenFilePath = () => path.join(os.homedir(), ".abby");
133 changes: 71 additions & 62 deletions packages/cli/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,82 @@ import { ConfigData } from "./types";
import { ABBY_BASE_URL, LOCAL_BASE_URL } from "./consts";
import fetch from "node-fetch";

export async function getConfigFromServer(
projectId: string,
apiKey: string,
localhost?: boolean
): Promise<unknown> {
let url: string;
export abstract class HttpService {
static async getConfigFromServer(
projectId: string,
apiKey: string,
localhost?: boolean
): Promise<any> {
let url: string;


if (localhost) {
console.log("LOCAL");
url = LOCAL_BASE_URL;
} else {
url = ABBY_BASE_URL;
}

const response = await fetch(`${url}/api/config/${projectId}?apiKey=${apiKey}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

const responseJson = await response.json();
return responseJson;
}

export async function updateConfigOnServer(
projectId: string,
apiKey: string,
localAbbyConfig: AbbyConfig,
localhost?: boolean
) {
let url: string;

if (localhost) {
console.log("LOCAL");
url = LOCAL_BASE_URL;
} else {
url = ABBY_BASE_URL;
}

try {
const response = await fetch(
`${url}/api/config/${projectId}?apiKey=${apiKey}`,
{
method: "PUT",
if (localhost) {
console.log("LOCAL");
url = LOCAL_BASE_URL;
} else {
url = ABBY_BASE_URL;
}
try {
const response = await fetch(`${url}/api/config/${projectId}?apiKey=${apiKey}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(localAbbyConfig),
}
);
// const res = await response;
const data = await response.json();
const status: number = response.status;

if (status == 200) {
console.log("pushed successfully");
} else if (status == 500) {
console.log(
"Pushed failed \n Please try again later \n 500: Internal server error"
);
} else if (status == 401) {
console.log("Pushed failed \n Please check your API key \n" + data);
});

const responseJson = await response.json();
return responseJson;
} catch (e) {
console.error(e)
throw e;
}
}

static async updateConfigOnServer(
projectId: string,
apiKey: string,
localAbbyConfig: AbbyConfig,
localhost?: boolean
) {
let url: string;

if (localhost) {
console.log("LOCAL");
url = LOCAL_BASE_URL;
} else {
console.log("Pushed failed: \n" + status + ": " + data);
url = ABBY_BASE_URL;
}

try {
const response = await fetch(
`${url}api/config/${projectId}?apiKey=${apiKey}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(localAbbyConfig),
}
);
// const res = await response;
const data = await response.json();
const status: number = response.status;

if (status == 200) {
console.log("pushed successfully");
} else if (status == 500) {
console.log(
"Pushed failed \n Please try again later \n 500: Internal server error"
);
} else if (status == 401) {
console.log("Pushed failed \n Please check your API key \n" + data);
} else {
console.log("Pushed failed: \n" + status + ": " + data);
}
} catch (e) {
console.log("Error: " + e);
}
} catch (e) {
console.log("Error: " + e);
}

}

77 changes: 57 additions & 20 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pull } from "./pull";
import { push } from "./push";
import { getToken, writeTokenFile } from "./auth";
import chalk from "chalk";
import { check } from "./check";

const program = new Command();

Expand All @@ -26,25 +27,61 @@ program
}
});

program
.command("push")
.description("push local config to server")
.argument("<filepath>", "filepath")
.option("-l, --localhost", "localhost")
.action((filepath, options) => {
if (!filepath) {
console.log(chalk.red("Filename is required"));
return;
}
try {
const token: string = getToken();
console.log(filepath);
console.log(options.localhost);
push(filepath, token, options.localhost);
} catch (e) {
console.log(chalk.red("Please login first"));
return;
}
});
program
.command("pull")
.argument("<filepath>", "filepath")
.option("-l, --localhost", "localhost")
.action((filepath, options) => {
if (!filepath) {
console.log(chalk.red("Filename is required"));
return;
}
try {
const token: string = getToken();
push(filepath, token, options.localhost);
} catch (e) {
console.error(e)
}
});


program
.command("push")
.description("push local config to server")
.argument("<filepath>", "filepath")
.option("-l, --localhost", "localhost")
.action( async (filepath, options) => {
if (!filepath) {
console.log(chalk.red("Filename is required"));
return;
}
try {
const token: string = getToken();
push(filepath, token, options.localhost);
} catch (e) {
console.log(chalk.red("Please login first"));
return;
}
});

program
.command("check")
.description("check local config against server")
.argument("<filepath>", "filepath")
.option("-l, --localhost", "localhost")
.action( async (filepath, options) => {
if (!filepath) {
console.log(chalk.red("Filename is required"));
return;
}
try {
const token: string = getToken();
const upToDate = await check(filepath, token, options.localhost);
} catch (e) {
console.log(chalk.red("Please login first"));
return;
}
});


program.parse(process.argv);
Loading

0 comments on commit 1bcca55

Please sign in to comment.