Skip to content

Conversation

@roderik
Copy link
Member

@roderik roderik commented Oct 5, 2024

Summary by Sourcery

Rebuild the CLI package by removing numerous commands and libraries, and enhance the utils package with new environment management features. Update the CI workflow to stop publishing certain SDK packages.

New Features:

  • Introduce environment management utilities with functions to load and write environment variables using dotenvx, including validation with a new DotEnv schema.

Enhancements:

  • Enhance the utils package by exporting new modules for environment management and filesystem operations.

CI:

  • Remove the publishing steps for the SDK Next and SDK Config packages from the CI workflow.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 5, 2024

Reviewer's Guide by Sourcery

This pull request rebuilds the CLI package, removing several packages and files while adding new functionality to the utils package. The changes focus on environment management, file system operations, and package restructuring.

Architecture diagram for CI/CD pipeline changes

graph TD;
    A[CI/CD Pipeline] -->|Removed| B[SDK Next Package]
    A -->|Removed| C[SDK Config Package]
    A -->|Retained| D[SDK Cli Package]
    A -->|Retained| E[SDK The Graph Package]
    A -->|Retained| F[SDK Hasura Package]
    A -->|Retained| G[SDK JS Package]
Loading

Class diagram for the new environment management

classDiagram
    class Environment {
        +loadEnv() Promise<DotEnv>
        +loadEnvironmentEnv(validateEnv: boolean, environment?: string) Promise<DotEnv | DotenvParseOutput>
    }
    class FileSystem {
        +projectRoot() Promise<string>
    }
    class Validation {
        +validate<T>(schema: T, value: unknown): T["_output"]
    }
    class DotEnvSchema {
        +SETTLEMINT_ENVIRONMENT: string
        +SETTLEMINT_INSTANCE: UrlSchema
        +SETTLEMINT_ACCESS_TOKEN: AccessTokenSchema
    }
    Environment --> FileSystem : uses
    Environment --> Validation : uses
    Environment --> DotEnvSchema : uses
    FileSystem --> "1" ProjectRoot : returns
    Validation --> "1" DotEnvSchema : validates
    DotEnvSchema --> "1" AccessTokenSchema
    DotEnvSchema --> "1" UrlSchema
Loading

File-Level Changes

Change Details Files
Removal of SDK Next and SDK Config packages from the build workflow
  • Removed publishing steps for SDK Next and SDK Config packages
  • Updated PR comment to exclude SDK Next and SDK Config versions
.github/workflows/build.yml
Addition of environment management functionality to the utils package
  • Added new entry point for environment.ts in tsup.config.ts
  • Created new files for loading and writing environment variables
  • Implemented DotEnvSchema for environment variable validation
packages/utils/tsup.config.ts
packages/utils/src/environment/load-env.ts
packages/utils/src/environment/write-env.ts
packages/utils/src/validation/dot-env.schema.ts
packages/utils/src/environment.ts
Enhancement of filesystem utilities in the utils package
  • Added projectRoot function to find the root directory of the project
  • Exported projectRoot function in filesystem.ts
packages/utils/src/filesystem/project-root.ts
packages/utils/src/filesystem.ts
Removal of CLI-specific command files and utilities
  • Deleted various command files (e.g., connect.ts, create.ts, forge.ts)
  • Removed utility files specific to CLI operations
packages/cli/src/commands/connect.ts
packages/cli/src/commands/create.ts
packages/cli/src/commands/forge.ts
packages/cli/src/lib/package-manager.ts
packages/cli/src/lib/run-cli.ts
packages/cli/src/lib/cli-message.ts
Deletion of Next.js related files and configurations
  • Removed Next.js specific files and directories
  • Deleted Next.js package configuration files
packages/next/src/node/next/with-settlemint.ts
packages/next/src/browser/sdk/plugins/viem.ts
packages/next/tsup.config.ts
packages/next/README.md
packages/next/tsconfig.json
Removal of config package
  • Deleted all files related to the config package
  • Removed config package configuration files
