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 192f6e4
Show file tree
Hide file tree
Showing 31 changed files with 287 additions and 101 deletions.
7 changes: 7 additions & 0 deletions apps/angular-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# angular-example

## 0.0.20

### Patch Changes

- @tryabby/angular@2.0.10
- @tryabby/devtools@5.0.1

## 0.0.19

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/angular-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-example",
"version": "0.0.19",
"version": "0.0.20",
"private": true,
"scripts": {
"ng": "ng",
Expand Down
9 changes: 9 additions & 0 deletions apps/web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# web

## 0.2.38

### Patch Changes

- Updated dependencies
- @tryabby/core@5.4.0
- @tryabby/devtools@5.0.1
- @tryabby/next@5.1.3

## 0.2.37

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web",
"version": "0.2.37",
"version": "0.2.38",
"private": true,
"scripts": {
"build": "next build",
Expand Down
7 changes: 7 additions & 0 deletions packages/angular/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/angular

## 2.0.10

### Patch Changes

- Updated dependencies
- @tryabby/core@5.4.0

## 2.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/angular",
"version": "2.0.9",
"version": "2.0.10",
"scripts": {
"ng": "ng",
"start": "ng serve",
Expand Down
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.2.0

### Minor Changes

- update ai command

## 1.1.1

### Patch Changes
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/cli",
"version": "1.1.1",
"version": "1.2.0",
"private": false,
"main": "./dist/index.js",
"bin": {
Expand Down Expand Up @@ -28,9 +28,10 @@
"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",
"ora": "^8.0.1",
"polka": "^0.5.2",
"portfinder": "^1.0.32",
"prettier": "^3.0.0",
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
27 changes: 20 additions & 7 deletions packages/cli/src/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getUseFeatureFlagRegex } from "@tryabby/core";
import { readFile } from "fs/promises";
import chalk from "chalk";
import { HttpService } from "./http";
import * as path from "path";

export async function removeFlagInstance(options: {
flagName: string;
Expand All @@ -13,8 +14,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 +23,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 @@ -45,14 +46,26 @@ export async function removeFlagInstance(options: {
apiUrl: options.host,
});

let updatedFileCount = filesToUse.length;
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);
updatedFileCount++;
await saveMutableConfig();
} catch (e) {
console.error(e);
// fail silently
}
console.log(chalk.green("Flag removed successfully"));
return updatedFileCount;
}
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
2 changes: 0 additions & 2 deletions packages/cli/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ 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) {
throw new Error("Internal server error trying to update files");
} else if (status === 401) {
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { addCommandTypeSchema } from "./schemas";
import { addFlag } from "./add-flag";
import { addRemoteConfig } from "./add-remote-config";
import { removeFlagInstance } from "./ai";
import ora from "ora";

const program = new Command();

Expand Down Expand Up @@ -195,14 +196,17 @@ aiCommand
.addOption(ConfigOption)
.addOption(HostOption)
.action(async (dir: string, flagName: string, options: { config?: string; host?: string }) => {
const files = await removeFlagInstance({
const spinner = ora("Removing feature flag with ✨AI✨").start();
const updatedFileCount = await removeFlagInstance({
apiKey: await getToken(),
flagName,
path: dir,
configPath: options.config,
host: options.host,
});
console.log(files);
spinner.succeed();
console.log(chalk.green("\nFlag removed successfully"));
console.log(chalk.green("Files updated:", updatedFileCount));
});

program.parse(process.argv);
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
17 changes: 12 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 Down Expand Up @@ -49,7 +56,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
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/core

## 5.4.0

### Minor Changes

- update ai command

## 5.3.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/core",
"version": "5.3.1",
"version": "5.4.0",
"description": "",
"main": "dist/index.js",
"files": [
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;
6 changes: 6 additions & 0 deletions packages/next/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tryabby/next

## 5.1.3

### Patch Changes

- @tryabby/react@5.2.2

## 5.1.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/next",
"version": "5.1.2",
"version": "5.1.3",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
7 changes: 7 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @tryabby/node

## 5.1.8

### Patch Changes

- Updated dependencies
- @tryabby/core@5.4.0

## 5.1.7

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tryabby/node",
"version": "5.1.7",
"version": "5.1.8",
"description": "",
"main": "dist/index.js",
"files": [
Expand Down
Loading

0 comments on commit 192f6e4

Please sign in to comment.