-
-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helpful error messages to CLI when trying to use commands outside…
… an app (#319) (patch)
- Loading branch information
Showing
7 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters