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

feature: apply pnp #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,17 @@ module.exports = (argv, handlers) => {
.command({
command: `new [rootPath] [starter]`,
desc: `Create new Gatsby project.`,
builder: _ =>
_.option(`--use-pnp`, {
alias: `usePnp`,
type: `boolean`,
default: false,
describe: `Use Plug'n'Play`,
}),
handler: handlerP(
({ rootPath, starter = `gatsbyjs/gatsby-starter-default` }) => {
({ usePnp, rootPath, starter = `gatsbyjs/gatsby-starter-default` }) => {
const initStarter = require(`./init-starter`)
return initStarter(starter, { rootPath })
return initStarter(starter, { rootPath }, usePnp)
}
),
})
Expand Down
79 changes: 68 additions & 11 deletions packages/gatsby-cli/src/init-starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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+/)
Expand All @@ -27,16 +28,48 @@ const shouldUseYarn = () => {
}
}

function checkYarnVersion() {
let hasMinYarnPnp = false
let yarnVersion = null
try {
yarnVersion = execSync(`yarnpkg --version`)
.toString()
.trim()
let trimmedYarnVersion = /^(.+?)[-+].+$/.exec(yarnVersion)
if (trimmedYarnVersion) {
trimmedYarnVersion = trimmedYarnVersion.pop()
}
hasMinYarnPnp = semver.gte(trimmedYarnVersion || yarnVersion, `1.12.0`)
} catch (err) {
// ignore
}
return {
hasMinYarnPnp: hasMinYarnPnp,
yarnVersion: yarnVersion,
}
}

// Executes `npm install` or `yarn install` in rootPath.
const install = async rootPath => {
// Executes --enable-pnp if enabled
// Tracks installation time
const install = async (rootPath: string, usePnp: boolean) => {
const prevDir = process.cwd()

report.info(`Installing packages...`)
process.chdir(rootPath)

const pnpCommand = usePnp ? `--enable-pnp` : ``
const pnpText = usePnp
? `Using Plug'n'Play took`
: `Installing node modules took`
try {
let cmd = shouldUseYarn() ? spawn(`yarnpkg`) : spawn(`npm install`)
const startTime = new Date().getTime()
let cmd = shouldUseYarn()
? spawn(`yarnpkg ${pnpCommand}`)
: spawn(`npm install`)
await cmd
const endTime = new Date().getTime()
const totalTime = ((endTime - startTime) / 1000).toFixed(2)
report.info(`${pnpText} ${totalTime} seconds`)
} finally {
process.chdir(prevDir)
}
Expand All @@ -45,7 +78,7 @@ const install = async rootPath => {
const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path))

// Copy starter from file system.
const copy = async (starterPath: string, rootPath: string) => {
const copy = async (starterPath: string, rootPath: string, usePnp: boolean) => {
// Chmod with 755.
// 493 = parseInt('755', 8)
await fs.mkdirp(rootPath, { mode: 493 })
Expand All @@ -71,13 +104,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: string, usePnp: boolean) => {
let url
// Let people use private repos accessed over SSH.
if (hostInfo.getDefaultRepresentation() === `sshurl`) {
Expand All @@ -97,7 +130,7 @@ const clone = async (hostInfo: any, rootPath: string) => {

await fs.remove(sysPath.join(rootPath, `.git`))

await install(rootPath)
await install(rootPath, usePnp)
}

type InitOptions = {
Expand All @@ -107,9 +140,33 @@ type InitOptions = {
/**
* Main function that clones or copies the starter.
*/
module.exports = async (starter: string, options: InitOptions = {}) => {
module.exports = async (
starter: string,
options: InitOptions = {},
usePnp: boolean
) => {
if (usePnp) {
// Used in create-react-app to determine what version of yarn is being used and sets usePnp to false if it's before version 1.12
if (!shouldUseYarn()) {
report.warning(`NPM does not support PnP`)
usePnp = false
} else {
const yarnInfo = checkYarnVersion()
if (!yarnInfo.hasMinYarnPnp) {
if (yarnInfo.yarnVersion) {
report.warning(
`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`
)
}
// 1.11 had an issue with webpack-dev-middleware, so better not use PnP with it (never reached stable, but still)
usePnp = false
}
}
}
const rootPath = options.rootPath || process.cwd()

const urlObject = url.parse(rootPath)
if (urlObject.protocol && urlObject.host) {
report.panic(
Expand All @@ -124,6 +181,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)
}