Skip to content

Commit

Permalink
New non-interactive cli init
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 committed Jan 16, 2020
1 parent d75db24 commit d87e3ef
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 21 deletions.
1 change: 1 addition & 0 deletions cli/introspection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"jest": "^24.9.0",
"prettier": "^1.19.1",
"react": "16.11.0",
"rimraf": "^3.0.0",
"ts-jest": "^24.2.0",
"ts-node": "^8.5.4",
"typescript": "^3.7.3"
Expand Down
23 changes: 23 additions & 0 deletions cli/introspection/src/__tests__/__snapshots__/init.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`is schema wriiten on disk replace:
We created prisma/schema.prisma for you.
Edit it with your favorite editor to update your database connection so Prisma can connect to it.
When done, run prisma2 introspect to test the connection and introspect the data model from your existing database.
Then run prisma2 generate to generate a Prisma Client based on this data model that can be used in your application.
More information in our documentation:
http://tbd
1`] = `
"
We created prisma/schema.prisma for you.
Edit it with your favorite editor to update your database connection so Prisma can connect to it.
When done, run prisma2 introspect to test the connection and introspect the data model from your existing database.
Then run prisma2 generate to generate a Prisma Client based on this data model that can be used in your application.
More information in our documentation:
http://tbd
"
`;
31 changes: 31 additions & 0 deletions cli/introspection/src/__tests__/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { join } from 'path'
import mkdir from 'make-dir'
import { promisify } from 'util'
import fs from 'fs'
import rimraf from 'rimraf'
import { Init } from '../commands/Init'

const del = promisify(rimraf)
const tmp = join(__dirname, '../../tmp')

beforeEach(async () => {
await del(tmp)
await mkdir(tmp)
})

beforeEach(async () => {
await del(tmp)
})

test('is schema wriiten on disk replace', async () => {
const schemaPath = join(__dirname, '../commands/default.prisma')
const defaultSchema = fs.readFileSync(schemaPath, 'utf-8')

const init = Init.new()
const result = await init.parse(['tmp'])

const schema = fs.readFileSync(join(tmp, 'prisma/schema.prisma'), 'utf-8')

expect(schema).toMatch(defaultSchema)
expect(result).toMatchSnapshot(result)
})
73 changes: 54 additions & 19 deletions cli/introspection/src/commands/Init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Command, arg, format } from '@prisma/cli'
import { isError } from 'util'
import { initPrompt } from '../prompt/initPrompt'
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
Expand Down Expand Up @@ -30,31 +29,67 @@ export class Init implements Command {

const outputDirName = args._[0]
const outputDir = outputDirName ? path.join(process.cwd(), outputDirName) : process.cwd()
const prismaFolder = path.join(outputDir, 'prisma')

if (fs.existsSync(outputDir)) {
const schemaExists = fs.existsSync(path.join(outputDir, 'schema.prisma'))
const prismaSchemaExists = fs.existsSync(path.join(outputDir, 'prisma/schema.prisma'))
if (schemaExists || prismaSchemaExists) {
const filePath = schemaExists ? 'schema.prisma' : 'prisma/schema.prisma'
console.log(printError(`The project directory must not contain a ${chalk.bold(filePath)} file.`))
console.log(
printFix(`Run the command in a directory without a ${chalk.bold(filePath)} file
or provide a project name, e.g.: ${chalk.bold('prisma2 init hello-world')}`),
)
process.exit(1)
}
}

await initPrompt(outputDir)
if (fs.existsSync(path.join(outputDir, 'schema.prisma'))) {
console.log(printError(`File ${chalk.bold('schema.prisma')} already exists in your project.
Please try again in a project that is not yet using Prisma.
`))
process.exit(1)
}

if (fs.existsSync(prismaFolder)) {
console.log(printError(`Folder ${chalk.bold('prisma')} already exists in your project.
Please try again in a project that is not yet using Prisma.
`))
process.exit(1)
}

if (fs.existsSync(path.join(prismaFolder, 'schema.prisma'))) {
console.log(printError(`File ${chalk.bold('prisma/schema.prisma')} already exists in your project.
Please try again in a project that is not yet using Prisma.
`))
process.exit(1)
}

if (fs.existsSync(path.join(prismaFolder, 'schema.prisma'))) {
console.log(printError(`File ${chalk.bold('prisma/schema.prisma')} already exists in your project.
Please try again in a project that is not yet using Prisma.
`))
process.exit(1)
}

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir)
}

if (!fs.existsSync(prismaFolder)) {
fs.mkdirSync(prismaFolder)
}

const defaultSchema = fs.readFileSync(path.join(__dirname, 'default.prisma'), 'utf-8')

fs.writeFileSync(path.join(prismaFolder, 'schema.prisma'), defaultSchema)

return format(`
We created ${chalk.green('prisma/schema.prisma')} for you.
Edit it with your favorite editor to update your database connection so Prisma can connect to it.
When done, run ${chalk.green('prisma2 introspect')} to test the connection and introspect the data model from your existing database.
Then run ${chalk.green('prisma2 generate')} to generate a Prisma Client based on this data model that can be used in your application.
More information in our documentation:
http://tbd
`)
}

help() {
return console.log(
format(`
Usage: prisma2 init
Usage: prisma2 init
Initialize files for a new Prisma project
`),
Setup Prisma for your existing database
`),
)
}
}
4 changes: 2 additions & 2 deletions cli/prisma2/src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ export class CLI implements Command {
${chalk.bold('Commands')}
init Setup Prisma for your app
init Setup Prisma for your existing database
dev Develop your application in watch mode
lift Migrate your datamodel
introspect Get the datamodel of your database
generate Generate Photon
${chalk.bold('Examples')}
Initialize files for a new Prisma project
Setup Prisma for your existing database
${chalk.dim(`$`)} prisma2 init
Start developing and auto migrating your changes locally
Expand Down

0 comments on commit d87e3ef

Please sign in to comment.