Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli-helpers): Root package install and RWJS env var #9296

Merged
merged 4 commits into from
Oct 15, 2023

Conversation

Josh-Walker-GM
Copy link
Collaborator

Problem
When working through a new CLI setup/plugin with @dthyresson we noted some actions which would benefit from being a helper provided by redwood.

Changes

  1. Adds a function which installs packages at the root level. Similar functions already exist for the specific web and api sides.
  2. Adds a function which performs the typical check to assign the correct RWJS_CWD env var value. This is code which isn't immediately obvious to third parties and would have to be repeated in all setup command packages - making it a good candidate for a helper.

Notes
There are a couple more points I'd like to address but I will do so in follow up PRs.

I guess setRedwoodCWD could have unit testing but it feels simple enough to maybe squeak through without them?

@Josh-Walker-GM Josh-Walker-GM added the release:feature This PR introduces a new feature label Oct 11, 2023
@Josh-Walker-GM Josh-Walker-GM added this to the next-release milestone Oct 11, 2023
@Josh-Walker-GM Josh-Walker-GM added the fixture-ok Override the test project fixture check label Oct 11, 2023
@dthyresson
Copy link
Contributor

What is nice about this new function is that it will replace allllll this ...

#!/usr/bin/env node
/* eslint-disable prefer-const */
import fs from "fs";
import path from "path";
import findup from "findup-sync";
import Parser from "yargs-parser";
import { hideBin } from "yargs/helpers";
import { addApiPackages, writeFile, getPaths } from "@redwoodjs/cli-helpers";
import { Listr } from "listr2";
import { execa } from "execa";

// We take in yargs because we want to allow `--cwd` to be passed in, similar to the redwood cli itself.
let { cwd, help } = Parser(hideBin(process.argv));

// Redwood must set the `RWJS_CWD` env var to the project's root directory so that the internal libraries
// know where to look for files.
cwd ??= process.env["RWJS_CWD"];
try {
  if (cwd) {
    // `cwd` was set by the `--cwd` option or the `RWJS_CWD` env var. In this case,
    // we don't want to find up for a `redwood.toml` file. The `redwood.toml` should just be in that directory.
    if (!fs.existsSync(path.join(cwd, "redwood.toml")) && !help) {
      throw new Error(`Couldn't find a "redwood.toml" file in ${cwd}`);
    }
  } else {
    // `cwd` wasn't set. Odds are they're in a Redwood project,
    // but they could be in ./api or ./web, so we have to find up to be sure.

    const redwoodTOMLPath = findup("redwood.toml", { cwd: process.cwd() });

    if (!redwoodTOMLPath && !help) {
      throw new Error(
        `Couldn't find up a "redwood.toml" file from ${process.cwd()}`
      );
    }

    if (redwoodTOMLPath) {
      cwd = path.dirname(redwoodTOMLPath);
    }
  }
} catch (error) {
  if (error instanceof Error) {
    console.error(error.message);
  }

  process.exit(1);
}
process.env["RWJS_CWD"] = cwd;

>>>>--- Now I can can just start here :)

// Run run the setup function
async function setup() {
  const tasks = new Listr([
    // add the unkey sdk
    addApiPackages(["@unkey/api"]),
    {
      title: "Adding unkey.ts",
      task: async () => {
        const template = fs.readFileSync(
          path.resolve(__dirname, "templates", "unkey.ts.template"),
          "utf-8"
        );

        writeFile(path.join(getPaths().api.lib, "unkey.ts"), template, {
          existingFiles: "OVERWRITE",
        });

so I can get to implementing the setup command right away.

I approve!

@dthyresson dthyresson self-requested a review October 15, 2023 12:55
Copy link
Contributor

@dthyresson dthyresson left a comment

Choose a reason for hiding this comment

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

See prior comment on how these helpers ... help!

Approved.

@dthyresson dthyresson enabled auto-merge (squash) October 15, 2023 12:56
@dthyresson dthyresson merged commit d6436e4 into main Oct 15, 2023
31 checks passed
@dthyresson dthyresson deleted the jgmw-cli-helpers/utils-1 branch October 15, 2023 15:09
jtoar pushed a commit that referenced this pull request Nov 2, 2023
**Problem**
When working through a new CLI setup/plugin with @dthyresson we noted
some actions which would benefit from being a helper provided by
redwood.

**Changes**
1. Adds a function which installs packages at the root level. Similar
functions already exist for the specific `web` and `api` sides.
2. Adds a function which performs the typical check to assign the
correct `RWJS_CWD` env var value. This is code which isn't immediately
obvious to third parties and would have to be repeated in all setup
command packages - making it a good candidate for a helper.

**Notes**
There are a couple more points I'd like to address but I will do so in
follow up PRs.

I guess `setRedwoodCWD` could have unit testing but it feels simple
enough to maybe squeak through without them?

---------

Co-authored-by: David Thyresson <dthyresson@gmail.com>
@jtoar jtoar modified the milestones: next-release, v6.4.0 Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixture-ok Override the test project fixture check release:feature This PR introduces a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants