Skip to content

Commit

Permalink
fix(cli): pass cli and project hash to checkpoint client
Browse files Browse the repository at this point in the history
Remove left over comment

Pass cli path to checkpoint
  • Loading branch information
2color authored and Jolg42 committed May 11, 2020
1 parent ee5b61a commit ce1f049
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/packages/cli/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#!/usr/bin/env ts-node

import fs from 'fs'
import { promisify } from 'util'
import path from 'path'
import dotenv from 'dotenv'
import chalk from 'chalk'
import crypto from 'crypto'
import { arg, drawBox } from '@prisma/sdk'
const packageJson = require('../package.json') // eslint-disable-line @typescript-eslint/no-var-requires

const exists = promisify(fs.exists)

export { byline } from '@prisma/migrate'

// do this before facebook's yoga
Expand Down Expand Up @@ -175,9 +178,17 @@ async function main(): Promise<number> {
return 1
}
console.log(result)

// Project hash is a SHA256 of the schemaPath
const projectHash = await getProjectHash()
// SHA256 of the cli path
const cliPathHash = await getCLIPathHash()

// check prisma for updates
const checkResult = await checkpoint.check({
product: 'prisma',
cli_path_hash: cliPathHash,
project: projectHash,
version: packageJson.version,
disable: ci.isCI,
})
Expand All @@ -198,6 +209,51 @@ async function main(): Promise<number> {
return 0
}

/**
* Get a unique identifier for the project by hashing
* the directory with `schema.prisma`
*/
async function getProjectHash(): Promise<string> {
const schemaPath = await getSchemaPath()

return crypto
.createHash('sha256')
.update(schemaPath)
.digest('hex')
.substring(0, 8)
}
/**
* Get a unique identifier for the CLI instllation path
* which can be either global or local (in project's node_modules)
*/
async function getCLIPathHash(): Promise<string> {
const cliPath = process.argv[1]
return crypto
.createHash('sha256')
.update(cliPath)
.digest('hex')
.substring(0, 8)
}

/**
* Get the path where `schema.prisma` lives
*/
async function getSchemaPath(): Promise<string> {
const cwd = process.cwd()
const prismaSchemaFile = 'schema.prisma'

if (await exists(path.join(cwd, prismaSchemaFile))) {
return cwd
}

if (await exists(path.join(cwd, 'prisma', prismaSchemaFile))) {
return path.normalize(path.join(cwd, 'prisma'))
}

// Default to cwd if prisma schema couldn't be found
return cwd
}

process.on('SIGINT', () => {
process.exit(0) // now the "exit" event will fire
})
Expand Down

0 comments on commit ce1f049

Please sign in to comment.