In this video, you followed the confirmation your Small Maps order wrote to the outbox back to the code that writes its words, listed where that code is defined and used, and followed one of its imports outward. Every search below runs in a terminal inside the second-draft-books folder, so you can type along.
Search for the Words in the Outbox
Here’s the confirmation your order wrote to outbox/email-1.json:
{
"id": "email-1",
"to": "[email protected]",
"subject": "Your order order-2",
"text": "Thanks for visiting The Second Draft. Total paid: £6.00."
}
- Some line of code produced those words, so search for them. Don’t search for the whole sentence.
£6.00is your order’s total, and another order shows another amount, so no source file holds that figure as typed. The code builds it for each order. Pick a piece every confirmation shares, such asThanks for visiting. git grepsearches every file Git tracks, meaning the files saved in the repository, not the ones the shop writes while it runs.-nadds line numbers.-Fsearches for exactly the characters you typed.
$ git grep -n -F "Thanks for visiting"
src/email/templates.js:6: text: 'Thanks for visiting The Second Draft.
⮑ Total paid: ' + formatPrice(order.total) + '.'
Each result is a file path, a line number and the matching line. The terminal prints this one on a single line, and the ⮑ marks where it’s wrapped here.
The match sits inside orderMessage(), the function createOrder() calls. Its text is joined from the fixed wording, formatPrice(order.total), and a closing '.':
import { formatPrice } from '../lib/money.js';
export function orderMessage(order) {
return {
subject: 'Your order ' + order.id,
text: 'Thanks for visiting The Second Draft. Total paid: '
+ formatPrice(order.total) + '.'
};
}
Line 6 is one line in the file, wrapped here after its first +.
- The total only exists once there’s an order. The finished sentence is only in files the shop writes as it runs,
outbox/email-1.jsonanddata/shop.json. Git doesn’t track them, sogit grepnever sees them. - Search for the whole sentence, and nothing comes back, though the code is right there in
templates.js. No results is a fact about your search, not the code. Try a shorter piece, or add-ito ignore case, before you decide something isn’t there.
If a search of yours comes back empty, this button asks Code Mentor what to try next. Paste the search you ran:
You’ve now reached orderMessage() from both ends, down from createOrder() in Grace’s notes and up from what the customer reads. Two routes that agree are better evidence than one.
List Where a Function Is Defined and Used
Before Grace’s wording changes, check what else uses orderMessage(). If another file calls it, that file’s message changes too:
$ git grep -n -F "orderMessage"
src/email/templates.js:3:export function orderMessage(order) {
src/orders/create.js:11:import { orderMessage } from '../email/templates.js';
src/orders/create.js:27: queueEmail(state, customer, orderMessage(order));
The line with export function is the definition. The other two are its uses, both in src/orders/create.js. Line 11 imports it, and line 27 calls it. So orderMessage() is called in one place, the order confirmation.
Follow the Import Outward
Line 1 of templates.js imports formatPrice(), the function that formats the total, from money.js in the lib folder. A search for its name lists 11 lines, including Grace’s notes in docs/FIRST-HOUR.md and three lines of the tests in test/pricing.test.js, which import and check it.
For now, you want the shop’s own code. Add -- and then src, the source folder. Whatever follows the two dashes says where to search, so git grep leaves out every file outside src/:
$ git grep -n -F "formatPrice" -- src
src/email/templates.js:1:import { formatPrice } from '../lib/money.js';
src/email/templates.js:6: text: 'Thanks for visiting The Second Draft.
⮑ Total paid: ' + formatPrice(order.total) + '.'
src/email/templates.js:13: return { subject: 'Refund for ' + order.id,
⮑ text: lead + formatPrice(refund.amount) + tail };
src/lib/money.js:9:export function formatPrice(pence) {
src/reports/csv.js:1:export function formatPrice(pence) {
src/reports/csv.js:8: output += row.orderId + ',' + row.date + ','
⮑ + formatPrice(row.paid) + ','
src/reports/csv.js:9: + formatPrice(row.refunded) + ','
⮑ + formatPrice(row.net) + '\n';
To narrow it further, search for the words that start a definition:
$ git grep -n -F "function formatPrice"
src/lib/money.js:9:export function formatPrice(pence) {
src/reports/csv.js:1:export function formatPrice(pence) {
These are two different functions with the same name. A text search can’t tell which one a call reaches. In templates.js, the import line tells you which one the confirmation reaches. An editor’s Go to Definition and Find All References follow that import line for you, so they tell the two apart.
If a search lists more lines than you want to read, this button asks Code Mentor how to narrow it. Add the command you ran and what you were looking for:
Your Map
Your first entry gains three lines:
- Wording: orderMessage() in src/email/templates.js,
found by searching "Thanks for visiting"
- Called once: src/orders/create.js line 27
(imported on line 11)
- Open question: two functions are called
formatPrice(). Which output uses which?
The import lines will say.
When you can see some text, search for the piece that never changes. Then follow the definition, its uses and its imports, one step at a time, and write down what you find.
Next comes Exercise: Track Down the Two Prices, where you’ll answer Grace’s question about why two prices look different, and find every place each one is used.