Skip to content

Commit

Permalink
feat(cli): major update msg + remove @prisma/cli update warning (#9079)
Browse files Browse the repository at this point in the history
Co-authored-by: Joël <joel.galeran@gmail.com>
  • Loading branch information
janpio and Jolg42 committed Sep 2, 2021
1 parent 076b58b commit e31d1c0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 156 deletions.
47 changes: 4 additions & 43 deletions packages/cli/scripts/preinstall.js
Expand Up @@ -77,45 +77,6 @@ function parsePackageManagerName(userAgent) {
}

export function main() {
if (__dirname.includes(`@prisma${path.sep}cli`)) {
console.error(
drawBox({
str: `
The package ${white('@prisma/cli')} has been renamed to ${white('prisma')}.
Please uninstall ${white('@prisma/cli')} first.
Then install ${white('prisma')} to continue using ${b('Prisma CLI')}:
# Uninstall old CLI
${white(
getPackageManagerName() === 'yarn'
? 'yarn remove @prisma/cli'
: 'npm uninstall @prisma/cli',
)}
# Install new CLI
${white(
getPackageManagerName() === 'yarn'
? `yarn add prisma --dev`
: `npm install prisma --save-dev`,
)}
# Invoke via npx
${white(
getPackageManagerName() === 'yarn'
? `yarn prisma --help`
: 'npx prisma --help',
)}
Learn more here: https://github.com/prisma/prisma/releases/tag/2.16.0
`,
verticalPadding: 1,
horizontalPadding: 3,
}),
)
process.exit(1)
}

const nodeVersions = process.version.split('.')
const nodeMajorVersion = parseInt(nodeVersions[0].slice(1))
const nodeMinorVersion = parseInt(nodeVersions[1])
Expand Down Expand Up @@ -157,10 +118,10 @@ Then install ${white('prisma')} to continue using ${b('Prisma 2.0')}:
# Uninstall old CLI
${white(
installedGlobally.pkgManager === 'yarn'
? 'yarn global remove prisma2'
: 'npm uninstall -g prisma2',
)}
installedGlobally.pkgManager === 'yarn'
? 'yarn global remove prisma2'
: 'npm uninstall -g prisma2',
)}
# Install new CLI
${white(`npm install prisma${isDev ? '@dev' : ''} --save-dev`)}
Expand Down
@@ -1,6 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should display a update message w/ dev tag 1`] = `
exports[`update available message dev tag - major 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 3.0.1 │
│ │
│ This is a major update - please follow the guide at │
│ https://pris.ly/d/major-version-upgrade │
│ │
│ Run the following to update │
│ npm i --save-dev prisma@dev │
│ npm i @prisma/client@dev │
└─────────────────────────────────────────────────────────┘
`;

exports[`update available message dev tag - minor 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 2.16.0 │
│ Run the following to update │
Expand All @@ -9,11 +22,24 @@ exports[`should display a update message w/ dev tag 1`] = `
└─────────────────────────────────────────────────────────┘
`;

exports[`should display a update message w/o tag 1`] = `
exports[`update available message latest tag - major 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 3.0.0 │
│ │
│ This is a major update - please follow the guide at │
│ https://pris.ly/d/major-version-upgrade │
│ │
│ Run the following to update │
│ npm i --save-dev prisma@latest │
│ npm i @prisma/client@latest │
└─────────────────────────────────────────────────────────┘
`;

exports[`update available message latest tag - minor 1`] = `
┌─────────────────────────────────────────────────────────┐
│ Update available 2.6.1 -> 2.16.0 │
│ Run the following to update │
│ npm i --save-dev prisma
│ npm i @prisma/client
│ npm i --save-dev prisma@latest
│ npm i @prisma/client@latest
└─────────────────────────────────────────────────────────┘
`;
94 changes: 65 additions & 29 deletions packages/cli/src/__tests__/update-message.test.ts
Expand Up @@ -4,38 +4,74 @@ import { consoleContext, Context } from './__helpers__/context'

const ctx = Context.new().add(consoleContext()).assemble()

it('should display a update message w/ dev tag', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '2.16.0',
package: 'prisma',
release_tag: 'dev',
},
describe('update available message', () => {
it('dev tag - minor', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '2.16.0',
package: 'prisma',
release_tag: 'dev',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('npm i --save-dev prisma@dev')
expect(message).toContain('npm i @prisma/client@dev')
expect(message).toMatchSnapshot()
})
// @ts-ignore
const message = ctx.mocked['console.error'].mock.calls[0][0]

// process.stdout.write(message + "\n")
expect(message).toMatchSnapshot()
})
it('dev tag - major', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '3.0.1',
package: 'prisma',
release_tag: 'dev',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('This is a major update')
expect(message).toContain('npm i --save-dev prisma@dev')
expect(message).toContain('npm i @prisma/client@dev')
expect(message).toMatchSnapshot()
})

it('should display a update message w/o tag', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '2.16.0',
package: 'prisma',
release_tag: 'latest',
},
it('latest tag - minor', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '2.16.0',
package: 'prisma',
release_tag: 'latest',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('npm i --save-dev prisma@latest')
expect(message).toContain('npm i @prisma/client@latest')
expect(message).toMatchSnapshot()
})
// @ts-ignore
const message = ctx.mocked['console.error'].mock.calls[0][0]

