-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d369807
commit fac2b79
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters