Skip to content

Commit

Permalink
feat: add validate command
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Dec 12, 2019
1 parent d369807 commit fac2b79
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli/prisma2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"scripts": {
"test": "./fixtures/test.sh && ./node_modules/.bin/mocha src/__tests__/integrate.test.ts --require ts-mocha src/__tests__/integrate.test.ts --timeout 10s",
"test:debug": "./node_modules/.bin/mocha --inspect-brk src/__tests__/integrate.test.ts --require ts-mocha src/__tests__/integrate.test.ts --timeout 10s",
"install": "node download-build/index.js || echo \"Have fun with prisma2!\"",
"install": "node download-build/index.js || echo \"Have fun with Prisma!\"",
"download": "node scripts/updateTag.js && node download-build/index.js || echo \"\"",
"tsc": "tsc -d && cp src/capture-worker.js dist/capture-worker.js && scripts/copy-runtime-dist.sh",
"ncc": "ncc build dist/bin.js -o build",
Expand Down
78 changes: 78 additions & 0 deletions cli/prisma2/src/Validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Command, format, HelpError, getSchemaPath, arg } from '@prisma/cli'
import chalk from 'chalk'
import { getConfig, getDMMF } from '@prisma/sdk'
import fs from 'fs'

/**
* $ prisma migrate new
*/
export class Validate implements Command {
public static new(): Validate {
return new Validate()
}

// static help template
private static help = format(`
Validate a schema.
${chalk.bold('Usage')}
With an existing schema.prisma, just
${chalk.bold('prisma2 validate')}
Or specify a schema:
${chalk.bold('prisma2 validate --schema=./schema.prisma')}
`)
private constructor() {}

// parse arguments
public async parse(argv: string[]): Promise<string | Error> {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--schema': String,
})

if (args instanceof Error) {
return this.help(args.message)
}

if (args['--help']) {
return this.help()
}

let schemaPath: string | null = args['--schema'] || (await getSchemaPath())
if (!schemaPath) {
throw new Error(
`Either provide ${chalk.greenBright(
'--schema',
)} or make sure that you are in a folder with a ${chalk.greenBright('schema.prisma')} file.`,
)
}

if (!fs.existsSync(schemaPath)) {
throw new Error(`Provided schema at ${schemaPath} doesn't exist.`)
}

const schema = fs.readFileSync(schemaPath, 'utf-8')

await getDMMF({
datamodel: schema,
})

await getConfig({
datamodel: schema,
})

return `The schema at ${chalk.underline(schemaPath)} is valid 🚀`
}

// help message
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(`\n${chalk.bold.red(`!`)} ${error}\n${Validate.help}`)
}
return Validate.help
}
}
2 changes: 2 additions & 0 deletions cli/prisma2/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { Generate } from './Generate'
import chalk from 'chalk'
import { Docs } from './Docs'
import { ProviderAliases } from '@prisma/sdk'
import { Validate } from './Validate'
export { Photon } from '@prisma/studio-transports'

// aliases are only used by @prisma/studio, but not for users anymore,
Expand Down Expand Up @@ -76,6 +77,7 @@ async function main(): Promise<number> {
studio: StudioCommand.new(aliases),
generate: Generate.new(),
version: Version.new(),
validate: Validate.new(),
})
// parse the arguments
const result = await cli.parse(process.argv.slice(2))
Expand Down

0 comments on commit fac2b79

Please sign in to comment.