-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proj: refactor routes command (#143)
- Loading branch information
Showing
7 changed files
with
198 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const Command = require("./Command"); | ||
const Router = require("../../core/RouterV2"); | ||
const ProcessHelper = require("../../core/helpers/ProcessHelper"); | ||
|
||
module.exports = class RoutesCommand extends Command { | ||
execute(context /* eslint-disable-line no-unused-vars */) { | ||
const { rootDirectory, projectConfig } = requireArguments(context); | ||
const routes = accumulateRoutes(rootDirectory, projectConfig); | ||
|
||
printRoutes(routes); | ||
} | ||
}; | ||
|
||
/** | ||
* @function requireArguments | ||
* @private | ||
* @description check if all values provided and are valid values | ||
* | ||
* @param {Context} context - the context object given | ||
* | ||
* @throws {Error} | ||
*/ | ||
function requireArguments(context /* eslint-disable-line no-unused-vars */) { | ||
const rootDirectory = ProcessHelper.cwd(); | ||
const projectConfig = ProcessHelper.require(`${rootDirectory}/config`); | ||
|
||
return { | ||
rootDirectory, | ||
projectConfig | ||
}; | ||
} | ||
|
||
/** | ||
* @function accumulateRoutes | ||
* @private | ||
* | ||
* @param {string} rootDirectory - directory the new command was run from | ||
* @param {string} projectConfig - config folder of the project | ||
* | ||
* @throws {Error} | ||
*/ | ||
function accumulateRoutes(rootDirectory, projectConfig) { | ||
const router = new Router(projectConfig, rootDirectory); | ||
const routes = router.load(); | ||
|
||
return routes; | ||
} | ||
|
||
/** | ||
* @function printRoutes | ||
* @private | ||
* | ||
* @param {string} rootDirectory - directory the new command was run from | ||
* @param {string} projectConfig - config folder of the project | ||
* | ||
* @throws {Error} | ||
*/ | ||
function printRoutes(routes) { | ||
const maxURILength = routes.reduce((acc, curr) => { | ||
if (curr.url.length > acc.length) { | ||
acc = curr.url; | ||
} | ||
return acc; | ||
}, "").length; | ||
const maxActionLength = 10; | ||
const VERB_HEADER = "Verb".padEnd(8, " "); | ||
const URI_HEADER = "URI Pattern".padEnd(Math.max(25, maxURILength + 5), " "); | ||
const CA_HEADER = "Controller#Action".padEnd( | ||
Math.max(25, maxActionLength + 5), | ||
" " | ||
); | ||
|
||
console.log(`${VERB_HEADER}${URI_HEADER}${CA_HEADER}`); | ||
|
||
routes.forEach(r => { | ||
const verb = r.method.padEnd(8, " "); | ||
const url = r.url.padEnd(Math.max(25, maxURILength + 5), " "); | ||
const controllerAndAction = `${r.controller}#${r.action}`.padEnd( | ||
Math.max(25, maxActionLength + 5), | ||
" " | ||
); | ||
|
||
console.log(`${verb}${url}${controllerAndAction}`); | ||
}); | ||
} |
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,12 @@ | ||
const InterpreterExpression = require("../InterpreterExpression"); | ||
const RoutesCommand = require("../../Command/Routes"); | ||
|
||
module.exports = class RoutesExpression extends InterpreterExpression { | ||
interpret(context) { | ||
const inputs = context.getInput(); | ||
|
||
if (inputs[0].trim() === "routes") { | ||
context.setOutput(new RoutesCommand()); | ||
} | ||
} | ||
}; |
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 was deleted.
Oops, something went wrong.
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,77 @@ | ||
jest.mock("../../../src/core/helpers/ProcessHelper", () => { | ||
return { | ||
cwd: jest.fn().mockImplementation(() => {}), | ||
exit: jest.fn().mockImplementation(() => {}), | ||
require: jest.fn().mockImplementation(() => {}) | ||
}; | ||
}); | ||
|
||
jest.mock("../../../src/core/RouterV2", () => { | ||
function MockRouter() {} | ||
|
||
MockRouter.prototype.load = jest.fn().mockImplementation(() => {}); | ||
|
||
return MockRouter; | ||
}); | ||
|
||
const Router = require("../../../src/core/RouterV2"); | ||
// const ProcessHelper = require("../../../src/core/helpers/ProcessHelper"); | ||
const InterpreterContext = require("../../../src/cli/Interpreter/InterpreterContext"); | ||
const RoutesExpression = require("../../../src/cli/Interpreter/Expressions/Routes"); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("throws if no config folder found", () => { | ||
try { | ||
interpretAndExecuteCommand(["$", "Boring", "routes"]); | ||
|
||
throw new Error("should throw"); | ||
} catch (ignore) {} | ||
}); | ||
|
||
test("prints empty header to the console if no routes", () => { | ||
console.log = jest.fn(); // eslint-disable-line | ||
Router.prototype.load.mockImplementation(() => []); | ||
|
||
interpretAndExecuteCommand(["$", "Boring", "routes"]); | ||
|
||
expect(Router.prototype.load).toHaveBeenCalled(); | ||
expect(console.log.mock.calls).toHaveLength(1); // eslint-disable-line | ||
}); | ||
|
||
test("prints routes to the console if no routes", () => { | ||
console.log = jest.fn(); // eslint-disable-line | ||
Router.prototype.load.mockImplementation(() => [ | ||
{ | ||
method: "GET", | ||
url: "/", | ||
controller: "Fake", | ||
action: "index" | ||
} | ||
]); | ||
|
||
interpretAndExecuteCommand(["$", "Boring", "routes"]); | ||
|
||
expect(Router.prototype.load).toHaveBeenCalled(); | ||
expect(console.log.mock.calls).toHaveLength(2); // eslint-disable-line | ||
|
||
const loggedRoute = console.log.mock.calls[1][0]; | ||
|
||
expect(loggedRoute.indexOf("GET")).toBeGreaterThan(-1); | ||
expect(loggedRoute.indexOf("/")).toBeGreaterThan(-1); | ||
expect(loggedRoute.indexOf("Fake#index")).toBeGreaterThan(-1); | ||
}); | ||
|
||
function interpretAndExecuteCommand(args) { | ||
const context = new InterpreterContext(args); | ||
const expression = new RoutesExpression(); | ||
|
||
expression.interpret(context); | ||
|
||
const command = context.getOutput(); | ||
|
||
command.execute(context); | ||
} |
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,11 @@ | ||
const RoutesExpression = require("../../../../src/cli/Interpreter/Expressions/Routes"); | ||
const InterpreterContext = require("../../../../src/cli/Interpreter/InterpreterContext"); | ||
|
||
test("if context input is correct, sets the proper command", () => { | ||
const context = new InterpreterContext(["$", "Boring", "routes"]); | ||
const expression = new RoutesExpression(); | ||
|
||
expression.interpret(context); | ||
|
||
expect(context.getOutput().constructor.name).toBe("RoutesCommand"); | ||
}); |