Skip to content

Commit

Permalink
simplify cwd resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Sep 24, 2019
1 parent ec5c958 commit cd628c0
Show file tree
Hide file tree
Showing 17 changed files with 267 additions and 277 deletions.
2 changes: 1 addition & 1 deletion cli/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/cli",
"version": "0.0.35",
"version": "0.1.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "git@github.com:prisma/prisma.git",
Expand Down
10 changes: 0 additions & 10 deletions cli/cli/src/Env.ts

This file was deleted.

50 changes: 0 additions & 50 deletions cli/cli/src/getCwd.ts

This file was deleted.

90 changes: 90 additions & 0 deletions cli/cli/src/getSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { promisify } from 'util'
import fs from 'fs'
import path from 'path'

const exists = promisify(fs.exists)
const readFile = promisify(fs.readFile)

/**
* Async
*/

export async function getSchemaPath(): Promise<string | null> {
let schemaPath = path.join(process.cwd(), 'schema.prisma')

if (await exists(schemaPath)) {
return schemaPath
}

schemaPath = path.join(process.cwd(), `prisma/schema.prisma`)

if (await exists(schemaPath)) {
return schemaPath
}

return null
}

/**
* Small helper that returns the directory which contains the `schema.prisma` file
*/
export async function getSchemaDir(): Promise<string | null> {
const schemaPath = await getSchemaPath()
if (schemaPath) {
return path.dirname(schemaPath)
}

return null
}

export async function getSchema(): Promise<string> {
const schemaPath = await getSchemaPath()

if (!schemaPath) {
throw new Error(`Could not find ${schemaPath}`)
}

return readFile(schemaPath, 'utf-8')
}

/**
* Sync
*/

export function getSchemaPathSync(): string | null {
let schemaPath = path.join(process.cwd(), 'schema.prisma')

if (fs.existsSync(schemaPath)) {
return schemaPath
}

schemaPath = path.join(process.cwd(), `prisma/schema.prisma`)

if (fs.existsSync(schemaPath)) {
return schemaPath
}

return null
}

/**
* Small helper that returns the directory which contains the `schema.prisma` file
*/
export function getSchemaDirSync(): string | null {
const schemaPath = getSchemaPathSync()
if (schemaPath) {
return path.dirname(schemaPath)
}

return null
}

