-
-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add debug command to help with setting up the CLI
Closes #136
- Loading branch information
Showing
6 changed files
with
180 additions
and
10 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,59 @@ | ||
import { Arguments, CommandModule } from 'yargs'; | ||
import chalk from 'chalk'; | ||
import { CLIHelper } from './CLIHelper'; | ||
import { pathExists } from 'fs-extra'; | ||
import { Utils } from '../utils'; | ||
|
||
export class DebugCommand implements CommandModule { | ||
|
||
command = 'debug'; | ||
describe = 'Debug CLI configuration'; | ||
|
||
async handler(args: Arguments) { | ||
CLIHelper.dump(`Current ${chalk.cyan('MikroORM')} CLI configuration`); | ||
|
||
if (await pathExists(process.cwd() + '/package.json')) { | ||
CLIHelper.dump(' - package.json ' + chalk.green('found')); | ||
} else { | ||
CLIHelper.dump(' - package.json ' + chalk.red('not found')); | ||
} | ||
|
||
const settings = await CLIHelper.getSettings(); | ||
|
||
if (settings.useTsNode) { | ||
CLIHelper.dump(' - ts-node ' + chalk.green('enabled')); | ||
} | ||
|
||
const configPaths = await CLIHelper.getConfigPaths(); | ||
CLIHelper.dump(' - searched config paths:'); | ||
await DebugCommand.checkPaths(configPaths); | ||
|
||
try { | ||
const config = await CLIHelper.getConfiguration(); | ||
CLIHelper.dump(` - configuration ${chalk.green('found')}`); | ||
const length = config.get('entities', []).length; | ||
|
||
if (length > 0) { | ||
CLIHelper.dump(` - will use \`entities\` array (contains ${length} items)`); | ||
} else if (config.get('entitiesDirs', []).length > 0) { | ||
CLIHelper.dump(' - will use `entitiesDirs` paths:'); | ||
await DebugCommand.checkPaths(config.get('entitiesDirs')); | ||
} | ||
} catch (e) { | ||
CLIHelper.dump(`- configuration ${chalk.red('not found')} ${chalk.red(`(${e.message})`)}`); | ||
} | ||
} | ||
|
||
private static async checkPaths(paths: string[]): Promise<void> { | ||
for (let path of paths) { | ||
path = Utils.normalizePath(path); | ||
|
||
if (await pathExists(path)) { | ||
CLIHelper.dump(` - ${path} (${chalk.green('found')})`); | ||
} else { | ||
CLIHelper.dump(` - ${path} (${chalk.red('not found')})`); | ||
} | ||
} | ||
} | ||
|
||
} |
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,91 @@ | ||
const dump = jest.fn(); | ||
const getSettings = jest.fn(); | ||
const getConfigPaths = jest.fn(); | ||
const getConfiguration = jest.fn(); | ||
jest.mock('../../lib/cli/CLIHelper', () => ({ CLIHelper: { dump, getSettings, getConfigPaths, getConfiguration } })); | ||
|
||
(global as any).process.env.FORCE_COLOR = 0; | ||
(global as any).console.log = jest.fn(); | ||
|
||
import { DebugCommand } from '../../lib/cli/DebugCommand'; | ||
import { Configuration } from '../../lib/utils'; | ||
import { FooBar } from '../entities/FooBar'; | ||
import { FooBaz } from '../entities/FooBaz'; | ||
|
||
describe('DebugCommand', () => { | ||
|
||
test('handler', async () => { | ||
const cmd = new DebugCommand(); | ||
|
||
const pathExistsMock = jest.spyOn(require('fs-extra'), 'pathExists'); | ||
pathExistsMock.mockResolvedValue(true); | ||
getSettings.mockResolvedValue({}); | ||
getConfiguration.mockResolvedValue(new Configuration({} as any, false)); | ||
getConfigPaths.mockReturnValue(['./path/orm-config.ts']); | ||
await expect(cmd.handler({} as any)).resolves.toBeUndefined(); | ||
expect(dump.mock.calls).toEqual([ | ||
['Current MikroORM CLI configuration'], | ||
[' - package.json found'], | ||
[' - searched config paths:'], | ||
[` - ${process.cwd()}/path/orm-config.ts (found)`], | ||
[' - configuration found'], | ||
]); | ||
|
||
getSettings.mockResolvedValue({ useTsNode: true }); | ||
pathExistsMock.mockResolvedValue((path: string) => path.endsWith('entities-1')); | ||
getConfiguration.mockResolvedValue(new Configuration({ entitiesDirs: ['./entities-1', './entities-2'] } as any, false)); | ||
dump.mock.calls.length = 0; | ||
await expect(cmd.handler({} as any)).resolves.toBeUndefined(); | ||
expect(dump.mock.calls).toEqual([ | ||
['Current MikroORM CLI configuration'], | ||
[' - package.json found'], | ||
[' - ts-node enabled'], | ||
[' - searched config paths:'], | ||
[` - ${process.cwd()}/path/orm-config.ts (found)`], | ||
[' - configuration found'], | ||
[' - will use `entitiesDirs` paths:'], | ||
[' - /usr/local/var/www/b4nan/mikro-orm/entities-1 (found)'], | ||
[' - /usr/local/var/www/b4nan/mikro-orm/entities-2 (found)'], | ||
]); | ||
|
||
getConfiguration.mockResolvedValue(new Configuration({ entities: [FooBar, FooBaz] } as any, false)); | ||
dump.mock.calls.length = 0; | ||
await expect(cmd.handler({} as any)).resolves.toBeUndefined(); | ||
expect(dump.mock.calls).toEqual([ | ||
['Current MikroORM CLI configuration'], | ||
[' - package.json found'], | ||
[' - ts-node enabled'], | ||
[' - searched config paths:'], | ||
[` - ${process.cwd()}/path/orm-config.ts (found)`], | ||
[' - configuration found'], | ||
[' - will use `entities` array (contains 2 items)'], | ||
]); | ||
|
||
getConfiguration.mockRejectedValueOnce(new Error('test error message')); | ||
dump.mock.calls.length = 0; | ||
await expect(cmd.handler({} as any)).resolves.toBeUndefined(); | ||
expect(dump.mock.calls).toEqual([ | ||
['Current MikroORM CLI configuration'], | ||
[' - package.json found'], | ||
[' - ts-node enabled'], | ||
[' - searched config paths:'], | ||
[` - ${process.cwd()}/path/orm-config.ts (found)`], | ||
['- configuration not found (test error message)'], | ||
]); | ||
|
||
pathExistsMock.mockResolvedValue(false); | ||
dump.mock.calls.length = 0; | ||
await expect(cmd.handler({} as any)).resolves.toBeUndefined(); | ||
expect(dump.mock.calls).toEqual([ | ||
['Current MikroORM CLI configuration'], | ||
[' - package.json not found'], | ||
[' - ts-node enabled'], | ||
[' - searched config paths:'], | ||
[` - ${process.cwd()}/path/orm-config.ts (not found)`], | ||
[' - configuration found'], | ||
[' - will use `entities` array (contains 2 items)'], | ||
]); | ||
pathExistsMock.mockRestore(); | ||
}); | ||
|
||
}); |