-
Notifications
You must be signed in to change notification settings - Fork 990
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
approved these changes
Oct 15, 2023
There was a problem hiding this 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.
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>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
web
andapi
sides.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?