Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
mavilein committed Mar 19, 2019
2 parents f5c6dc8 + 433b613 commit f783df8
Show file tree
Hide file tree
Showing 135 changed files with 7,411 additions and 1,947 deletions.
3 changes: 3 additions & 0 deletions cli/packages/prisma-cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@types/fs-extra": "^5.0.0",
"@types/graphql": "^14.0.3",
"@types/node": "^8.0.22",
"@types/semver": "^5.5.0",
"faker": "^4.1.0",
"graphql-tools": "^4.0.3",
"husky": "^1.2.0",
Expand Down Expand Up @@ -76,6 +77,7 @@
}
},
"dependencies": {
"@types/express": "^4.16.1",
"adm-zip": "^0.4.7",
"archiver": "^2.0.3",
"callsites": "^2.0.0",
Expand Down Expand Up @@ -107,6 +109,7 @@
"prisma-generate-schema": "1.20.4",
"prisma-yml": "1.0.95",
"scuid": "^1.0.2",
"semver": "^5.6.0",
"sillyname": "^0.1.0",
"source-map-support": "^0.4.18",
"table": "^4.0.1"
Expand Down
57 changes: 57 additions & 0 deletions cli/packages/prisma-cli-core/src/commands/admin/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Command, flags, Flags } from 'prisma-cli-engine'
import chalk from 'chalk'
import * as opn from 'opn'
import { satisfiesVersion } from '../../utils/satisfiesVersion'

export default class Admin extends Command {
static topic = 'admin'
static description = 'Open service endpoints in Prisma Admin'

static flags: Flags = {
'env-file': flags.string({
description: 'Path to .env file to inject env vars',
char: 'e',
}),
}

async run() {
const envFile = this.flags['env-file']
await this.definition.load(this.flags, envFile)

const serviceName = this.definition.service!
const stage = this.definition.stage!

const token = this.definition.getToken(serviceName, stage)
const cluster = await this.definition.getCluster(false)
const clusterVersion = await cluster!.getVersion()

if (satisfiesVersion(clusterVersion!, '1.29.0')) {
let adminUrl = this.definition.endpoint + '/_admin'
if (token && token.length > 0) {
adminUrl += `?token=${token}`
}
this.out.log(`Opening Prisma Admin ${adminUrl} in the browser`)
opn(adminUrl).catch(() => {})
} else {
this.out.log(`Your Prisma server at ${chalk.bold(
`${this.definition.endpoint}`,
)} doesn't support Prisma Admin yet. Prisma Admin is supported from Prisma ${chalk.green(
`1.29`,
)} and higher. Your Prisma server currently uses Prisma ${chalk.red(
`${clusterVersion}`,
)}.\n\n
Please upgrade your Prisma server to use Prisma Admin.`)
this.out.exit(1)
}
}

normalizeVersion(version: string) {
version = version.replace(/-beta.*/, '').replace('-alpha', '')
const regex = /(\d+\.\d+)/
const match = regex.exec(version)
if (match) {
return match[1] + '.0'
}
return version
}
}
2 changes: 1 addition & 1 deletion cli/packages/prisma-cli-core/src/commands/console/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class ConsoleCommand extends Command {
async run() {
const url = `https://app.prisma.io`

this.out.log(`Opening prisma console ${url} in the browser`)
this.out.log(`Opening Prisma console ${url} in the browser`)
opn(url).catch(() => {}) // Prevent `unhandledRejection` error.
}
}
4 changes: 4 additions & 0 deletions cli/packages/prisma-cli-core/src/commands/delete/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default class Delete extends Command {
description: 'Path to .env file to inject env vars',
char: 'e',
}),
['project']: flags.string({
description: 'Path to Prisma definition file',
char: 'p',
}),
}
async run() {
const { force } = this.flags
Expand Down
171 changes: 26 additions & 145 deletions cli/packages/prisma-cli-core/src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import * as path from 'path'
import * as fs from 'fs-extra'
import { Seeder } from '../seed/Seeder'
const debug = require('debug')('deploy')
import { prettyTime, concatName, defaultDockerCompose } from '../../utils/util'
import {
prettyTime,
concatName,
defaultDockerCompose,
printAdminLink,
} from '../../utils/util'
import * as sillyname from 'sillyname'
import { EndpointDialog } from '../../utils/EndpointDialog'
import { spawnSync } from 'npm-run'
import { spawnSync as nativeSpawnSync } from 'child_process'
import * as figures from 'figures'
import { satisfiesVersion } from '../../utils/satisfiesVersion'

export default class Deploy extends Command {
static topic = 'deploy'
Expand Down Expand Up @@ -58,10 +64,12 @@ ${chalk.gray(
description: 'Path to .env file to inject env vars',
char: 'e',
}),
['project']: flags.string({
description: 'Path to Prisma definition file',
char: 'p',
}),
}
private deploying: boolean = false
private showedHooks: boolean = false
private loggedIn: boolean = false
async run() {
/**
* Get Args
Expand Down Expand Up @@ -95,7 +103,6 @@ ${chalk.gray(
*/
let workspace: string | undefined | null = this.definition.getWorkspace()
let cluster
let dockerComposeYml = defaultDockerCompose
if (!serviceName || !stage || interactive) {
await this.env.fetchClusters()
const endpointDialog = new EndpointDialog({
Expand All @@ -111,7 +118,6 @@ ${chalk.gray(
workspace = results.workspace
serviceName = results.service
stage = results.stage
dockerComposeYml = results.dockerComposeYml
this.definition.replaceEndpoint(results.endpoint)
// Reload definition because we are changing the yml file
await this.definition.load(this.flags, envFile)
Expand Down Expand Up @@ -182,12 +188,6 @@ ${chalk.gray(
)
}

private getSillyName() {
return `${slugify(sillyname()).split('-')[0]}-${Math.round(
Math.random() * 1000,
)}`
}

private async projectExists(
cluster: Cluster,
name: string,
Expand Down Expand Up @@ -232,7 +232,6 @@ ${chalk.gray(
workspace: string | null,
noMigrate: boolean,
): Promise<void> {
this.deploying = true
let before = Date.now()

const b = s => `\`${chalk.bold(s)}\``
Expand Down Expand Up @@ -351,9 +350,8 @@ ${chalk.gray(
}

// no action required
this.deploying = false
if (migrationResult.migration) {
this.printEndpoints(
await this.printEndpoints(
cluster,
serviceName,
stageName,
Expand Down Expand Up @@ -458,149 +456,32 @@ ${chalk.gray(
}
}

private printEndpoints(
private async printEndpoints(
cluster: Cluster,
serviceName: string,
stageName: string,
workspace?: string,
) {
this.out.log(`\n${chalk.bold(
'Your Prisma GraphQL database endpoint is live:',
)}
const version = await cluster.getVersion()
const hasAdmin = satisfiesVersion(version!, '1.29.0')
const adminText = hasAdmin
? printAdminLink(
cluster.getApiEndpoint(serviceName, stageName, workspace),
)
: ''

${chalk.bold('HTTP:')} ${cluster.getApiEndpoint(
serviceName,
stageName,
workspace,
)}
${chalk.bold('WS:')} ${cluster.getWSEndpoint(
this.out.log(`\n${'Your Prisma GraphQL database endpoint is live:'}
${'HTTP:'} ${cluster.getApiEndpoint(serviceName, stageName, workspace)}
${'WS:'} ${cluster.getWSEndpoint(
serviceName,
stageName,
workspace,
)}
)}${adminText}
`)
}

private getCloudClusters(): Cluster[] {
return this.env.clusters.filter(c => c.shared || c.isPrivate)
}

private async clusterSelection(loggedIn: boolean): Promise<string> {
debug({ loggedIn })

const choices = loggedIn
? await this.getLoggedInChoices()
: this.getPublicChoices()

const question = {
name: 'cluster',
type: 'list',
message: `Please choose the cluster you want to deploy to`,
choices,
pageSize: 9,
}

const { cluster } = await this.out.prompt(question)

if (cluster === 'login') {
await this.client.login()
this.loggedIn = true
return this.clusterSelection(true)
}

return cluster
}

private getLocalClusterChoices(): string[][] {
return [['local', 'Local cluster (requires Docker)']]
}

private async getLoggedInChoices(): Promise<any[]> {
await this.env.fetchClusters()
const localChoices = this.getLocalClusterChoices()
const combinations: string[][] = []
const remoteClusters = this.env.clusters.filter(
c => c.shared || c.isPrivate,
)

remoteClusters.forEach(cluster => {
const label = this.env.sharedClusters.includes(cluster.name)
? 'Free development cluster (hosted on Prisma Cloud)'
: 'Private Prisma Cluster'
combinations.push([`${cluster.workspaceSlug}/${cluster.name}`, label])
})

const allCombinations = [...combinations, ...localChoices]

return [
new inquirer.Separator(' '),
...this.convertChoices(allCombinations),
new inquirer.Separator(' '),
new inquirer.Separator(
chalk.dim(
`You can learn more about deployment in the docs: http://bit.ly/prisma-graphql-deployment`,
),
),
]
}

private convertChoices(
choices: string[][],
): Array<{ value: string; name: string }> {
const padded = this.out.printPadded(choices, 0, 6).split('\n')
return padded.map((name, index) => ({
name,
value: choices[index][0],
}))
}

private getPublicChoices(): any[] {
const publicChoices = [
[
'prisma-eu1',
'Public development cluster (hosted in EU on Prisma Cloud)',
],
[
'prisma-us1',
'Public development cluster (hosted in US on Prisma Cloud)',
],
]
const allCombinations = [...publicChoices, ...this.getLocalClusterChoices()]

return [
...this.convertChoices(allCombinations),
new inquirer.Separator(' '),
{
value: 'login',
name: 'Log in or create new account on Prisma Cloud',
},
new inquirer.Separator(' '),
new inquirer.Separator(
chalk.dim(
`Note: When not logged in, service deployments to Prisma Cloud expire after 7 days.`,
),
),
new inquirer.Separator(
chalk.dim(
`You can learn more about deployment in the docs: http://bit.ly/prisma-graphql-deployment`,
),
),
new inquirer.Separator(' '),
]
}
}

export function isValidProjectName(projectName: string): boolean {
return /^[A-Z](.*)/.test(projectName)
}

function slugify(text) {
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}
}
4 changes: 4 additions & 0 deletions cli/packages/prisma-cli-core/src/commands/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export default class Export extends Command {
description: 'Path to .env file to inject env vars',
char: 'e',
}),
['project']: flags.string({
description: 'Path to Prisma definition file',
char: 'p',
}),
}
async run() {
let exportPath =
Expand Down
Loading

0 comments on commit f783df8

Please sign in to comment.