-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract CLI repl into its own package (#520)
Co-authored-by: Brandon Bayer <b@bayer.ws> (meta)
- Loading branch information
Showing
18 changed files
with
339 additions
and
140 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
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,9 @@ | ||
import {REGISTER_INSTANCE} from 'ts-node' | ||
|
||
export const setupTsnode = () => { | ||
if (!process[REGISTER_INSTANCE]) { | ||
// During blitz interal dev, oclif automaticaly sets up ts-node so we have to check | ||
require('ts-node').register({compilerOptions: {module: 'commonjs'}}) | ||
} | ||
require('tsconfig-paths/register') | ||
} |
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,46 +1,72 @@ | ||
import * as repl from 'repl' | ||
import * as chokidar from 'chokidar' | ||
import {Console} from '../../src/commands/console' | ||
import {getBlitzModulePaths} from '../../src/utils/load-blitz' | ||
import {REPLServer} from 'repl' | ||
import {FSWatcher} from 'chokidar' | ||
|
||
const mockRepl = ({ | ||
defineCommand: jest.fn(), | ||
on: jest.fn(), | ||
context: {}, | ||
} as any) as REPLServer | ||
const mockWatcher = ({ | ||
on: jest.fn(), | ||
} as any) as FSWatcher | ||
|
||
jest.mock('repl') | ||
jest.mock('chokidar') | ||
|
||
import * as repl from '@blitzjs/repl' | ||
import * as db from '../../src/commands/db' | ||
|
||
jest.spyOn(global.console, 'log').mockImplementation() | ||
|
||
jest.mock( | ||
'@blitzjs/server', | ||
jest.fn(() => { | ||
return { | ||
log: { | ||
branded: jest.fn(), | ||
spinner: () => { | ||
return { | ||
start: jest.fn().mockImplementation(() => ({succeed: jest.fn()})), | ||
} | ||
}, | ||
}, | ||
} | ||
}), | ||
) | ||
|
||
jest.mock(`${process.cwd()}/package.json`, () => ({ | ||
dependencies: { | ||
ramda: '1.0.0', | ||
}, | ||
})) | ||
jest.mock('@blitzjs/generator/src/utils/load-dependencies') | ||
jest.mock('../../src/utils/load-blitz') | ||
|
||
jest.mock( | ||
'@blitzjs/repl', | ||
jest.fn(() => { | ||
return { | ||
runRepl: jest.fn(), | ||
} | ||
}), | ||
) | ||
|
||
jest.mock( | ||
'../../src/commands/db', | ||
jest.fn(() => { | ||
return { | ||
runPrismaGeneration: jest.fn(), | ||
} | ||
}), | ||
) | ||
|
||
describe('Console command', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('runs REPL', async () => { | ||
jest.spyOn(Console.prototype, 'log') | ||
jest.spyOn(repl, 'start').mockReturnValue(mockRepl) | ||
jest.spyOn(chokidar, 'watch').mockReturnValue(mockWatcher) | ||
jest.spyOn(mockRepl, 'on').mockReturnValue(mockRepl) | ||
it('runs PrismaGeneration', async () => { | ||
await Console.prototype.run() | ||
expect(db.runPrismaGeneration).toHaveBeenCalled() | ||
}) | ||
|
||
it('runs PrismaGeneration with silent allowed', async () => { | ||
await Console.prototype.run() | ||
expect(db.runPrismaGeneration).toHaveBeenCalledWith({silent: true}) | ||
}) | ||
|
||
expect(repl.start).toBeCalledWith(Console.replOptions) | ||
expect(mockRepl.defineCommand).toBeCalledWith('reload', Console.commands.reload) | ||
it('runs repl', async () => { | ||
await Console.prototype.run() | ||
expect(repl.runRepl).toHaveBeenCalled() | ||
}) | ||
|
||
// expect(chokidar.watch).toBeCalledWith('package.json') | ||
expect(chokidar.watch).toBeCalledWith(getBlitzModulePaths()) | ||
it('runs repl with replOptions', async () => { | ||
await Console.prototype.run() | ||
expect(repl.runRepl).toHaveBeenCalledWith(Console.replOptions) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,21 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": [ | ||
"src", | ||
"test" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
], | ||
"include": ["src", "test"], | ||
"exclude": ["node_modules"], | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"declarationDir": "./dist", | ||
"downlevelIteration": true, | ||
"paths": { | ||
"*": [ | ||
"src/*", | ||
"node_modules/*" | ||
] | ||
"*": ["src/*", "node_modules/*"] | ||
} | ||
} | ||
} | ||
} |
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,5 @@ | ||
# `console` | ||
|
||
This package houses all files related to Blitz console. | ||
|
||
In the main `src` directory you'll find the base `console` file that connects and manage the REPL. |
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,24 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
moduleFileExtensions: ['ts', 'js', 'json'], | ||
coverageReporters: ['json', 'lcov', 'text', 'clover'], | ||
// collectCoverage: !!`Boolean(process.env.CI)`, | ||
collectCoverageFrom: ['src/**/*.ts'], | ||
modulePathIgnorePatterns: ['<rootDie>/tmp', '<rootDir>/lib'], | ||
// TODO enable threshold | ||
// coverageThreshold: { | ||
// global: { | ||
// branches: 100, | ||
// functions: 100, | ||
// lines: 100, | ||
// statements: 100, | ||
// }, | ||
// }, | ||
|
||
globals: { | ||
'ts-jest': { | ||
tsConfig: 'test/tsconfig.json', | ||
}, | ||
}, | ||
} |
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,44 @@ | ||
{ | ||
"name": "@blitzjs/repl", | ||
"version": "0.11.0", | ||
"description": "Repl package for Blitz CLI", | ||
"homepage": "https://github.com/blitz-js/blitz/packages/repl/#readme", | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rimraf dist", | ||
"dev": "tsdx watch --verbose", | ||
"build": "tsdx build", | ||
"lint": "tsdx lint", | ||
"test": "tsdx test", | ||
"test:watch": "tsdx test --watch" | ||
}, | ||
"author": { | ||
"name": "Brandon Bayer", | ||
"email": "b@bayer.ws", | ||
"url": "https://twitter.com/flybayer" | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/repl.esm.js", | ||
"types": "dist/packages/repl/src/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
"blitz", | ||
"repl" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/blitz-js/blitz.git" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"dependencies": { | ||
"pkg-dir": "4.2.0", | ||
"chokidar": "3.3.1", | ||
"globby": "11.0.0" | ||
} | ||
} |
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 @@ | ||
export * from './repl' |
Oops, something went wrong.