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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/cli-helpers/src/lib/installHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ export const addApiPackages = (apiPackages: string[]) => ({
},
})

export const addRootPackages = (packages: string[], devDependency = false) => {
const addMode = devDependency ? ['add', '-D'] : ['add']
return {
title: 'Installing packages...',
task: async () => {
await execa('yarn', [...addMode, ...packages], { cwd: getPaths().base })
},
}
}

export const installPackages = {
title: 'Installing packages...',
task: async () => {
Expand Down
37 changes: 36 additions & 1 deletion packages/cli-helpers/src/lib/project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'

import { resolveFile } from '@redwoodjs/project-config'
import { resolveFile, findUp } from '@redwoodjs/project-config'

import { colors } from './colors'
import { getPaths } from './paths'
Expand Down Expand Up @@ -49,3 +49,38 @@ export const addEnvVarTask = (name: string, value: string, comment: string) => {
},
}
}

/**
* This sets the `RWJS_CWD` env var to the redwood project directory. This is typically required for internal
* redwood packages to work correctly. For example, `@redwoodjs/project-config` uses this when reading config
* or paths.
*
* @param cwd Explicitly set the redwood cwd. If not set, we'll try to determine it automatically. You likely
* only want to set this based on some specific input, like a CLI flag.
*/
export const setRedwoodCWD = (cwd?: string) => {
// Get the existing `cwd` from the `RWJS_CWD` env var, if it exists.
cwd ??= process.env.RWJS_CWD

if (cwd) {
// `cwd` was specifically passed in 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'))) {
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', process.cwd())
if (!redwoodTOMLPath) {
throw new Error(
`Couldn't find up a "redwood.toml" file from ${process.cwd()}`
)
}
if (redwoodTOMLPath) {
cwd = path.dirname(redwoodTOMLPath)
}
}

process.env.RWJS_CWD = cwd
}
Loading