Skip to content

Commit

Permalink
Generate install missing @prisma/client (with prisma2) automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 committed Feb 12, 2020
1 parent 944dd25 commit 244d6cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 40 deletions.
2 changes: 0 additions & 2 deletions cli/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@
"execa": "^3.4.0",
"flat-map-polyfill": "^0.3.8",
"globby": "^9.2.0",
"is-ci": "^2.0.0",
"make-dir": "^3.0.0",
"node-fetch": "2.6.0",
"p-map": "^3.0.0",
"prompts": "^2.3.0",
"read-pkg-up": "^7.0.0",
"resolve-pkg": "^2.0.0",
"rimraf": "^3.0.0",
Expand Down
58 changes: 20 additions & 38 deletions cli/sdk/src/predefinedGeneratorResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import resolvePkg from 'resolve-pkg'
import chalk from 'chalk'
import prompts from 'prompts'
import execa from 'execa'
import path from 'path'
import fs from 'fs'
import Debug from 'debug'
import isCi from 'is-ci'
const debugEnabled = Debug.enabled('generator')

export type GeneratorPaths = {
Expand Down Expand Up @@ -43,41 +41,36 @@ export const predefinedGeneratorResolvers: PredefinedGeneratorResolvers = {
!fs.existsSync(path.join(process.cwd(), 'package.json')) &&
!fs.existsSync(path.join(process.cwd(), '../package.json'))
) {
throw new PrismaClientFacadeMissingError()
}
if (!process.stdout.isTTY || isCi || process.env.GITHUB_ACTIONS) {
throw new PrismaClientFacadeMissingError()
} else {
console.log(
`In order to use the ${chalk.underline(
'"prisma-client-js"',
)} generator, you need to install ${chalk.bold(
'@prisma/client',
)} to your project.`,
)
const { value } = await prompts({
type: 'confirm',
name: 'value',
message: 'Do you want to install it now?',
initial: true,
})

if (!value) {
throw new PrismaClientFacadeMissingError()
const defaultPackageJson = `{
"name": "my-prisma-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

await installPackage(baseDir, `@prisma/client@${version ?? 'latest'}`)
`
fs.writeFileSync(path.join(process.cwd(), 'package.json'), defaultPackageJson)
}

await installPackage(baseDir, `--save-dev prisma2@${version ?? 'latest'}`)
await installPackage(baseDir, `@prisma/client@${version ?? 'latest'}`)
prismaClientDir = resolvePkg('@prisma/client', { cwd: baseDir })

if (!prismaClientDir) {
throw new Error(
`Could not resolve @prisma/client despite the installation that just happened. We're sorry.
Please try to install it by hand and rerun ${chalk.bold(
`Could not resolve @prisma/client despite the installation that we just tried.
Please try to install it by hand with ${chalk.bold.greenBright('npm install @prisma/client')} and rerun ${chalk.bold(
'prisma2 generate',
)} 🙏.`,
)
}

console.info(`We successfully installed the required Prisma packages ${chalk.bold.green('@prisma/client')} and ${chalk.bold.green('prisma2')} into your current project for you.`)
}

return {
Expand All @@ -87,17 +80,6 @@ Please try to install it by hand and rerun ${chalk.bold(
},
}

class PrismaClientFacadeMissingError extends Error {
constructor() {
super(`In order to use the ${chalk.underline(
'"prisma-client-js"',
)} generator, you need to install ${chalk.bold(
'@prisma/client',
)} to your project:
${chalk.bold.greenBright('npm install @prisma/client')}`)
}
}

async function installPackage(baseDir: string, pkg: string): Promise<void> {
const yarnInstalled = await isYarnInstalled()

Expand Down

0 comments on commit 244d6cc

Please sign in to comment.