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

fix: resolve path ~ when checking if can import at @prisma/client #104

Merged
merged 7 commits into from
Jul 27, 2021
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
47 changes: 26 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,57 @@
"prepublishOnly": "yarn build"
},
"devDependencies": {
"@homer0/prettier-plugin-jsdoc": "^4.0.0",
"@homer0/prettier-plugin-jsdoc": "^4.0.1",
"@prisma-labs/prettier-config": "0.1.0",
"@prisma/client": "2.25.0",
"@prisma/sdk": "^2.25.0",
"@types/debug": "^4.1.5",
"@types/jest": "26.0.23",
"@types/lodash": "^4.14.170",
"@types/debug": "^4.1.7",
"@types/expand-tilde": "^2.0.0",
"@types/jest": "26.0.24",
"@types/lodash": "^4.14.171",
"@types/pluralize": "^0.0.29",
"@types/semver": "^7.3.6",
"@types/semver": "^7.3.8",
"@types/strip-ansi": "^5.2.1",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"arg": "^5.0.0",
"cross-env": "^7.0.3",
"dripip": "0.10.0",
"eslint": "^7.29.0",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-only-warn": "^1.0.2",
"execa": "^5.0.1",
"eslint-plugin-only-warn": "^1.0.3",
"execa": "^5.1.1",
"graphql": "^15.5.1",
"graphql-request": "^3.4.0",
"graphql-tag": "^2.12.4",
"jest": "27.0.4",
"graphql-request": "^3.5.0",
"graphql-tag": "^2.12.5",
"jest": "27.0.6",
"jest-watch-typeahead": "0.6.4",
"lodash": "^4.17.21",
"markdown-toc": "^1.2.0",
"nexus": "^1.1.0",
"nodemon": "^2.0.7",
"prettier": "2.3.1",
"nodemon": "^2.0.12",
"prettier": "2.3.2",
"prisma": "2.25.0",
"strip-ansi": "^6",
"ts-jest": "27.0.3",
"ts-node": "^10.0.0",
"type-fest": "^1.2.1",
"typescript": "4.3.4",
"zod": "^3.2.0"
"ts-jest": "27.0.4",
"ts-node": "^10.1.0",
"type-fest": "^1.2.2",
"typescript": "4.3.5",
"zod": "^3.5.1"
},
"prettier": "@prisma-labs/prettier-config",
"peerDependencies": {
"@prisma/client": "2.17.x || 2.18.x || 2.19.x || 2.20.x || 2.21.x || 2.22.x || 2.23.x || 2.24.x || 2.25.x",
"nexus": "^1.0.0"
},
"resolutions": {
"comment-parser": "1.1.5"
},
"dependencies": {
"@prisma/generator-helper": "^2.25.0",
"debug": "^4.3.1",
"debug": "^4.3.2",
"dindist": "^1.0.2",
"expand-tilde": "^2.0.2",
"fs-jetpack": "^4.1.0",
"graphql-scalars": "^1.10.0",
"kleur": "^4.1.4",
Expand Down
15 changes: 11 additions & 4 deletions src/cli/nexus-prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
process.env.DEBUG_COLORS = 'true'
process.env.DEBUG_HIDE_DATE = 'true'
import { GeneratorConfig, generatorHandler } from '@prisma/generator-helper'
import expandTilde from 'expand-tilde'
import * as Path from 'path'
import { generateRuntimeAndEmit } from '../generator'
import { loadUserGentimeSettings } from '../generator/gentime/settingsLoader'
import { Gentime } from '../generator/gentime/settingsSingleton'
import { d } from '../helpers/debugNexusPrisma'
import { externalToInternalDmmf } from '../helpers/prismaExternalToInternalDMMF'
import { resolveGitHubActionsWindowsPathTilde } from '../helpers/utils'

// todo by default error in ci and warn in local
// enforceValidPeerDependencies({
Expand Down Expand Up @@ -67,7 +69,7 @@ function getPrismaClientImportIdForItsGeneratorOutputConfig(
prismaClientGeneratorConfig: GeneratorConfig
): string {
const prismaClientPackageMoniker = `@prisma/client`
const prismaClientDefaultOutput = Path.join(`node_modules`, `@prisma`, `client`)
const prismaClientDefaultOutput = Path.normalize(`/node_modules/@prisma/client`)

if (!prismaClientGeneratorConfig.output || !prismaClientGeneratorConfig.output.value) {
return prismaClientPackageMoniker
Expand Down Expand Up @@ -102,9 +104,14 @@ function getPrismaClientImportIdForItsGeneratorOutputConfig(
* We're not certain what path standard we'll get from Prisma for example and ideally we don't care. Path.relative function should let us not
* care.
*/
const dirPrismaClientOutputWithoutTrailingNodeModulesMoniker =
prismaClientGeneratorConfig.output.value.replace(prismaClientDefaultOutput, '')
const dirProjectForThisNexusPrisma = Path.join(__dirname, '../../../..')
const dirPrismaClientOutputWithoutTrailingNodeModulesMoniker = resolveGitHubActionsWindowsPathTilde(
expandTilde(prismaClientGeneratorConfig.output.value.replace(prismaClientDefaultOutput, ''))
)

const dirProjectForThisNexusPrisma = resolveGitHubActionsWindowsPathTilde(
expandTilde(Path.join(__dirname, '../../../..'))
)

const dirDiff = Path.relative(
dirPrismaClientOutputWithoutTrailingNodeModulesMoniker,
dirProjectForThisNexusPrisma
Expand Down
41 changes: 41 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as Os from 'os'
import * as Path from 'path'
import { inspect } from 'util'

export type Resolver = (root: RecordUnknown, args: RecordUnknown, ctx: RecordUnknown) => MaybePromise<unknown>
Expand Down Expand Up @@ -26,3 +28,42 @@ export function arrayify<T>(x: T): T extends unknown[] ? T : T[] {
export function dump(x: unknown): void {
console.error(inspect(x, { depth: null }))
}

/**
* On GitHub Actions on Windows the path returned by __dirname is something like:
*
* C:\\Users\\RUNNER~1\\foo\\bar\\qux
*
* This function resolvers the `C:\\Users\\RUNNER~1` into the current system user's homedir.
*
* https://github.com/prisma/nexus-prisma/pull/104/checks?check_run_id=3175632323#step:9:146
* https://prisma-company.slack.com/archives/C016KUHB1R6/p1627416092002000
* https://github.com/actions/virtual-environments/issues/712
*/
export function resolveGitHubActionsWindowsPathTilde(path: string): string {
return resolveNestedTilde('RUNNER~1', path)
}

/**
* Sometimes a path tilde, shorthand for homedir, does not show up at the beginning of a path.
* For example:
*
* C:\\\\Users\\\\RUNNER~1\\\\foobar
*
* This function will look for the given tilde and if found replace it (and the path preceeding it if any)
* with the homedir.
*
* @example
*
* resolveNestedTilde('RUNNER~1', `C:\\Users\\RUNNER~1\\foo\\bar\\qux`) // --> <whatever homedir is>\\foo\\bar\\qux
*
*/
export function resolveNestedTilde(tildePattern: string, path: string): string {
if (!path.includes(tildePattern)) return path

const pathEnd = path.slice(path.indexOf(tildePattern) + tildePattern.length)
const pathStart = Os.homedir()
const newPath = Path.join(pathStart, pathEnd)

return newPath
}
8 changes: 6 additions & 2 deletions tests/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export namespace Specs {
authorId String
}
`,
apiSchema({ User, Post }) {
apiSchema(nexusPrisma) {
const { User, Post } = nexusPrisma

return [
queryType({
definition(t) {
Expand Down Expand Up @@ -80,7 +82,9 @@ export namespace Specs {
authorId String
}
`,
apiSchema({ User, Post }) {
apiSchema(nexusPrisma) {
const { User, Post } = nexusPrisma

return [
queryType({
definition(t) {
Expand Down
Loading