Skip to content

Commit

Permalink
Add helpful error messages to CLI when trying to use commands outside…
Browse files Browse the repository at this point in the history
… an app (#319)

(patch)
  • Loading branch information
Zeko369 authored May 5, 2020
1 parent 6cd3abe commit f0517e0
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged",
"pre-commit": "yarn lint && pretty-quick --staged",
"pre-push": "yarn test"
}
},
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@
"plugins": [
"@oclif/plugin-help",
"@oclif/plugin-not-found"
]
],
"hooks": {
"prerun": [
"./src/check-before-running"
]
}
},
"keywords": [
"blitz",
Expand Down
37 changes: 37 additions & 0 deletions packages/cli/src/check-before-running.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {Hook} from '@oclif/config'
import chalk from 'chalk'

import {isBlitzRoot, IsBlitzRootError} from './utils/is-blitz-root'

const whitelistGlobal = ['new']

export const hook: Hook<'init'> = async function (options) {
// Bug with oclif
const id = (options as any).Command.id
if (id && whitelistGlobal.includes(id)) return

const {err, message, depth} = await isBlitzRoot()

if (err) {
switch (message) {
case IsBlitzRootError.NotBlitz:
return this.error(
`You are not inside a Blitz project, so this command won't work.\nYou can create a new app with ${chalk.bold(
'blitz new myapp',
)} or see help with ${chalk.bold('blitz help')}`,
)
case IsBlitzRootError.NotRoot:
const help = depth
? `\nUse ${chalk.bold('cd ' + '../'.repeat(depth))} to get to the root of your project`
: ''

return this.error(
`You are currently in a sub-folder of your Blitz app, but this command must be used from the root of your project.${help}`,
)
case IsBlitzRootError.BadPackageJson:
return this.error(`Reading package.json file`)
default:
return this.error(`An error occurred`)
}
}
}
7 changes: 0 additions & 7 deletions packages/cli/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ export class Generate extends Command {
debug('args: ', args)
debug('flags: ', flags)

const isInRoot = fs.existsSync(path.resolve('blitz.config.js'))

if (!isInRoot) {
log.error('No blitz.config.js found. `generate` must be run from the root of the project.')
this.exit(1)
}

try {
let singularRootContext: string

Expand Down
68 changes: 68 additions & 0 deletions packages/cli/src/utils/is-blitz-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {readJSON} from 'fs-extra'
import pkgDir from 'pkg-dir'
import {resolve} from 'path'

export enum IsBlitzRootError {
NotBlitz,
NotRoot,
BadPackageJson,
}

const checkParent = async (): Promise<false | number> => {
const rootDir = await pkgDir('./')

if (rootDir) {
const file = await readJSON(resolve(rootDir, 'package.json'))

if (file && Object.keys(file.dependencies || {}).includes('blitz')) {
return process.cwd().slice(rootDir.length).split('/').length - 1
}
}

return false
}

/**
* @name isBlitzRoot
* @returns {IsBlitzRootError}
* notBlitz -> when can't find package.json in current folder and first found in parent
* doesn't have blitz in dependencies
* notRoot -> if in a nested folder of blitz project (found blitz as depend in a parent package.json)
* badPackageJson -> an error occurred while reading local package.json
*/

export const isBlitzRoot = async (): Promise<{err: boolean; message?: IsBlitzRootError; depth?: number}> => {
try {
const local = await readJSON('./package.json')
if (local) {
if (Object.keys(local.dependencies || {}).includes('blitz')) {
return {err: false}
} else {
return {
err: true,
message: IsBlitzRootError.NotBlitz,
}
}
}
return {err: true, message: IsBlitzRootError.BadPackageJson}
} catch (err) {
// No local package.json
if (err.code === 'ENOENT') {
const out = await checkParent()

if (out === false) {
return {
err: true,
message: IsBlitzRootError.NotBlitz,
}
} else {
return {
err: true,
message: IsBlitzRootError.NotRoot,
depth: out,
}
}
}
}
return {err: true}
}
2 changes: 0 additions & 2 deletions packages/generator/test/generator.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

describe('generator', () => {
it('needs tests', () => {
expect(1).toBe(1)
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"register"
],
"scripts": {
"predev": "wait-on -d 500 ../core/dist/packages/core/src/index.d.ts",
"dev": "tsdx watch --verbose",
"build": "tsdx build",
"test": "tsdx test",
Expand Down

0 comments on commit f0517e0

Please sign in to comment.