Skip to content

Commit

Permalink
feat(cli): use proper filepaths
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Aug 2, 2024
1 parent 18e5f96 commit 7e29d29
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 85 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"esbuild": "0.18.17",
"figlet": "^1.6.0",
"globby": "^14.0.2",
"magicast": "^0.3.2",
"magicast": "^0.3.4",
"msw": "^1.2.2",
"node-fetch": "^3.3.1",
"polka": "^0.5.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/add-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ 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 { mutableConfig, saveMutableConfig, restoreConfig } = await loadLocalConfig({
configPath: options.configPath,
});

const { flagName } = await prompts({
type: "text",
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/add-remote-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export async function addRemoteConfig(options: {
host?: string;
configPath?: string;
}) {
const { mutableConfig, saveMutableConfig, restoreConfig } = await loadLocalConfig(
options.configPath
);
const { mutableConfig, saveMutableConfig, restoreConfig } = await loadLocalConfig({
configPath: options.configPath,
});

const { remoteConfigName, remoteConfigType } = await prompts([
{
Expand Down
24 changes: 18 additions & 6 deletions packages/cli/src/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getUseFeatureFlagRegex } from "@tryabby/core";
import { readFile } from "fs/promises";
import chalk from "chalk";
import { HttpService } from "./http";
import * as path from "path";
import { copyFileSync } from "fs";

export async function removeFlagInstance(options: {
flagName: string;
Expand All @@ -13,8 +15,7 @@ export async function removeFlagInstance(options: {
configPath?: string;
}) {
const files = await globby("**/*.tsx", {
cwd: options.path ?? process.cwd(),
absolute: true,
cwd: options.path,
gitignore: true,
onlyFiles: true,
});
Expand All @@ -23,7 +24,8 @@ export async function removeFlagInstance(options: {

const filesToUse = (
await Promise.all(
files.flatMap(async (filePath) => {
files.flatMap(async (fp) => {
const filePath = path.join(options.path, fp);
const content = await readFile(filePath, "utf-8").then((content) => {
const matches = content.match(regex);
return matches ? content : null;
Expand All @@ -46,12 +48,22 @@ export async function removeFlagInstance(options: {
});

try {
const { mutableConfig, saveMutableConfig } = await loadLocalConfig(
options.configPath
const { mutableConfig, saveMutableConfig } = await loadLocalConfig({
configPath: options.configPath,
cwd: options.path,
});

if (mutableConfig.flags === undefined) {
console.error("No flags found in the config file");
return;
}

mutableConfig.flags = Array.from(mutableConfig.flags).filter(
(flag) => flag !== options.flagName
);
mutableConfig.flags = mutableConfig.flags.filter((flag: string) => flag !== options.flagName);
await saveMutableConfig();
} catch (e) {
console.error(e);
// fail silently
}
console.log(chalk.green("Flag removed successfully"));
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function verifyLocalConfig({
apiUrl?: string;
configPath?: string;
}) {
const { config: localConfig } = await loadLocalConfig(configPath);
const { config: localConfig } = await loadLocalConfig({ configPath });

const remoteConfig = await HttpService.getConfigFromServer({
projectId: localConfig.projectId,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export abstract class HttpService {
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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function pullAndMerge({
apiUrl?: string;
configPath?: string;
}): Promise<void> {
const { config: localConfig, configFilePath } = await loadLocalConfig(configPath);
const { config: localConfig, configFilePath } = await loadLocalConfig({ configPath });

const configFileContents = await fs.readFile(configFilePath, "utf-8");

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function push({
apiUrl?: string;
configPath?: string;
}) {
const { config } = await loadLocalConfig(configPath);
const { config } = await loadLocalConfig({ configPath });

await HttpService.updateConfigOnServer({
apiKey,
Expand Down
19 changes: 14 additions & 5 deletions packages/cli/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { abbyConfigSchema } from "@tryabby/core";
import { AbbyConfig, abbyConfigSchema } from "@tryabby/core";
import { loadConfig } from "unconfig";
import { config as loadEnv } from "dotenv";
import path from "path";
Expand All @@ -8,11 +8,18 @@ import { ABBY_BASE_URL } from "./consts";
import cors from "cors";
import fs from "fs/promises";
import { writeFile, loadFile, parseModule } from "magicast";
import { chownSync } from "fs";

export async function loadLocalConfig(configPath?: string) {
loadEnv();
export async function loadLocalConfig({
configPath,
cwd: initialCwd,
}: {
configPath?: string;
cwd?: string;
}) {
loadEnv({ path: initialCwd ? path.resolve(initialCwd, ".env") : "" });

let cwd = process.cwd();
let cwd = initialCwd ?? process.cwd();
let fileName = "abby.config";
let extensions = ["ts", "js", "mjs", "cjs"];

Expand All @@ -35,6 +42,8 @@ export async function loadLocalConfig(configPath?: string) {
cwd,
});

console.log(config, sources);

if (!config || !sources[0]) throw new Error("Could not load config file");

const result = await abbyConfigSchema.safeParseAsync(config);
Expand All @@ -49,7 +58,7 @@ export async function loadLocalConfig(configPath?: string) {
return {
config: result.data,
configFilePath: sources[0],
mutableConfig: mod.exports.default.$args[1],
mutableConfig: mod.exports.default.$args[1] as AbbyConfig,
saveMutableConfig: () => writeFile(mod, sources[0]),
restoreConfig: () => {
const mod = parseModule(originalConfig);
Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/shared/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import { AbbyEventType } from "./types";
import { AbbyConfig } from "..";

export const abbyEventSchema = z.object({
type: z.nativeEnum(AbbyEventType),
Expand All @@ -25,7 +26,7 @@ export const remoteConfigValueStringSchema = z.union([
export const abbyConfigSchema = z.object({
projectId: z.string(),
apiUrl: z.string().optional(),
currentEnvironment: z.string().optional(),
currentEnvironment: z.string(),
environments: z.array(z.string()),
tests: z
.record(
Expand Down Expand Up @@ -59,7 +60,14 @@ export const abbyConfigSchema = z.object({
})
.optional(),
debug: z.boolean().optional(),
});
cookies: z
.object({
disableByDefault: z.boolean().optional(),
expiresInDays: z.number().optional(),
})
.optional(),
__experimentalCdnUrl: z.string().optional(),
}) satisfies z.ZodType<AbbyConfig>;

export type AbbyConfigFile = z.infer<typeof abbyConfigSchema>;

Expand All @@ -75,7 +83,7 @@ export type RemoteConfigValueString = z.infer<typeof remoteConfigValueStringSche
export type RemoteConfigValueStringToType<T extends RemoteConfigValueString> = T extends "String"
? string
: T extends "Number"
? number
: T extends "JSON"
? Record<string, unknown>
: never;
? number
: T extends "JSON"
? Record<string, unknown>
: never;
Loading

0 comments on commit 7e29d29

Please sign in to comment.