packages/config/src/schemas.ts
packages/config/src/writer.ts
packages/config/src/path.ts
packages/config/src/loader.ts
packages/config/README.md
packages/config/tsconfig.json
packages/config/tsup.config.ts
packages/config/knip.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions bot added the feat New feature label Oct 5, 2024
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @roderik - I've reviewed your changes - here's some feedback:

Overall Comments:

  • This pull request involves significant restructuring, particularly in the CLI package. Could you provide more context about the motivation behind these changes and their expected impact on the project's functionality and architecture?
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Review instructions: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

import { deepmerge } from "deepmerge-ts";
import { loadEnvironmentEnv } from "./load-env.js";

export async function writeEnv(env: DotEnv, environment: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider adding error handling to writeEnv function

The writeEnv function doesn't have any error handling. Consider adding try-catch blocks to handle potential file system errors when writing to the .env file.

export async function writeEnv(env: DotEnv, environment: string) {
  try {
    // Existing function implementation here
  } catch (error) {
    console.error(`Error writing .env file for ${environment}:`, error);
    throw error;
  }
}

* @returns A promise that resolves to the environment variables, either validated or raw.
* @throws Will throw an error if validation fails when validateEnv is true.
*/
export async function loadEnvironmentEnv<T extends boolean = true>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider restructuring the loadEnvironmentEnv function to improve code organization and readability.

While the loadEnvironmentEnv function handles a complex task, it can be restructured to improve readability and maintainability without losing functionality. Here's a suggested approach:

  1. Separate file path resolution:
function resolveEnvFilePaths(projectDir: string, environment?: string): string[] {
  return [
    `.env.${process.env.NODE_ENV}.local`,
    ".env.local",
    ...(environment ? [`.env.${environment}.local`, `.env.${environment}`] : []),
    `.env.${process.env.NODE_ENV}`,
    ".env",
  ].map((file) => join(projectDir, file));
}
  1. Simplify the main function:
export async function loadEnvironmentEnv<T extends boolean = true>(
  validateEnv: T,
  environment?: string
): Promise<T extends true ? DotEnv : DotenvParseOutput> {
  const projectDir = await projectRoot();
  const paths = resolveEnvFilePaths(projectDir, environment);

  let { parsed } = dotenvx.config({ path: paths });
  parsed = parsed || {};

  const envToUse = environment || parsed.SETTLEMINT_ENVIRONMENT;
  if (envToUse && envToUse !== environment) {
    return loadEnvironmentEnv(validateEnv, envToUse);
  }

  return validateEnv ? validate(DotEnvSchema, parsed) : parsed;
}

This restructuring:

  • Separates the file path resolution logic
  • Reduces nesting in the main function
  • Maintains the existing functionality
  • Improves readability by making the flow more linear

Consider adding comments to explain the purpose of each step, especially the environment fallback logic. This will make the code more self-documenting and easier to maintain in the future.

const quote = /[\s"'#]/;

function stringifyPair([key, val]: [string, unknown]): string {
if (!val) return `${key}=""`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Use block braces for ifs, whiles, etc. (use-braces)

Suggested change
if (!val) return `${key}=""`;
if (!val) {


ExplanationIt is recommended to always use braces and create explicit statement blocks.

Using the allowed syntax to just write a single statement can lead to very confusing
situations, especially where subsequently a developer might add another statement
while forgetting to add the braces (meaning that this wouldn't be included in the condition).

@github-actions
Copy link

github-actions bot commented Oct 6, 2024

📦 Packages

Package Version
SDK Cli @settlemint/sdk-cli@0.4.34-prf006314
SDK The Graph @settlemint/sdk-thegraph@0.4.34-prf006314
SDK Hasura @settlemint/sdk-hasura@0.4.34-prf006314
SDK JS @settlemint/sdk-js@0.4.34-prf006314
SDK Utils @settlemint/sdk-utils@0.4.34-prf006314

@roderik roderik merged commit abe86e7 into main Oct 6, 2024
@roderik roderik deleted the feat/rebuild-cli branch October 6, 2024 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants