Skip to content

Commit

Permalink
(chore): move hash functions to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
2color authored and Jolg42 committed May 11, 2020
1 parent 4a93210 commit 1b0f48d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
35 changes: 1 addition & 34 deletions src/packages/cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { promisify } from 'util'
import path from 'path'
import dotenv from 'dotenv'
import chalk from 'chalk'
import crypto from 'crypto'
import { arg, drawBox, getSchemaPath } from '@prisma/sdk'
import { arg, drawBox, getCLIPathHash, getProjectHash } 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 @@ -209,36 +206,6 @@ 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 args = arg(process.argv.slice(3), { '--schema': String })

let projectPath = await getSchemaPath(args['--schema'])
projectPath = projectPath || process.cwd() // Default to cwd if the schema couldn't be found

return crypto
.createHash('sha256')
.update(projectPath)
.digest('hex')
.substring(0, 8)
}

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

process.on('SIGINT', () => {
process.exit(0) // now the "exit" event will fire
})
Expand Down
33 changes: 33 additions & 0 deletions src/packages/sdk/src/cli/hashes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getSchemaPath } from './getSchema'
import { arg } from './utils'
import crypto from 'crypto'

/**
* Get a unique identifier for the project by hashing
* the directory with `schema.prisma`
*/
export async function getProjectHash(): Promise<string> {
const args = arg(process.argv.slice(3), { '--schema': String })

let projectPath = await getSchemaPath(args['--schema'])
projectPath = projectPath || process.cwd() // Default to cwd if the schema couldn't be found

return crypto
.createHash('sha256')
.update(projectPath)
.digest('hex')
.substring(0, 8)
}

/**
* Get a unique identifier for the CLI installation path
* which can be either global or local (in project's node_modules)
*/
export function getCLIPathHash(): string {
const cliPath = process.argv[1]
return crypto
.createHash('sha256')
.update(cliPath)
.digest('hex')
.substring(0, 8)
}
1 change: 1 addition & 0 deletions src/packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
Dictionary,
CompiledGeneratorDefinition,
} from './cli/types'
export { getCLIPathHash, getProjectHash } from './cli/hashes'
export { arg, format, isError } from './cli/utils'
export {
getSchemaPath,
Expand Down

0 comments on commit 1b0f48d

Please sign in to comment.