Skip to content

Commit

Permalink
Closes #824
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Nov 4, 2019
1 parent f062b67 commit d81ff14
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 49 deletions.
2 changes: 1 addition & 1 deletion cli/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/cli",
"version": "0.1.11",
"version": "0.1.19",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "git@github.com:prisma/prisma2.git",
Expand Down
47 changes: 36 additions & 11 deletions cli/cli/src/getSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@ import path from 'path'
const exists = promisify(fs.exists)
const readFile = promisify(fs.readFile)

function getContextualCwd() {
// For https://github.com/prisma/photonjs/issues/261
// When prisma2 generate is run from within node_modules, npm scripts provide that path as the process.cwd() path
// However, it also provides the init cwd in this environment variable: https://docs.npmjs.com/cli/run-script
return process.env.INIT_CWD || process.cwd()
}

/**
* Async
*/

export async function getSchemaPath(): Promise<string | null> {
let schemaPath = path.join(getContextualCwd(), 'schema.prisma')
// first try the normal cwd
const schemaPath = await getRelativeSchemaPath(process.cwd())

if (schemaPath) {
return schemaPath
}

// in case no schema can't be found there, try the npm-based INIT_CWD
if (process.env.INIT_CWD) {
return getRelativeSchemaPath(process.env.INIT_CWD)
}

return null
}

async function getRelativeSchemaPath(cwd: string): Promise<string | null> {
let schemaPath = path.join(cwd, 'schema.prisma')
if (await exists(schemaPath)) {
return schemaPath
}

schemaPath = path.join(getContextualCwd(), `prisma/schema.prisma`)
schemaPath = path.join(cwd, `prisma/schema.prisma`)

if (await exists(schemaPath)) {
return schemaPath
Expand Down Expand Up @@ -58,13 +67,29 @@ export async function getSchema(): Promise<string> {
*/

export function getSchemaPathSync(): string | null {
let schemaPath = path.join(getContextualCwd(), 'schema.prisma')
// first try intuitive schema path
const schemaPath = getRelativeSchemaPathSync(process.cwd())

if (schemaPath) {
return schemaPath
}

// in case the normal schema path doesn't exist, try the npm base dir
if (process.env.INIT_CWD) {
return getRelativeSchemaPathSync(process.env.INIT_CWD)
}

return null
}

function getRelativeSchemaPathSync(cwd: string): string | null {
let schemaPath = path.join(cwd, 'schema.prisma')

if (fs.existsSync(schemaPath)) {
return schemaPath
}

schemaPath = path.join(getContextualCwd(), `prisma/schema.prisma`)
schemaPath = path.join(cwd, `prisma/schema.prisma`)

if (fs.existsSync(schemaPath)) {
return schemaPath
Expand Down
1 change: 1 addition & 0 deletions cli/prisma2/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ photogen_prisma_ncc_build
examples
prisma-test-utils_ncc
tmp
@generated

prisma
.envrc
5 changes: 5 additions & 0 deletions cli/prisma2/fixtures/project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"postinstall": "cd subdir && ../../../build/index.js generate"
}
}
14 changes: 14 additions & 0 deletions cli/prisma2/fixtures/project/subdir/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
datasource my_db {
provider = "sqlite"
url = "file:dev7.db"
}

generator photon {
provider = "photonjs"
output = "@generated/photon"
}

model Blog {
id Int @id
viewCount Int
}
3 changes: 3 additions & 0 deletions cli/prisma2/fixtures/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

cd fixtures/project/ && yarn postinstall
12 changes: 6 additions & 6 deletions cli/prisma2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"version": "20b6dc13949cccccfef5be07c0be7a3d7c858abe"
},
"devDependencies": {
"@prisma/cli": "^0.1.17",
"@prisma/cli": "^0.1.19",
"@prisma/fetch-engine": "^0.3.4",
"@prisma/generator-helper": "^0.0.14",
"@prisma/introspection": "0.0.94",
"@prisma/lift": "0.3.65",
"@prisma/photon": "^0.2.133",
"@prisma/introspection": "0.0.95",
"@prisma/lift": "0.3.68",
"@prisma/photon": "^0.2.140",
"@prisma/sdk": "^0.0.21",
"@prisma/studio-transports": "^0.135.0",
"@prisma/studio-transports": "^0.139.0",
"@sentry/node": "5",
"@types/debug": "^4.1.5",
"@types/mocha": "^5.2.7",
Expand All @@ -50,7 +50,7 @@
"typescript": "3.5.2"
},
"scripts": {
"test": "./node_modules/.bin/mocha src/__tests__/integrate.test.ts --require ts-mocha src/__tests__/integrate.test.ts --timeout 10s",
"test": "./fixtures/test.sh && ./node_modules/.bin/mocha src/__tests__/integrate.test.ts --require ts-mocha src/__tests__/integrate.test.ts --timeout 10s",
"test:debug": "./node_modules/.bin/mocha --inspect-brk src/__tests__/integrate.test.ts --require ts-mocha src/__tests__/integrate.test.ts --timeout 10s",
"install": "node download-build/index.js || echo \"\"",
"download": "node scripts/updateTag.js && node download-build/index.js || echo \"\"",
Expand Down
2 changes: 1 addition & 1 deletion cli/prisma2/src/__tests__/integrate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async function generate(isdl: ISDL) {
],
)

const schemaPath = path.join(tmp, 'prisma.schema')
const schemaPath = path.join(tmp, 'schema.prisma')
fs.writeFileSync(schemaPath, datamodel)

const photonjsPath = require.resolve('@prisma/photon/generator-build')
Expand Down
2 changes: 1 addition & 1 deletion cli/prisma2/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"declaration": true,
"skipLibCheck": true
},
"exclude": ["dist", "build", "runtime", "examples", "src/__tests__"]
"exclude": ["dist", "build", "runtime", "examples", "src/__tests__", "fixtures"]
}
84 changes: 55 additions & 29 deletions cli/prisma2/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,16 @@
resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.3.tgz#2b5a3ab3f918cca48a8c754c08168e3f03eba61b"
integrity sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw==

