-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): Install studio package if not found (#8058)
* Automatically install studio package if not found * Use existing package check/install code and rework the check in the cli command * fix typo and comment auth reference
- Loading branch information
1 parent
8265e59
commit 090ef33
Showing
2 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import path from 'path' | ||
|
||
import execa from 'execa' | ||
import fs from 'fs-extra' | ||
|
||
import { getPaths } from './index' | ||
|
||
/** | ||
* Installs a Redwood module into a user's project keeping the version consistent with that of \@redwoodjs/cli. | ||
* If the module is already installed, this function does nothing. | ||
* If no remote version can not be found which matches the local cli version then the latest canary version will be used. | ||
* | ||
* @param {string} module A redwoodjs module, e.g. \@redwoodjs/web | ||
*/ | ||
export async function installRedwoodModule(module) { | ||
const packageJsonPath = require.resolve('@redwoodjs/cli/package.json') | ||
let { version } = fs.readJSONSync(packageJsonPath) | ||
|
||
if (!isModuleInstalled(module)) { | ||
const { stdout } = await execa.command( | ||
`yarn npm info ${module} --fields versions --json` | ||
) | ||
|
||
// If the version includes a plus, like '4.0.0-rc.428+dd79f1726' | ||
// (all @canary, @next, and @rc packages do), get rid of everything after the plus. | ||
if (version.includes('+')) { | ||
version = version.split('+')[0] | ||
} | ||
|
||
const versionIsPublished = JSON.parse(stdout).versions.includes(version) | ||
|
||
if (!versionIsPublished) { | ||
// Fallback to canary. This is most likely because it's a new package | ||
version = 'canary' | ||
} | ||
|
||
// We use `version` to make sure we install the same version as the rest | ||
// of the RW packages | ||
await execa.command(`yarn add -D ${module}@${version}`, { | ||
stdio: 'inherit', | ||
cwd: getPaths().base, | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Check if a user's project's package.json has a module listed as a dependency | ||
* or devDependency. If not, check node_modules. | ||
* | ||
* @param {string} module | ||
*/ | ||
export function isModuleInstalled(module) { | ||
const { dependencies, devDependencies } = fs.readJSONSync( | ||
path.join(getPaths().base, 'package.json') | ||
) | ||
|
||
const deps = { | ||
...dependencies, | ||
...devDependencies, | ||
} | ||
|
||
if (deps[module]) { | ||
return true | ||
} | ||
|
||
// Check any of the places require would look for this module. | ||
// This enables testing with `yarn rwfw project:copy`. | ||
// | ||
// We can't use require.resolve here because it caches the exception | ||
// Making it impossible to require when we actually do install it... | ||
return require.resolve | ||
.paths(`${module}/package.json`) | ||
.some((requireResolvePath) => { | ||
return fs.existsSync(path.join(requireResolvePath, module)) | ||
}) | ||
} |