// process.stdout.write(message + "\n")
expect(message).toMatchSnapshot()
it('latest tag - major', () => {
printUpdateMessage({
status: 'ok',
// @ts-ignore
data: {
previous_version: '2.6.1',
current_version: '3.0.0',
package: 'prisma',
release_tag: 'latest',
},
})
const message = ctx.mocked['console.error'].mock.calls[0][0]
expect(message).toContain('This is a major update')
expect(message).toContain('npm i --save-dev prisma@latest')
expect(message).toContain('npm i @prisma/client@latest')
expect(message).toMatchSnapshot()
})
})
5 changes: 0 additions & 5 deletions packages/cli/src/bin.ts
Expand Up @@ -101,7 +101,6 @@ import { Doctor } from './Doctor'
import { Studio } from './Studio'
import { Telemetry } from './Telemetry'
import {
printPrismaCliUpdateWarning,
printUpdateMessage,
} from './utils/printUpdateMessage'
import { enginesVersion } from '@prisma/engines'
Expand All @@ -121,10 +120,6 @@ const isPrismaInstalledGlobally = isCurrentBinInstalledGlobally()
async function main(): Promise<number> {
// create a new CLI with our subcommands

if (__dirname.includes(`@prisma${path.sep}cli`)) {
printPrismaCliUpdateWarning()
}

detectPrisma1()

const cli = CLI.new(
Expand Down
115 changes: 40 additions & 75 deletions packages/cli/src/utils/printUpdateMessage.ts
Expand Up @@ -8,86 +8,49 @@ export function printUpdateMessage(checkResult: {
status: 'ok'
data: Check.Response
}): void {
console.error(
drawBox({
height: 4,
width: 59,
str: `\n${chalk.blue('Update available')} ${
checkResult.data.previous_version
} -> ${checkResult.data.current_version}\nRun the following to update
${chalk.bold(
makeInstallCommand(checkResult.data.package, checkResult.data.release_tag),
)}
${chalk.bold(
makeInstallCommand('@prisma/client', checkResult.data.release_tag, {
let boxHeight = 4
let majorText = ''

const currentVersionInstalled = checkResult.data.previous_version
const latestVersionAvailable = checkResult.data.current_version

const prismaCLICommand = makeInstallCommand(
checkResult.data.package,
checkResult.data.release_tag,
)
const prismaClientCommand = makeInstallCommand(
'@prisma/client',
checkResult.data.release_tag,
{
canBeGlobal: false,
canBeDev: false,
}),
)}`,
horizontalPadding: 2,
}),
},
)
}

export function makeUninstallCommand(
packageName: string,
tag: string,
options = {
canBeGlobal: true,
canBeDev: true,
},
): string {
// Examples
// yarn 'yarn/1.22.4 npm/? node/v12.14.1 darwin x64'
// npm 'npm/6.14.7 node/v12.14.1 darwin x64'
const yarnUsed = process.env.npm_config_user_agent?.includes('yarn')
try {
const [majorInstalled] = currentVersionInstalled.split('.')
const [majorLatest] = latestVersionAvailable.split('.')

let command = ''
if (isPrismaInstalledGlobally === 'yarn' && options.canBeGlobal) {
command = `yarn global remove ${packageName}`
} else if (isPrismaInstalledGlobally === 'npm' && options.canBeGlobal) {
command = `npm remove -g ${packageName}`
} else if (yarnUsed && options.canBeDev) {
command = `yarn remove ${packageName}`
} else if (options.canBeDev) {
command = `npm remove ${packageName}`
} else if (yarnUsed) {
command = `yarn remove ${packageName}`
} else {
command = `npm remove ${packageName}`
}
if (tag && tag !== 'latest') {
command += `@${tag}`
}
if (majorInstalled < majorLatest) {
majorText = `\nThis is a major update - please follow the guide at\nhttps://pris.ly/d/major-version-upgrade\n\n`
boxHeight = boxHeight + 4
}
} catch (e) {}

return command
}
const boxText = `\n${chalk.blue(
'Update available',
)} ${currentVersionInstalled} -> ${latestVersionAvailable}\n${majorText}Run the following to update
${chalk.bold(prismaCLICommand)}
${chalk.bold(prismaClientCommand)}`

/**
* Users of `@prisma/cli` will be pointed to `prisma`
*/
export function printPrismaCliUpdateWarning() {
logger.error(`${chalk.bold(
'@prisma/cli',
)} package has been renamed to ${chalk.bold('prisma')}.
Please uninstall ${chalk.bold('@prisma/cli')}: ${makeUninstallCommand(
'@prisma/cli',
'latest',
{
canBeGlobal: true,
canBeDev: false,
},
)}
And install ${chalk.bold.greenBright('prisma')}: ${makeInstallCommand(
'prisma',
'latest',
{
canBeGlobal: true,
canBeDev: true,
},
)}\n`)
const boxedMessage = drawBox({
height: boxHeight,
width: 59,
str: boxText,
horizontalPadding: 2,
})

process.exit(1)
console.error(boxedMessage)
}

function makeInstallCommand(
Expand Down Expand Up @@ -117,9 +80,11 @@ function makeInstallCommand(
} else {
command = `npm i ${packageName}`
}
if (tag && tag !== 'latest') {
command += `@${tag}`
}

// always output tag (so major upgrades work)
// see https://www.npmjs.com/package/prisma?activeTab=versions
// could be latest, dev, patch-dev, integration
command += `@${tag}`

return command
}

0 comments on commit e31d1c0

Please sign in to comment.