"@prisma/cli@0.1.17", "@prisma/cli@^0.1.12", "@prisma/cli@^0.1.17", "@prisma/cli@^0.1.6":
"@prisma/cli@0.1.18":
version "0.1.18"
resolved "https://registry.yarnpkg.com/@prisma/cli/-/cli-0.1.18.tgz#c74a378a9201ff7870255175ae437779bb202ba0"
integrity sha512-s9wcHSUKZCRPhOe3o1yYWWbsBmz89/OJyWs5fHMHjcN8lhm+mMYd6KCRpkDNIANNtZProDuc8uIHe9wgcPXYDw==
dependencies:
arg "^4.1.0"
chalk "^2.4.2"
strip-indent "^3.0.0"

"@prisma/cli@^0.1.12", "@prisma/cli@^0.1.6":
version "0.1.17"
resolved "https://registry.yarnpkg.com/@prisma/cli/-/cli-0.1.17.tgz#713e613b2e71ee6be9f74213b15c7e29f0f594fd"
integrity sha512-gXdh9dzK218ZVIAy4wCqB/f5Q24UkWGZUQZyrbTNBKy+G573qz1IBLEGuSiEI2ZqDuWE34pq14KLUr7aWR4u2Q==
Expand All @@ -318,6 +327,15 @@
chalk "^2.4.2"
strip-indent "^3.0.0"

"@prisma/cli@^0.1.19":
version "0.1.19"
resolved "https://registry.yarnpkg.com/@prisma/cli/-/cli-0.1.19.tgz#19a99da0af3347b200c120bd7a908e2cb3fd342d"
integrity sha512-oNC5KIF+yV/W6WruekB5C8Zweo4l7ijBs4+x9pojLmwok56bF5kagmaN13O/lLF+iEh1JXn+aIUSH5zx6ZHuow==
dependencies:
arg "^4.1.0"
chalk "^2.4.2"
strip-indent "^3.0.0"

