forked from gatsbyjs/gatsby
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): add a flag for Yarn Plug'n'Play #1
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ const sysPath = require(`path`) | |
const report = require(`./reporter`) | ||
const url = require(`url`) | ||
const existsSync = require(`fs-exists-cached`).sync | ||
const semver = require(`semver`) | ||
|
||
const spawn = (cmd: string) => { | ||
const [file, ...args] = cmd.split(/\s+/) | ||
|
@@ -27,15 +28,56 @@ const shouldUseYarn = () => { | |
} | ||
} | ||
|
||
const checkYarnVersion = () => { | ||
let hasMinYarnPnp = false | ||
let yarnVersion = null | ||
try { | ||
yarnVersion = execSync(`yarnpkg --version`) | ||
.toString() | ||
.trim() | ||
hasMinYarnPnp = semver.gte(yarnVersion, `1.12.0`) | ||
} catch (err) { | ||
// ignore | ||
} | ||
return { | ||
hasMinYarnPnp: hasMinYarnPnp, | ||
yarnVersion: yarnVersion, | ||
} | ||
} | ||
|
||
// Executes `npm install` or `yarn install` in rootPath. | ||
const install = async rootPath => { | ||
const install = async ({ rootPath, usePnp }: Options) => { | ||
const prevDir = process.cwd() | ||
|
||
report.info(`Installing packages...`) | ||
process.chdir(rootPath) | ||
|
||
try { | ||
let cmd = shouldUseYarn() ? spawn(`yarnpkg`) : spawn(`npm install`) | ||
let cmd | ||
if (shouldUseYarn()) { | ||
const yarnInfo = checkYarnVersion() | ||
const args = [] | ||
if (usePnp) { | ||
if (!yarnInfo.hasMinYarnPnp) { | ||
if (yarnInfo.yarnVersion) { | ||
report.warn( | ||
`You are using Yarn ${ | ||
yarnInfo.yarnVersion | ||
} together with the --use-pnp flag, but Plug'n'Play is only supported starting from the 1.12 release.\n\n` + | ||
`Please update to Yarn 1.12 or higher for a better, fully supported experience.\n` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will need to update this with the version that comes out of yarnpkg/yarn#6682 |
||
) | ||
} | ||
// 1.11 had an issue with webpack-dev-middleware, so better not use PnP with it (never reached stable, but still) | ||
} else { | ||
args.push(`--enable-pnp`) | ||
} | ||
} | ||
|
||
cmd = execa(`yarnpkg`, args, { stdio: `inherit` }) | ||
} else { | ||
cmd = spawn(`npm install`) | ||
} | ||
|
||
await cmd | ||
} finally { | ||
process.chdir(prevDir) | ||
|
@@ -44,8 +86,13 @@ const install = async rootPath => { | |
|
||
const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path)) | ||
|
||
type Options = { | ||
rootPath: string, | ||
usePnp: boolean, | ||
} | ||
|
||
// Copy starter from file system. | ||
const copy = async (starterPath: string, rootPath: string) => { | ||
const copy = async (starterPath: string, { rootPath, usePnp }: Options) => { | ||
// Chmod with 755. | ||
// 493 = parseInt('755', 8) | ||
await fs.mkdirp(rootPath, { mode: 493 }) | ||
|
@@ -71,13 +118,13 @@ const copy = async (starterPath: string, rootPath: string) => { | |
|
||
report.success(`Created starter directory layout`) | ||
|
||
await install(rootPath) | ||
await install({ rootPath, usePnp }) | ||
|
||
return true | ||
} | ||
|
||
// Clones starter from URI. | ||
const clone = async (hostInfo: any, rootPath: string) => { | ||
const clone = async (hostInfo: any, { rootPath, usePnp }: Options) => { | ||
let url | ||
// Let people use private repos accessed over SSH. | ||
if (hostInfo.getDefaultRepresentation() === `sshurl`) { | ||
|
@@ -97,18 +144,20 @@ const clone = async (hostInfo: any, rootPath: string) => { | |
|
||
await fs.remove(sysPath.join(rootPath, `.git`)) | ||
|
||
await install(rootPath) | ||
await install({ rootPath, usePnp }) | ||
} | ||
|
||
type InitOptions = { | ||
rootPath?: string, | ||
usePnp?: boolean, | ||
} | ||
|
||
/** | ||
* Main function that clones or copies the starter. | ||
*/ | ||
module.exports = async (starter: string, options: InitOptions = {}) => { | ||
const rootPath = options.rootPath || process.cwd() | ||
const usePnp = options.usePnp || false | ||
|
||
const urlObject = url.parse(rootPath) | ||
if (urlObject.protocol && urlObject.host) { | ||
|
@@ -124,6 +173,6 @@ module.exports = async (starter: string, options: InitOptions = {}) => { | |
} | ||
|
||
const hostedInfo = hostedGitInfo.fromUrl(starter) | ||
if (hostedInfo) await clone(hostedInfo, rootPath) | ||
else await copy(starter, rootPath) | ||
if (hostedInfo) await clone(hostedInfo, { rootPath, usePnp }) | ||
else await copy(starter, { rootPath, usePnp }) | ||
} |
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
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
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.
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.
will have to update this after yarnpkg/yarn#6682 comes out (since Gatsby has a dependency,
relay-compiler
which is affected by this bug)