Skip to content

Commit 4b9cfac

Browse files
authored
feat(vscode): prompt middleware route path when activated from command palette (#838)
1 parent 67220e3 commit 4b9cfac

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

extensions/vscode/src/commands/new-middleware.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const cp = require("child_process");
22

3-
import { Uri, window, OpenDialogOptions, ProgressOptions } from "vscode";
3+
import {
4+
Uri,
5+
window,
6+
OpenDialogOptions,
7+
ProgressOptions,
8+
InputBoxOptions,
9+
} from "vscode";
410
import {
511
nearestDartFrogProject,
612
normalizeRoutePath,
@@ -51,7 +57,20 @@ export const newMiddleware = async (uri: Uri | undefined): Promise<void> => {
5157
return;
5258
}
5359

54-
const routePath = normalizeRoutePath(selectedPath, dartFrogProjectPath);
60+
const normalizedRoutePath = normalizeRoutePath(
61+
selectedPath,
62+
dartFrogProjectPath
63+
);
64+
65+
let routePath = normalizedRoutePath;
66+
if (uri === undefined) {
67+
const newRoutePath = await promptRoutePath(`${normalizedRoutePath}`);
68+
if (newRoutePath === undefined || newRoutePath.trim() === "") {
69+
window.showErrorMessage("Please enter a valid route path");
70+
return;
71+
}
72+
routePath = newRoutePath;
73+
}
5574

5675
const options: ProgressOptions = {
5776
location: 15,
@@ -88,6 +107,21 @@ async function promptForTargetDirectory(): Promise<string | undefined> {
88107
});
89108
}
90109

110+
/**
111+
* Shows an input box to the user and returns a Thenable that resolves to
112+
* a string the user provided.
113+
*
114+
* @returns The route name the user provided or undefined if the user canceled.
115+
*/
116+
function promptRoutePath(routePath: string): Thenable<string | undefined> {
117+
const inputBoxOptions: InputBoxOptions = {
118+
prompt: "Middleware's route path",
119+
value: routePath,
120+
placeHolder: "_middleware",
121+
};
122+
return window.showInputBox(inputBoxOptions);
123+
}
124+
91125
/**
92126
* Runs the `dart_frog new middleware` command with the given route path.
93127
*

extensions/vscode/src/test/suite/commands/new-middleware.test.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ suite("new-middleware command", () => {
1919
showErrorMessage: sinon.stub(),
2020
showOpenDialog: sinon.stub(),
2121
withProgress: sinon.stub(),
22+
showInputBox: sinon.stub(),
2223
},
2324
};
2425
childProcessStub = {
@@ -67,8 +68,10 @@ suite("new-middleware command", () => {
6768

6869
test("is not shown when Uri is undefined but resolves a path from workspace", async () => {
6970
utilsStub.resolveDartFrogProjectPathFromWorkspace.returns(
70-
validUri.fsPath
71+
"/home/dart_frog/routes"
7172
);
73+
utilsStub.nearestDartFrogProject.returns("/home/dart_frog/");
74+
utilsStub.normalizeRoutePath.returns("/");
7275

7376
await command.newMiddleware();
7477

@@ -133,6 +136,75 @@ suite("new-middleware command", () => {
133136
}
134137
);
135138

139+
suite("prompts for route path", () => {
140+
test("is shown when Uri is undefined and selected file is valid", async () => {
141+
vscodeStub.window.showInputBox.returns("animals/frog");
142+
utilsStub.resolveDartFrogProjectPathFromWorkspace.returns(
143+
"home/routes/animals/frog"
144+
);
145+
utilsStub.nearestDartFrogProject.returns("home/routes/animals/frog");
146+
utilsStub.normalizeRoutePath.returns("/animals/frog");
147+
148+
await command.newMiddleware();
149+
150+
sinon.assert.calledWith(vscodeStub.window.showInputBox, {
151+
prompt: "Middleware's route path",
152+
value: "/animals/frog",
153+
placeHolder: "_middleware",
154+
});
155+
});
156+
157+
test("is not shown when Uri is defined and selected file is valid", async () => {
158+
utilsStub.nearestDartFrogProject.returns("home/routes/animals/frog");
159+
utilsStub.normalizeRoutePath.returns("/animals/frog");
160+
161+
await command.newMiddleware(validUri);
162+
163+
sinon.assert.neverCalledWith(vscodeStub.window.showInputBox, {
164+
prompt: "Middleware's route path",
165+
value: "/animals/frog/",
166+
placeHolder: "_middleware",
167+
});
168+
});
169+
});
170+
171+
suite("invalid route path error message", () => {
172+
const errorMessage = "Please enter a valid route path";
173+
174+
beforeEach(() => {
175+
vscodeStub.window.showInputBox.returns("animals/frog");
176+
utilsStub.resolveDartFrogProjectPathFromWorkspace.returns(
177+
"home/routes/animals/frog"
178+
);
179+
utilsStub.nearestDartFrogProject.returns("home/routes/animals/frog");
180+
utilsStub.normalizeRoutePath.returns("/animals/frog");
181+
});
182+
183+
test("is shown when prompt is empty", async () => {
184+
vscodeStub.window.showInputBox.returns("");
185+
186+
await command.newMiddleware();
187+
188+
sinon.assert.calledWith(vscodeStub.window.showErrorMessage, errorMessage);
189+
});
190+
191+
test("is shown when prompt is white spaced", async () => {
192+
vscodeStub.window.showInputBox.returns(" ");
193+
194+
await command.newMiddleware();
195+
196+
sinon.assert.calledWith(vscodeStub.window.showErrorMessage, errorMessage);
197+
});
198+
199+
test("is shown when prompt is undefined", async () => {
200+
vscodeStub.window.showInputBox.returns(undefined);
201+
202+
await command.newMiddleware();
203+
204+
sinon.assert.calledWith(vscodeStub.window.showErrorMessage, errorMessage);
205+
});
206+
});
207+
136208
test("shows progess on middleware creation", async () => {
137209
const routePath = "food/pizza";
138210
const selectedUri = {
@@ -220,6 +292,25 @@ suite("new-middleware command", () => {
220292
`dart_frog new middleware 'food/italian'`
221293
);
222294
});
295+
296+
test("successfully with prompt route path", async () => {
297+
utilsStub.resolveDartFrogProjectPathFromWorkspace.returns(
298+
"home/routes/animals/frog"
299+
);
300+
utilsStub.nearestDartFrogProject.returns("home/routes/animals/frog");
301+
utilsStub.normalizeRoutePath.returns("/animals/frog");
302+
vscodeStub.window.showInputBox.returns("animals/lion");
303+
304+
await command.newMiddleware();
305+
const progressFunction =
306+
vscodeStub.window.withProgress.getCall(0).args[1];
307+
await progressFunction();
308+
309+
sinon.assert.calledWith(
310+
childProcessStub.exec,
311+
`dart_frog new middleware 'animals/lion'`
312+
);
313+
});
223314
});
224315

225316
test("shows error message when `dart_frog new middleware` fails", async () => {

0 commit comments

Comments
 (0)