export function getSchemaSync(): string {
const schemaPath = getSchemaPathSync()

if (!schemaPath) {
throw new Error(`Could not find ${schemaPath}`)
}

return fs.readFileSync(schemaPath, 'utf-8')
}
10 changes: 8 additions & 2 deletions cli/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { Env } from './Env'
export { HelpError, unknownCommand } from './Help'
export {
Command,
Expand All @@ -12,4 +11,11 @@ export {
CompiledGeneratorDefinition,
} from './types'
export { arg, format, isError } from './utils'
export { getCwd, getDatamodel } from './getCwd'
export {
getSchemaPath,
getSchemaDir,
getSchema,
getSchemaPathSync,
getSchemaSync,
getSchemaDirSync,
} from './getSchema'
8 changes: 4 additions & 4 deletions cli/introspection/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@prisma/introspection",
"version": "0.0.52",
"version": "0.0.75",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "Apache-2.0",
"devDependencies": {
"@prisma/cli": "^0.0.70",
"@prisma/lift": "^0.2.55",
"@prisma/photon": "^0.2.61",
"@prisma/cli": "^0.1.0",
"@prisma/lift": "^0.2.66",
"@prisma/photon": "^0.2.73",
"@types/dotenv": "^6.1.1",
"@types/figures": "^3.0.1",
"@types/fs-extra": "^7.0.0",
Expand Down
10 changes: 2 additions & 8 deletions cli/introspection/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@
/**
* Dependencies
*/
import { isError, HelpError, Env } from '@prisma/cli'
import { isError, HelpError } from '@prisma/cli'
import { Init } from './commands/Init'

/**
* Main function
*/
async function main(): Promise<number> {
// load the environment
const env = await Env.load(process.env, process.cwd())
if (isError(env)) {
console.error(env)
return 1
}
// create a new CLI with our subcommands
const cli = Init.new(env)
const cli = Init.new()
// parse the arguments
const result = await cli.parse(process.argv.slice(2))
if (result instanceof HelpError) {
Expand Down
9 changes: 4 additions & 5 deletions cli/introspection/src/commands/Init.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Command, Env, arg, format } from '@prisma/cli'
import { Command, arg, format } from '@prisma/cli'
import { isError } from 'util'
import { mkdirpSync } from 'fs-extra'
import { initPrompt } from '../prompt/initPrompt'
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
import { printError, printFix } from '../prompt/utils/print'

export class Init implements Command {
static new(env: Env): Init {
return new Init(env)
static new(): Init {
return new Init()
}

private constructor(private readonly env: Env) {}
private constructor() {}

async parse(argv: string[]): Promise<any> {
// parse the arguments according to the spec
Expand Down
25 changes: 9 additions & 16 deletions cli/introspection/src/commands/Introspect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { arg, Command, Env, format, isError } from '@prisma/cli'
import { arg, Command, format, isError, getSchemaDir } from '@prisma/cli'
import { Result } from 'arg'
import chalk from 'chalk'
import fs from 'fs'
Expand Down Expand Up @@ -54,11 +54,11 @@ type Args = {
}

export class Introspect implements Command {
static new(env: Env): Introspect {
return new Introspect(env)
static new(): Introspect {
return new Introspect()
}

private constructor(private readonly env: Env) {}
private constructor() {}

async parse(argv: string[]): Promise<any> {
// parse the arguments according to the spec
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Introspect implements Command {
* Write the result to the filesystem
*/

const fileName = this.writeDatamodel(newDatamodelSdl)
const fileName = await this.writeDatamodel(newDatamodelSdl)

console.log(`\nCreated datamodel definition based on ${numTables} database tables`)
const andDatamodelText = referenceDatamodelExists ? ' and the existing datamodel' : ''
Expand All @@ -138,23 +138,16 @@ ${chalk.bold('Created 1 new file:')} Prisma DML datamodel (derived from existing
process.exit(0)
}

getExistingDatamodel(): string | undefined {
const datamodelPath = path.join(this.env.cwd, 'schema.prisma')
if (!fs.existsSync(datamodelPath)) {
return undefined
}
return fs.readFileSync(datamodelPath, 'utf-8')
}

writeDatamodel(renderedSdl: string): string {
async writeDatamodel(renderedSdl: string): Promise<string> {
const fileName = `datamodel-${Math.round(new Date().getTime() / 1000)}.prisma`
const fullFileName = path.join(this.env.cwd, fileName)
const schemaDir = (await getSchemaDir()) || process.cwd()
const fullFileName = path.join(schemaDir, fileName)
fs.writeFileSync(fullFileName, renderedSdl)
return fileName
}

async introspectDatabase(args: Result<Args>, sdl: boolean | undefined): Promise<IntrospectionResult> {
const credentialsByFlag = this.getCredentialsByFlags(args) || (await getCredentialsFromExistingDatamodel(this.env))
const credentialsByFlag = this.getCredentialsByFlags(args) || (await getCredentialsFromExistingDatamodel())

// Get everything interactively
if (!credentialsByFlag) {
Expand Down
16 changes: 4 additions & 12 deletions cli/introspection/src/introspectionConnector.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Env } from '@prisma/cli'
import { getSchemaPath } from '@prisma/cli'
import { isdlToDatamodel2, getConfig, DataSource } from '@prisma/photon'
import chalk from 'chalk'
import { existsSync, readFileSync } from 'fs'
import { readFileSync } from 'fs'
import { MongoClient } from 'mongodb'
import { Connection, createConnection } from 'mysql'
import { join } from 'path'
import { Client as PGClient } from 'pg'
import { DatabaseType } from 'prisma-datamodel'
import { Connectors, IConnector } from 'prisma-db-introspection'
Expand Down Expand Up @@ -139,15 +138,8 @@ export async function getDatabaseSchemas(connector: IConnector): Promise<string[
}
}

function getSchemaPath(cwd: string): string | undefined {
if (existsSync(join(cwd, 'schema.prisma'))) {
return join(cwd, 'schema.prisma')
}
return undefined
}

export async function getCredentialsFromExistingDatamodel(env: Env): Promise<undefined | DatabaseCredentials> {
const schemaPath = getSchemaPath(env.cwd)
export async function getCredentialsFromExistingDatamodel(): Promise<undefined | DatabaseCredentials> {
const schemaPath = await getSchemaPath()
if (schemaPath) {
const datamodel = readFileSync(schemaPath, 'utf-8')
const { datasources } = await getConfig(datamodel)
Expand Down

0 comments on commit cd628c0

Please sign in to comment.