Skip to content

Commit

Permalink
fix(cli): exit code 1 if generate fails
Browse files Browse the repository at this point in the history
Closes #2729
  • Loading branch information
timsuchanek committed Jul 1, 2020
1 parent 96380e5 commit ebe8e0d
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports['should work with a custom output dir 1'] = [
exports['generate should work with a custom output dir 1'] = [
{
"id": 1,
"email": "bob@bob.bob",
Expand Down
43 changes: 24 additions & 19 deletions src/packages/cli/src/Generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,30 @@ export class Generate implements Command {
schemaPath,
)}\n`

if (watchMode) {
if (!watchMode) {
const hint = `
You can now start using Prisma Client in your code:
\`\`\`
${highlightTS(`\
import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()`)}
\`\`\`
Explore the full API: ${link('http://pris.ly/d/client')}`
const message =
'\n' +
this.logText +
(isJSClient && !this.hasGeneratorErrored ? hint : '')

if (this.hasGeneratorErrored) {
throw new Error(message)
} else {
return message
}
} else {
logUpdate(watchingText + '\n' + this.logText)

fs.watch(schemaPath, async (eventType) => {
Expand Down Expand Up @@ -190,24 +213,6 @@ export class Generate implements Command {
}
})
await new Promise((_) => null) // eslint-disable-line @typescript-eslint/no-unused-vars
} else {
const hint = `
You can now start using Prisma Client in your code:
\`\`\`
${highlightTS(`\
import { PrismaClient } from '@prisma/client'
// or const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()`)}
\`\`\`
Explore the full API: ${link('http://pris.ly/d/client')}`
logUpdate(
'\n' +
this.logText +
(isJSClient && !this.hasGeneratorErrored ? hint : ''),
)
}

return ''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!prisma
!prisma/dev.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PrismaClient } from './generated/client'

export async function main() {
const client = new PrismaClient()

const data = await client.user.findMany()
client.disconnect()
return data
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
generator client {
provider = "prisma-client-js"
output = "../generated/client"
}

datasource db {
provider = "sqlite"
url = "file:dev.db"
}

model Post {
id Int @default(autoincrement()) @id
createdAt DateTime @default(now())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}

model Profile {
id Int @default(autoincrement()) @id
bio String?
user User @relation(fields: [userId], references: [id])
userId In @unique
}

model User {
id Int @default(autoincrement()) @id
email String @unique
name String?
posts Post[]
profile Profile?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es2017" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"strict": true /* Enable all strict type-checking options. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@prisma/cli@^2.0.0-alpha.1137":
version "2.0.0-alpha.1137"
resolved "https://registry.yarnpkg.com/@prisma/cli/-/cli-2.0.0-alpha.1137.tgz#5edb93bb430958d3def715679dda22e7c62ad0eb"
integrity sha512-54YANz8G7oVpEE+gVF6prbxT61Pyea3YHv2fPYZemBXscXlrlG7GZkDh0JX0yR1Rcp9cjL4QMsJvNO5XKzwAew==

"@prisma/client@^2.0.0-alpha.1137":
version "2.0.0-alpha.1137"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-2.0.0-alpha.1137.tgz#fd5216e4a8b836a373b887aa20466a53e3942828"
integrity sha512-UNKgKySmeC17YZ3OfqZpwU0zR4Ljfi1gWEO4+aLWo7DyrK3sdp/45G2EZGIOpX/weVDM+vcZWIMcWOyJ3apkEQ==

"@types/node@^13.11.1":
version "13.11.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.11.1.tgz#49a2a83df9d26daacead30d0ccc8762b128d53c7"
integrity sha512-eWQGP3qtxwL8FGneRrC5DwrJLGN4/dH1clNTuLfN81HCrxVtxRjygDTUoZJ5ASlDEeo0ppYFQjQIlXhtXpOn6g==

arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

source-map-support@^0.5.6:
version "0.5.16"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.16.tgz#0ae069e7fe3ba7538c64c98515e35339eac5a042"
integrity sha512-efyLRJDr68D9hBBNIPWFjhpFzURh+KJykQwvMyW5UiZzYwoF6l4YMMDIJJEyFWxWCqfyxLzz6tSfUFR+kXXsVQ==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"

source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

ts-node@^8.8.2:
version "8.8.2"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.8.2.tgz#0b39e690bee39ea5111513a9d2bcdc0bc121755f"
integrity sha512-duVj6BpSpUpD/oM4MfhO98ozgkp3Gt9qIp3jGxwU2DFvl/3IRaEAvbLa8G60uS7C77457e/m5TMowjedeRxI1Q==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.6"
yn "3.1.1"

typescript@^3.8.3:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==

yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==
80 changes: 54 additions & 26 deletions src/packages/cli/src/__tests__/integrate.generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,62 @@ import path from 'path'
import copy from '@apexearth/copy'
import execa from 'execa'
import snapshot from 'snap-shot-it'
import assert from 'assert'
import 'ts-node/register'

it('should work with a custom output dir', async () => {
// get temp dir
const target = tempy.directory()
// copy example into temp dir
// why a tmp dir? To make sure, we're outside of this workspace
await copy({
from: path.join(__dirname, './fixtures/example-project'),
to: target,
recursive: true,
})
// generate into temp dir
const data = await execa.node(
path.join(__dirname, '../../build/index.js'),
['generate'],
{
cwd: target,
stdio: 'pipe',
},
)
describe('generate', () => {
it('should work with a custom output dir', async () => {
// get temp dir
const target = tempy.directory()
// copy example into temp dir
// why a tmp dir? To make sure, we're outside of this workspace
await copy({
from: path.join(__dirname, './fixtures/example-project'),
to: target,
recursive: true,
})
// generate into temp dir
const data = await execa.node(
path.join(__dirname, '../../build/index.js'),
['generate'],
{
cwd: target,
stdio: 'pipe',
},
)

if (typeof data.signal === 'number' && data.signal !== 0) {
throw new Error(data.stderr + data.stdout)
}

if (typeof data.signal === 'number' && data.signal !== 0) {
throw new Error(data.stderr + data.stdout)
}
// run code
const { main } = await import(path.join(target, 'main.ts'))
const result = await main()
snapshot(result)
})

// run code
const { main } = await import(path.join(target, 'main.ts'))
const result = await main()
snapshot(result)
it('should error with exit code 1 with incorrect schema', async () => {
// get temp dir
const target = tempy.directory()
// copy example into temp dir
// why a tmp dir? To make sure, we're outside of this workspace
await copy({
from: path.join(__dirname, './fixtures/broken-example-project'),
to: target,
recursive: true,
})
// generate into temp dir
try {
const data = await execa.node(
path.join(__dirname, '../../build/index.js'),
['generate'],
{
cwd: target,
stdio: 'pipe',
},
)
} catch (e) {
assert.equal(e.exitCode, 1)
}
})
})

0 comments on commit ebe8e0d

Please sign in to comment.