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

[legacy-framework] Fix usage with pnpm #2673

Merged
merged 13 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
64 changes: 41 additions & 23 deletions nextjs/packages/next/build/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { NextConfigComplete } from '../server/config-shared'
import { createPagesMapping } from './entries'
import { collectPages, getIsRpcFile } from './utils'
import { isInternalDevelopment } from '../server/utils'
import { join } from 'path'
import { existsSync, outputFile } from 'fs-extra'
import { baseLogger } from '../server/lib/logging'
import { join, dirname } from 'path'
import { outputFile } from 'fs-extra'
import findUp from 'next/dist/compiled/find-up'
import resolveFrom from 'resolve-from'
const readFile = promises.readFile
const manifestDebug = require('debug')('blitz:manifest')

export type RouteType = 'page' | 'rpc' | 'api'
export type RouteVerb = 'get' | 'post' | 'patch' | 'head' | 'delete' | '*'
Expand Down Expand Up @@ -116,7 +118,7 @@ export async function saveRouteManifest(

const { declaration, implementation } = generateManifest(routes)

const dotBlitz = join(findNodeModulesRoot(directory), '.blitz')
const dotBlitz = join(await findNodeModulesRoot(directory), '.blitz')

await outputFile(join(dotBlitz, 'index.js'), implementation, {
encoding: 'utf-8',
Expand All @@ -129,25 +131,41 @@ export async function saveRouteManifest(
})
}

function findNodeModulesRoot(src: string) {
const log = baseLogger()
let nodeModules = join(src, 'node_modules')
let includesBlitzPackage = existsSync(join(nodeModules, 'blitz'))
let count = 0
while (!includesBlitzPackage) {
// Check for node_modules at the next level up
nodeModules = join(nodeModules, '../../node_modules')
includesBlitzPackage = existsSync(join(nodeModules, 'blitz'))
count++
if (count > 5) {
log.warn(
"We couldn't determine your actual node_modules location, so defaulting to normal location"
)
nodeModules = join(src, 'node_modules')
break
}
async function findNodeModulesRoot(src: string) {
/*
* Because of our package structure, and because of how things like pnpm link modules,
* we must first find blitz package, and then find @blitzjs/core and then
* the root of @blitzjs/core
*
* This is because we import from `.blitz` inside @blitzjs/core.
* If that changes, then this logic here will need to change
*/
manifestDebug('src ' + src)
const blitzPkgLocation = dirname(
(await findUp('package.json', {
cwd: resolveFrom(src, 'blitz'),
})) ?? ''
)
manifestDebug('blitzPkgLocation ' + blitzPkgLocation)
if (!blitzPkgLocation) {
throw new Error(
"Internal Blitz Error: unable to find 'blitz' package location"
)
}
const blitzCorePkgLocation = dirname(
(await findUp('package.json', {
cwd: resolveFrom(blitzPkgLocation, '@blitzjs/core'),
})) ?? ''
)
manifestDebug('blitzCorePkgLocation ' + blitzCorePkgLocation)
if (!blitzCorePkgLocation) {
throw new Error(
"Internal Blitz Error: unable to find '@blitzjs/core' package location"
)
}
return nodeModules
const root = join(blitzCorePkgLocation, '../../')
manifestDebug('root ' + root)
return root
}

export function parseDefaultExportName(contents: string): string | null {
Expand Down Expand Up @@ -224,7 +242,7 @@ export function generateManifest(
'\n}',
declaration: `
import type { ParsedUrlQueryInput } from "querystring"
import type { RouteUrlObject } from "next/types"
import type { RouteUrlObject } from "blitz"

export const Routes: {
${declarationLines.map((line) => ' ' + line).join(';\n') + declarationEnding}
Expand Down
1 change: 1 addition & 0 deletions nextjs/packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"react-is": "17.0.2",
"react-query": "3.16.0",
"react-refresh": "0.8.3",
"resolve-from": "^5.0.0",
"secure-password": "4.0.0",
"stream-browserify": "3.0.0",
"stream-http": "3.1.1",
Expand Down
1 change: 1 addition & 0 deletions packages/blitz/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ export type {
RedirectAuthenticatedTo,
RedirectAuthenticatedToFnCtx,
RedirectAuthenticatedToFn,
RouteUrlObject,
} from "next/types"
7 changes: 3 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
],
"dependencies": {
"@blitzjs/display": "0.40.0-canary.5",
"@blitzjs/generator": "0.40.0-canary.5",
"@blitzjs/installer": "0.40.0-canary.5",
"@blitzjs/repl": "0.40.0-canary.5",
"@blitzjs/server": "0.40.0-canary.5",
"@oclif/command": "1.8.0",
"@oclif/config": "1.17.0",
"@oclif/plugin-autocomplete": "0.3.0",
Expand All @@ -43,7 +46,6 @@
"got": "^11.8.1",
"has-yarn": "2.1.0",
"hasbin": "1.2.3",
"import-cwd": "3.0.0",
"minimist": "1.2.5",
"p-event": "4.2.0",
"pkg-dir": "^5.0.0",
Expand All @@ -56,9 +58,6 @@
"v8-compile-cache": "2.2.0"
},
"devDependencies": {
"@blitzjs/generator": "0.40.0-canary.5",
"@blitzjs/installer": "0.40.0-canary.5",
"@blitzjs/server": "0.40.0-canary.5",
"@oclif/dev-cli": "1.26.0",
"@oclif/test": "1.2.8",
"nock": "13.0.6",
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/commands/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {Command} from "@oclif/command"

// @blitzjs/server imports react, so we must import the @blitzjs/server version of the
// local app instead of the global.
// import-cwd is required so this works correctly during new app generation
const getPrismaBin = async () => {
let bin: any
try {
bin = require("import-cwd")("@blitzjs/server").resolveBinAsync("prisma", "prisma")
bin = require("@blitzjs/server").resolveBinAsync("prisma", "prisma")
} catch {
// legacy compatability
bin = require("import-cwd")("@blitzjs/server").resolveBinAsync("@prisma/cli", "prisma")
bin = require("@blitzjs/server").resolveBinAsync("@prisma/cli", "prisma")
}
return bin
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ beforeEach(async () => {

const generatedToken = "plain-token"
jest.mock("blitz", () => ({
...jest.requireActual<object>("blitz")!,
...jest.requireActual<Record<string, unknown>>("blitz")!,
generateToken: () => generatedToken,
}))
jest.mock("preview-email", () => jest.fn())
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/templates/app/npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
save-exact=true
legacy-peer-deps=true

public-hoist-pattern[]=next
public-hoist-pattern[]=secure-password
public-hoist-pattern[]=*jest*
public-hoist-pattern[]=@testing-library/*
4 changes: 4 additions & 0 deletions packages/server/src/next-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {newline} from "next/dist/server/lib/logging"
import path from "path"
import pkgDir from "pkg-dir"
import {ServerConfig} from "./config"
const debug = require("debug")("blitz:utils")

function getSpawnEnv(config: ServerConfig) {
let spawnEnv: NodeJS.ProcessEnv = process.env
Expand Down Expand Up @@ -49,6 +50,9 @@ export async function nextStartDev(

process.env.BLITZ_DEV_SERVER_ORIGIN = `http://localhost:${availablePort}`

debug("cwd ", cwd)
debug("spawn ", nextBin, spawnCommand)

return new Promise<void>((res, rej) => {
if (config.port && availablePort !== config.port) {
log.error(`Couldn't start server on port ${config.port} because it's already in use`)
Expand Down
7 changes: 6 additions & 1 deletion packages/server/src/resolve-bin-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {readJSON} from "fs-extra"
import path from "path"
import pkgDir from "pkg-dir"
import resolveCwd from "resolve-cwd"
const debug = require("debug")("blitz:utils")

export async function resolveBinAsync(pkg: string, executable = pkg) {
const packageDir = await pkgDir(resolveCwd(pkg))
Expand All @@ -11,6 +12,10 @@ export async function resolveBinAsync(pkg: string, executable = pkg) {
const binPath = typeof bin === "object" ? bin[executable] : bin

if (!binPath) throw new Error(`No bin '${executable}' in module '${pkg}'`)
debug("binPath: " + binPath)

return path.join(packageDir, binPath)
const fullPath = path.join(packageDir, binPath)
debug("fullPath: " + fullPath)

return fullPath
}
14 changes: 0 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12625,13 +12625,6 @@ immediate@~3.0.5:
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-3.0.0.tgz#20845547718015126ea9b3676b7592fb8bd4cf92"
integrity sha512-4pnzH16plW+hgvRECbDWpQl3cqtvSofHWh44met7ESfZ8UZOWWddm8hEyDTqREJ9RbYHY8gi8DqmaelApoOGMg==
dependencies:
import-from "^3.0.0"

import-cwd@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-2.1.0.tgz#aa6cf36e722761285cb371ec6519f53e2435b0a9"
Expand Down Expand Up @@ -12662,13 +12655,6 @@ import-from@^2.1.0:
dependencies:
resolve-from "^3.0.0"

import-from@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/import-from/-/import-from-3.0.0.tgz#055cfec38cd5a27d8057ca51376d7d3bf0891966"
integrity sha512-CiuXOFFSzkU5x/CR0+z7T91Iht4CXgfCxVOFRhh2Zyhg5wOpWvvDLQUsWl+gcN+QscYBjez8hDCt85O7RLDttQ==
dependencies:
resolve-from "^5.0.0"

import-lazy@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"
Expand Down