-
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
95c92ff
commit ff4a8d1
Showing
20 changed files
with
1,272 additions
and
164 deletions.
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
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 was deleted.
Oops, something went wrong.
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
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,71 @@ | ||
import { arg, Command, format, HelpError, isError } from '@prisma/cli' | ||
import chalk from 'chalk' | ||
import { DatabaseType, DefaultParser } from 'prisma-datamodel' | ||
import { isdlToDatamodel2 } from '@prisma/sdk' | ||
|
||
export class Converter implements Command { | ||
public static new(): Converter { | ||
return new Converter() | ||
} | ||
|
||
// static help template | ||
private static help = format(` | ||
Convert a datamodel 1.1 to datamodel 2 | ||
${chalk.bold('Usage')} | ||
prisma2 convert | ||
${chalk.bold('Options')} | ||
-h, --help Displays this help message | ||
${chalk.bold('Examples')} | ||
${chalk.dim(`$`)} cat old-datamodel.prisma | prisma2 convert > new-datamodel.prisma | ||
`) | ||
private constructor() {} | ||
|
||
// parse arguments | ||
public async parse(argv: string[]): Promise<string | Error> { | ||
// parse the arguments according to the spec | ||
const args = arg(argv, { | ||
'--help': Boolean, | ||
'-h': '--help', | ||
}) | ||
|
||
if (isError(args)) { | ||
return this.help(args.message) | ||
} else if (args['--help']) { | ||
return this.help() | ||
} | ||
|
||
const datamodel = await this.readStdin() | ||
const parser = DefaultParser.create(DatabaseType.postgres) | ||
const isdl = parser.parseFromSchemaString(datamodel) | ||
return isdlToDatamodel2(isdl, []) | ||
} | ||
|
||
public readStdin(): Promise<string> { | ||
return new Promise(resolve => { | ||
let input = '' | ||
|
||
process.stdin.on('data', data => { | ||
input += data.toString() | ||
}) | ||
process.stdin.once('end', () => { | ||
resolve(input) | ||
}) | ||
process.stdin.setEncoding('utf-8') | ||
process.stdin.resume() | ||
}) | ||
} | ||
|
||
// help message | ||
public help(error?: string): string | HelpError { | ||
if (error) { | ||
return new HelpError(`\n${chalk.bold.red(`!`)} ${error}\n${Converter.help}`) | ||
} | ||
return Converter.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
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.