diff --git a/package.json b/package.json index f8f8e1317..cf9163a77 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,7 @@ "mkdirp": "^0.5.1", "node-fetch": "^2.0.0", "npm-paths": "^1.0.0", - "npm-run": "^5.0.1", + "npm-run": "4.1.2", "opn": "^5.2.0", "ora": "^1.4.0", "parse-github-url": "^1.0.2", diff --git a/renovate.json b/renovate.json index 61291d59a..b9af016ad 100644 --- a/renovate.json +++ b/renovate.json @@ -2,5 +2,6 @@ "extends": [ "config:base", "docker:disable" - ] + ], + "ignoreDeps": ["npm-run"] } diff --git a/src/cmds/codegen/codegen.ts b/src/cmds/codegen/codegen.ts index 05f77f507..543f6195f 100644 --- a/src/cmds/codegen/codegen.ts +++ b/src/cmds/codegen/codegen.ts @@ -3,7 +3,6 @@ import { GraphQLConfig, GraphQLProjectConfig } from 'graphql-config' import { merge } from 'lodash' import { Arguments } from 'yargs' import { spawnSync } from 'npm-run' -import getApolloCodegenBin from './getApolloCodegenBin' import * as crossSpawn from 'cross-spawn' import { getTmpPath } from '../..' import * as fs from 'fs' @@ -128,8 +127,13 @@ export class Codegen { } if (generator === 'typegen') { + + if (!output.typings || output.typings === '') { + throw new Error('Please provide output.typings path in graphql config to use typegen') + } + inputSchemaPath = inputSchemaPath || '**/*.ts' - const binPath = await getApolloCodegenBin() + const binPath = require.resolve('apollo-codegen').replace('index.js', 'cli.js') const tmpSchemaPath = getTmpPath() fs.writeFileSync( tmpSchemaPath, @@ -137,7 +141,7 @@ export class Codegen { ) const args = [ 'generate', - input, + input || '{binding,prisma}/*.ts', '--schema', tmpSchemaPath, '--output', @@ -145,10 +149,18 @@ export class Codegen { '--target', language, ] + const child = crossSpawn.sync(binPath, args) + if (child.error) { + if (child.error.message === `spawnSync apollo-codegen ENOENT`) { + throw new Error(`Generator apollo-codegen is not installed.`) + } + throw new Error(child.error.message) + } + const stderr = child.stderr && child.stderr.toString() if (stderr && stderr.length > 0) { - console.error(child.stderr.toString()) + throw new Error(child.stderr.toString()) } this.context.spinner.succeed( `Typedefs for project ${this.projectDisplayName()} generated to ${chalk.green( @@ -158,6 +170,11 @@ export class Codegen { // fs.unlinkSync(tmpSchemaPath) } else { const args = ['--input', inputSchemaPath, '--language', language] + + if (!output.binding || output.binding === '' && !output.typeDefs || output.typeDefs === '') { + throw new Error('Please provide either output.binding or output.typeDefs in graphql config to use this generator') + } + if (output.binding) { args.push('--outputBinding', output.binding) } @@ -165,9 +182,19 @@ export class Codegen { args.push('--outputTypedefs', output.typeDefs) } const child = spawnSync(generator, args) + + if (child.error) { + if (child.error.message === `spawnSync ${generator} ENOENT`) { + const prismaVersionMessage = generator === 'prisma-binding' || 'graphql-binding' ? + `Please install ${generator} version > 2.x to use "graphql codegen"` : '' + throw new Error(`Generator ${generator} is not installed. ${prismaVersionMessage}`) + } + throw new Error(child.error.message) + } + const stderr = child.stderr && child.stderr.toString() if (stderr && stderr.length > 0) { - console.error(stderr) + throw new Error(stderr) } this.context.spinner.succeed( diff --git a/src/cmds/codegen/getApolloCodegenBin.ts b/src/cmds/codegen/getApolloCodegenBin.ts deleted file mode 100644 index 82bc9151f..000000000 --- a/src/cmds/codegen/getApolloCodegenBin.ts +++ /dev/null @@ -1,27 +0,0 @@ -import * as path from 'path' -import * as fs from 'fs-extra' -import { getBinPath } from './getBin' - -export default async function getApolloCodegenBin(): Promise { - let binPath = path.join( - __dirname, - '../../node_modules/apollo-codegen/lib/cli.js', - ) - - if (!fs.pathExistsSync(binPath)) { - binPath = path.join( - __dirname, - '../../../../node_modules/apollo-codegen/lib/cli.js', - ) - } - - if (!fs.pathExistsSync(binPath)) { - binPath = path.join(__dirname, '../../../apollo-codegen/lib/cli.js') - } - - if (!fs.pathExistsSync(binPath)) { - binPath = (await getBinPath('apollo-codegen')) || 'apollo-codegen' - } - - return binPath -} diff --git a/src/cmds/codegen/getBin.ts b/src/cmds/codegen/getBin.ts deleted file mode 100644 index c84d3871b..000000000 --- a/src/cmds/codegen/getBin.ts +++ /dev/null @@ -1,29 +0,0 @@ -import * as path from 'path' -import * as fs from 'fs-extra' - -function getPaths(bin: string): string[] { - const envPath = process.env.PATH || '' - const envExt = process.env.PATHEXT || '' - return envPath - .replace(/["]+/g, '') - .split(path.delimiter) - .map(chunk => { - return envExt.split(path.delimiter).map(ext => { - return path.join(chunk, bin + ext) - }) - }) - .reduce((a, b) => { - return a.concat(b) - }) -} - -export async function getBinPath(bin: string): Promise { - const paths = getPaths(bin) - const exist = await Promise.all(paths.map(p => fs.pathExists(p))) - const existsIndex = exist.findIndex(e => e) - if (existsIndex > -1) { - return paths[existsIndex] - } - - return null -}