Skip to content

Commit

Permalink
fix(client): add yarn workspaces support
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Apr 21, 2020
1 parent 8d00f9f commit 902d438
Showing 1 changed file with 97 additions and 14 deletions.
111 changes: 97 additions & 14 deletions src/packages/sdk/src/cli/getSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promisify } from 'util'
import fs from 'fs'
import path from 'path'
import execa from 'execa'

const exists = promisify(fs.exists)
const readFile = promisify(fs.readFile)
Expand All @@ -9,17 +10,23 @@ const readFile = promisify(fs.readFile)
* Async
*/

export async function getSchemaPath(schemaPathFromArgs?): Promise<string | null> {
if (schemaPathFromArgs) {
export async function getSchemaPath(
schemaPathFromArgs?,
): Promise<string | null> {
if (schemaPathFromArgs) {
// try the user custom path
const customSchemaPath = await getAbsoluteSchemaPath(path.resolve(schemaPathFromArgs))
const customSchemaPath = await getAbsoluteSchemaPath(
path.resolve(schemaPathFromArgs),
)
if (customSchemaPath) {
return customSchemaPath
} else {
throw new Error(`Provided --schema at ${schemaPathFromArgs} doesn't exist.`)
throw new Error(
`Provided --schema at ${schemaPathFromArgs} doesn't exist.`,
)
}
}

// try the normal cwd
const relativeSchemaPath = await getRelativeSchemaPath(process.cwd())
if (relativeSchemaPath) {
Expand All @@ -28,13 +35,74 @@ export async function getSchemaPath(schemaPathFromArgs?): Promise<string | null>

// in case no schema can't be found there, try the npm-based INIT_CWD
if (process.env.INIT_CWD) {
return getRelativeSchemaPath(process.env.INIT_CWD)
return (
(await getRelativeSchemaPath(process.env.INIT_CWD)) ||
(await resolveYarnSchema())
)
}

return null
}

async function getAbsoluteSchemaPath(schemaPath: string): Promise<string | null> {
async function resolveYarnSchema(): Promise<null | string> {
if (process.env.npm_config_user_agent?.includes('yarn')) {
try {
const { stdout: version } = await execa.command('yarn --version')

if (version.startsWith('2')) {
return null
}

const { stdout } = await execa.command('yarn workspaces info --json')
const json = getJson(stdout)
for (const workspace of Object.values<any>(json)) {
const workspacePath = path.join(
process.env.INIT_CWD!,
workspace.location,
)
const workspaceSchemaPath = await getRelativeSchemaPath(workspacePath)
if (workspaceSchemaPath) {
return workspaceSchemaPath
}
}
} catch (e) {
return null
}
}
return null
}

function resolveYarnSchemaSync(): string | null {
if (process.env.npm_config_user_agent?.includes('yarn')) {
try {
const { stdout: version } = execa.commandSync('yarn --version')

if (version.startsWith('2')) {
return null
}

const { stdout } = execa.commandSync('yarn workspaces info --json')
const json = getJson(stdout)
for (const workspace of Object.values<any>(json)) {
const workspacePath = path.join(
process.env.INIT_CWD!,
workspace.location,
)
const workspaceSchemaPath = getRelativeSchemaPathSync(workspacePath)
if (workspaceSchemaPath) {
return workspaceSchemaPath
}
}
} catch (e) {
return null
}
}
return null
}

async function getAbsoluteSchemaPath(
schemaPath: string,
): Promise<string | null> {
if (await exists(schemaPath)) {
return schemaPath
}
Expand All @@ -60,7 +128,9 @@ async function getRelativeSchemaPath(cwd: string): Promise<string | null> {
/**
* Small helper that returns the directory which contains the `schema.prisma` file
*/
export async function getSchemaDir(schemaPathFromArgs?): Promise<string | null> {
export async function getSchemaDir(
schemaPathFromArgs?,
): Promise<string | null> {
if (schemaPathFromArgs) {
return path.resolve(path.dirname(schemaPathFromArgs))
}
Expand Down Expand Up @@ -88,16 +158,20 @@ export async function getSchema(schemaPathFromArgs?): Promise<string> {
*/

export function getSchemaPathSync(schemaPathFromArgs?): string | null {
if (schemaPathFromArgs) {
if (schemaPathFromArgs) {
// try the user custom path
const customSchemaPath = getAbsoluteSchemaPathSync(path.resolve(schemaPathFromArgs))
const customSchemaPath = getAbsoluteSchemaPathSync(
path.resolve(schemaPathFromArgs),
)
if (customSchemaPath) {
return customSchemaPath
} else {
throw new Error(`Provided --schema at ${schemaPathFromArgs} doesn't exist.`)
throw new Error(
`Provided --schema at ${schemaPathFromArgs} doesn't exist.`,
)
}
}

// first try intuitive schema path
const relativeSchemaPath = getRelativeSchemaPathSync(process.cwd())

Expand All @@ -107,7 +181,9 @@ export function getSchemaPathSync(schemaPathFromArgs?): string | null {

// in case the normal schema path doesn't exist, try the npm base dir
if (process.env.INIT_CWD) {
return getRelativeSchemaPathSync(process.env.INIT_CWD)
return (
getRelativeSchemaPathSync(process.env.INIT_CWD) || resolveYarnSchemaSync()
)
}

return null
Expand Down Expand Up @@ -161,4 +237,11 @@ export function getSchemaSync(schemaPathFromArgs?): string {
}

return fs.readFileSync(schemaPath, 'utf-8')
}
}

function getJson(stdout: string): any {
const firstCurly = stdout.indexOf('{')
const lastCurly = stdout.lastIndexOf('}')
const sliced = stdout.slice(firstCurly, lastCurly + 1)
return JSON.parse(sliced)
}

0 comments on commit 902d438

Please sign in to comment.