"@prisma/engine-core@0.0.98", "@prisma/engine-core@^0.0.98":
version "0.0.98"
resolved "https://registry.yarnpkg.com/@prisma/engine-core/-/engine-core-0.0.98.tgz#811735633a64fd901b4dc8102f89d32f38eca8ae"
Expand Down Expand Up @@ -391,10 +409,10 @@
strip-indent "^3.0.0"
terminal-link "^2.0.0"

"@prisma/introspection@0.0.94":
version "0.0.94"
resolved "https://registry.yarnpkg.com/@prisma/introspection/-/introspection-0.0.94.tgz#f91f4fd15c986572d1bd47af14296c8a3a066770"
integrity sha512-Ts/ttb+knXBSFvYdq0VJIGads2IiW34xKgz0FWDtkAw3d9MitC7I23xQ5hlKynRTvGn6bNP49gR1lj972AfNfw==
"@prisma/introspection@0.0.95":
version "0.0.95"
resolved "https://registry.yarnpkg.com/@prisma/introspection/-/introspection-0.0.95.tgz#5ad833d6bd8fbe39e601328c7abf2033ab9f4941"
integrity sha512-9WDt+hxmWtRGBnHzsZ47jxUPSkEldrHd6tP7ksZ8CAZzxkuiBzGYEaz41U+hGm16mFFyaONUgXqoFAhSZu/BQA==
dependencies:
"@prisma/fetch-engine" "^0.3.4"
"@prisma/ink-components" "^0.0.7"
Expand All @@ -421,15 +439,15 @@
tar "^4.4.10"
terminal-link "^2.0.0"

"@prisma/lift@0.3.65":
version "0.3.65"
resolved "https://registry.yarnpkg.com/@prisma/lift/-/lift-0.3.65.tgz#e346eb938588104458a44c476530dad7ed39e707"
integrity sha512-rJGSwUyLPNEOXYPtmLCZ1j4O0T5hKBbDWv7Ro5N8BprQh6qyjCOeQz6hpQsiPmQvcap4ll8uNc4aNfrXzxOjhQ==
"@prisma/lift@0.3.68":
version "0.3.68"
resolved "https://registry.yarnpkg.com/@prisma/lift/-/lift-0.3.68.tgz#8752e909eb4d38f13670fbe7b3ab5429b81476ab"
integrity sha512-a6ZcfthYQN22Zh156tWK/a4GvaGyjkMz7io+8NT4BwrCFeKG97LqGzzk7YjH2O75v11k/KyQLIHEMry0dyRopA==
dependencies:
"@prisma/fetch-engine" "^0.3.2"
"@prisma/get-platform" "^0.1.1"
"@prisma/ink-components" "^0.0.7"
"@prisma/studio-server" "0.135.0"
"@prisma/studio-server" "0.137.0"
"@types/execa" "^0.9.0"
adm-zip "^0.4.13"
ansi-escapes "^4.2.1"
Expand Down Expand Up @@ -466,13 +484,13 @@
strip-indent "^3.0.0"
supports-hyperlinks "^1.0.1"

"@prisma/photon@^0.2.133":
version "0.2.133"
resolved "https://registry.yarnpkg.com/@prisma/photon/-/photon-0.2.133.tgz#84b089939939835538ed8e88a65f4d9d8484b6c8"
integrity sha512-D1synxfEjujFT2x3OJKXo3iKtSv7HBTi5Ici9U/FgYAikbMPMHCEQrD1vxL8B1f0y3f0MLTvOm89HNrfj8zqiA==
"@prisma/photon@^0.2.140":
version "0.2.140"
resolved "https://registry.yarnpkg.com/@prisma/photon/-/photon-0.2.140.tgz#18a89734427c8ff8c437f23221e9674501fcb92c"
integrity sha512-eeuLc4782/S2C9vfLFSrpJhJKXm05JID03cUO2+Y+tRKBcDpXtFaa8ozymFspHi6FxtmHh1cNtq0Uve9wAVwcA==
dependencies:
"@apexearth/copy" "^1.4.2"
"@prisma/cli" "0.1.17"
"@prisma/cli" "0.1.18"
"@prisma/engine-core" "0.0.98"
"@prisma/fetch-engine" "^0.3.4"
"@prisma/generator-helper" "^0.0.14"
Expand Down Expand Up @@ -514,29 +532,37 @@
p-map "^3.0.0"
prisma-datamodel "1.36.0-test.2"

"@prisma/studio-server@0.135.0":
version "0.135.0"
resolved "https://registry.yarnpkg.com/@prisma/studio-server/-/studio-server-0.135.0.tgz#fbad69ab12d5fefeb690dd723d13c3a448ee13ed"
integrity sha512-zyjAo7hQKF2dHKdLpV0vUVcGFCZarChiKynuSp2ykECnq7N2VDwZYH2ZWo7FvCpdxbezqvGQVZ4FMANx3WQUsw==
"@prisma/studio-server@0.137.0":
version "0.137.0"
resolved "https://registry.yarnpkg.com/@prisma/studio-server/-/studio-server-0.137.0.tgz#c91e3822e243a4688c3010035c9b4951b2fdf19b"
integrity sha512-Wu7v0HeCA/kdyeX6XDyUrF7NjYAZYVwvbkcIrilSdWjqjETAU4zBkH7Z0XD76FM1+ut1LHND3OCbQ15u0p2iug==
dependencies:
"@prisma/studio" "0.135.0"
"@prisma/studio-transports" "0.135.0"
"@prisma/studio" "0.137.0"
"@prisma/studio-transports" "0.137.0"
"@sentry/node" "^5.6.2"
express "^4.17.1"
express-ws "^4.0.0"

"@prisma/studio-transports@0.135.0", "@prisma/studio-transports@^0.135.0":
version "0.135.0"
resolved "https://registry.yarnpkg.com/@prisma/studio-transports/-/studio-transports-0.135.0.tgz#0b1b002618d02dd0b223250be3c3bf4ed6b72e67"
integrity sha512-DyK0olJyWf4OgdRrVf2wv46nd43U0hnIykWpi+wq3UO13bseZ6HdYWZxA4VZTg7aBcFKBgUeIvlC8aeuC6qRAg==
"@prisma/studio-transports@0.137.0":
version "0.137.0"
resolved "https://registry.yarnpkg.com/@prisma/studio-transports/-/studio-transports-0.137.0.tgz#349d41c0b6c085908ece4c9a603116b2aaab5611"
integrity sha512-iR0q6jCNrMAE1C/RJb+ms2uwe8nVZ2WPLdfPJC9oNmZ6wuRCeI4oWkmJuILiE9c+uR8ObVSeBgfWwEKX3cDTcA==
dependencies:
"@sentry/node" "^5.6.2"
strip-ansi "^5.2.0"

"@prisma/studio-transports@^0.139.0":
version "0.139.0"
resolved "https://registry.yarnpkg.com/@prisma/studio-transports/-/studio-transports-0.139.0.tgz#408ec270dbe689e9ff3c4579292fac7b72869d69"
integrity sha512-MBfqb0/Dfavl2g9hgvaHhEibL767bYNi9enhltLIfv91Tk0cXzmNZJ3MFVJYPsMyrlYuZub8us4YhiuWDxOqZA==
dependencies:
"@sentry/node" "^5.6.2"
strip-ansi "^5.2.0"

"@prisma/studio@0.135.0":
version "0.135.0"
resolved "https://registry.yarnpkg.com/@prisma/studio/-/studio-0.135.0.tgz#e75a054a22b05311deacdf57419eae1427722975"
integrity sha512-jOOtL7ZWkN2w9lXoHXFc9D7ACwiMbYNqGG04/w325LtorpQTuLxFXVWj8OApxrMhbmQ2oJKSGTxy3qgn+tgT2A==
"@prisma/studio@0.137.0":
version "0.137.0"
resolved "https://registry.yarnpkg.com/@prisma/studio/-/studio-0.137.0.tgz#ade297f2bbb6331433563b82c1049ebbea8a1b50"
integrity sha512-3LtBF+rI2JziPiawQTFvL5j4qY6fLEJljSeZalb05/mCGEFuWSjilC8I9jXvM23zkrHutFKI/A1Viyi1PeMLzQ==

"@sentry/core@5.7.1":
version "5.7.1"
Expand Down

0 comments on commit d81ff14

Please sign in to comment.