| 📦 Turbo JS 91999d1 k33g 12h ago | 1 | # Reference: JavaScript tools |
| 2 | |
| 3 | > Neutral description of `.turbo-js/tools.toml`, the JavaScript menu, and what running a command does. |
| 4 | |
| 5 | ## File |
| 6 | |
| 7 | | Property | Value | |
| 8 | | --- | --- | |
| 9 | | Path | `./.turbo-js/tools.toml` | |
| 10 | | Search | The working directory only. Parent directories are **not** searched. | |
| 11 | | Read | Every time one of its menus opens, for the items | |
| 12 | | Re-read | Whenever the file's size or modification time changes, for the **set** of menus | |
| 13 | | Missing file | Not an error | |
| 14 | | Unreadable file | An error, shown in the menu | |
| 15 | | User-level file | **None.** Unlike snippets, there is no `~/.config/turbo-js/tools.toml`. | |
| 16 | |
| 17 | ## File format |
| 18 | |
| 19 | One `[[tool]]` table per command. |
| 20 | |
| 21 | | Key | Type | Required | Description | |
| 22 | | --- | --- | --- | --- | |
| 23 | | `name` | string | yes | What the menu shows. May carry a hot key written with tildes, as in `"~T~est"`. | |
| 24 | | `command` | string | yes | The shell command to run | |
| 25 | | `output` | string | no | Where its output goes: `popup`, `terminal` or `editor`. Absent means `popup`. | |
| 26 | | `menu` | string | no | Which menu it appears in. Absent means `JavaScript`. Any name; the menu is created for you. May carry a hot key written with tildes. | |
| 27 | |
| 28 | `menu` is not checked against a list, because there is no list: a name that no other tool uses simply creates a menu. A tool with no `name`, no `command`, or an `output` naming something that does not exist makes the whole file an error. An unknown `output` is **refused rather than corrected**: `"termnial"` would otherwise look as though it had worked while sending the output somewhere else. |
| 29 | |
| 30 | ### Example |
| 31 | |
| 32 | ```toml |
| 33 | [[tool]] |
| 34 | name = "~T~est" |
| 35 | command = "node --test" |
| 36 | output = "popup" |
| 37 | |
| 38 | [[tool]] |
| 39 | name = "~E~cho" |
| 40 | command = "echo TADA" |
| 41 | output = "terminal" |
| 42 | menu = "Tools" |
| 43 | ``` |
| 44 | |
| 45 | ## The starter file |
| 46 | |
| 47 | **JavaScript ▸ Create tools file** writes these seven, in this order: |
| 48 | |
| 49 | | Name | Command | Output | Menu | |
| 50 | | --- | --- | --- | --- | |
| 51 | | `~I~nstall` | `npm install` | `popup` | JavaScript | |
| 52 | | `~F~ormat` | `npx prettier --write .` | `popup` | JavaScript | |
| 53 | | `~L~int` | `npx eslint .` | `popup` | JavaScript | |
| 54 | | `~T~est` | `node --test` | `popup` | JavaScript | |
| 55 | | `~R~un` | `node {{script, e.g. main.js}}` | `terminal` | JavaScript | |
| 56 | | `~S~tart` | `npm start` | `terminal` | JavaScript | |
| 57 | | `~E~cho` | `echo 🎉 tada!` | `terminal` | Tools | |
| 58 | |
| 59 | `Install` comes first because it is the first thing run in a fresh clone, and the one that has to have run before any of the others can. One of them asks for a value before it runs — `Run`, because a Node project has no single entry point the editor could know — and one names a `menu` of its own. Those two features are invisible unless the starter file shows them. |
| 60 | |
| 61 | `Run` and `Start` get a terminal: a script may read the keyboard, and `npm start` is usually a server that has to be interrupted with `Ctrl-C`. `Format` and `Lint` go through `npx`, which runs the project's own Prettier and ESLint when `package.json` depends on them and downloads them into a cache otherwise; ESLint 9 needs an `eslint.config.js` in the project. `Test` is Node's own runner, which needs nothing installed: it runs every `*.test.js`, `*.spec.js` and `test/**/*.js` file under the current directory. |
| 62 | |
| 63 | There is no `Build`, because JavaScript has no compile step. `node --check {{script}}` is the syntax check somebody who wants one adds. |
| 64 | |
| 65 | Every tool names its `output`, including the ones that name the default: the key is the interesting part of the format, and a file where it appears once is a file where nobody notices it exists. |
| 66 | |
| 67 | The item is greyed out once the project has a tools file, so it cannot overwrite one. The file is written through a temporary file in the same directory, renamed into place. |
| 68 | |
| 69 | ## The JavaScript menu |
| 70 | |
| 71 | Always on the bar, whether or not a tools file exists. Its hot key is `Alt-J`. |
| 72 | |
| 73 | | Item | Condition | |
| 74 | | --- | --- | |
| 75 | | One line per tool with no `menu`, in file order | The file holds at least one | |
| 76 | | `Cannot read tools`, greyed out | The file is present but unreadable | |
| 77 | | `Create tools file` | The project has no tools file | |
| 78 | | `Open tools file` | The project has one | |
| 79 | |
| 80 | ## Menus a tool asks for |
| 81 | |
| 82 | A `menu` naming anything other than `JavaScript` puts a menu of that name on the bar. |
| 83 | |
| 84 | | Property | Value | |
| 85 | | --- | --- | |
| 86 | | Position | Between JavaScript and Help | |
| 87 | | Order | The order each name first appears in the file | |
| 88 | | Items | One line per tool naming that menu, in file order. Nothing else — `Create tools file` and `Open tools file` stay in JavaScript. | |
| 89 | | Unreadable file | No menus at all; the JavaScript menu carries the error | |
| 90 | | While the editor runs | Added, removed and renamed as the file changes, without restarting | |
| 91 | |
| 92 | ### Hot keys |
| 93 | |
| 94 | Assigned automatically, because a name from a file cannot be checked against the fixed menus in advance. |
| 95 | |
| 96 | | Case | Result | |
| 97 | | --- | --- | |
| 98 | | No tildes in the name | The first letter no other menu has claimed is marked. `Tools` becomes `~T~ools`: `T` is free. `Format` becomes `For~m~at`: `F` is File's, `o` is Options', `r` is Run's. | |
| 99 | | Tildes naming a free letter | Kept as written. `Doc~k~er` answers to `Alt-K`. | |
| 100 | | Tildes naming a taken letter | Dropped, and a free letter chosen instead. `~F~oo` becomes `F~o~o`. | |
| 101 | | Every letter taken | No hot key. `F10` and the mouse still open it. | |
| 102 | |
| 103 | The letters the editor's own menus hold are `F`, `E`, `S`, `R`, `C`, `O`, `W`, `N` (Snippets), `A` (Agent), `J` (JavaScript) and `H`. |
| 104 | |
| 105 | ## Running a command |
| 106 | |
| 107 | Common to every output: |
| 108 | |
| 109 | | Property | Value | |
| 110 | | --- | --- | |
| 111 | | Shell | `/bin/sh -c "<command>"` on Linux and macOS; `cmd.exe /S /C "<command>"` — the shell `%COMSPEC%` names — on Windows | |
| 112 | | Directory | The directory the editor was started in | |
| 113 | | Standard error | Merged into standard output, in the order the command wrote them | |
| 114 | |
| 115 | Going through a shell means pipes, globs, `&&` and `;` all work, so one tool can be a sequence. On Windows the shell is cmd.exe, which knows `&&`, `|` and `>` but does not expand globs, and where `;` is not a separator. |
| 116 | |
| 117 | ### `output = "popup"` |
| 118 | |
| 119 | | Property | Value | |
| 120 | | --- | --- | |
| 121 | | Opens | Immediately, before the command has finished | |
| 122 | | Modal | Yes: nothing else in the editor can be used while it is up | |
| 123 | | Fills in | As output arrives, following it until you scroll back | |
| 124 | | Title while running | `<command> — running` | |
| 125 | | Title when finished | `<command> — ok`, or `<command> — exit <n>` | |
| 126 | | Empty output, finished | Shows `(no output)` | |
| 127 | | Empty output, running | Shows nothing | |
| 128 | | Output cap | 10000 lines; past it the oldest go and a `… n earlier lines dropped …` line says so | |
| 129 | |
| 130 | | Key | Effect | |
| 131 | | --- | --- | |
| 132 | | `↑` `↓` `PgUp` `PgDn` `Home` `End` | Read through the output | |
| 133 | | Wheel | The same | |
| 134 | | `Escape`, `Enter`, **Close** | Close it, **stopping the command** if it is still running | |
| 135 | |
| 136 | Closing stops the command because there is no other way to interrupt one whose output is not in a terminal. |
| 137 | |
| 138 | ### `output = "terminal"` |
| 139 | |
| 140 | | Property | Value | |
| 141 | | --- | --- | |
| 142 | | Window | A terminal window of its own, titled with the command | |
| 143 | | Environment | The editor's own, with `TERM` set to `xterm-256color` | |
| 144 | | After it exits | The window stays, showing its output | |
| 145 | | Modal | No: the editor carries on beside it | |
| 146 | |
| 147 | Because it is a real terminal, colours, paging, `Ctrl-C` and reading from the keyboard all work — `node --test`'s green ticks, a script reading `process.stdin`, a server logging until it is interrupted. See [Terminal windows](terminal.md). |
| 148 | |
| 149 | Keys in a **finished** terminal window: |
| 150 | |
| 151 | | Key | Effect | |
| 152 | | --- | --- | |
| 153 | | `Shift-PgUp`, `Shift-PgDn` | Read back through the output | |
| 154 | | `Ctrl-W` | Close the window | |
| 155 | | Anything else | Reaches the editor, not the dead shell | |
| 156 | |
| 157 | ### `output = "editor"` |
| 158 | |
| 159 | | Property | Value | |
| 160 | | --- | --- | |
| 161 | | Shows | A popup while it runs, as above | |
| 162 | | On closing the popup | An editing window holding the output, titled with the command | |
| 163 | | Filled | Once, when the command has finished — not as it goes | |
| 164 | | The window | An ordinary editing window with no file name: searchable with `Ctrl-F`, and `Save as` keeps it | |
| 165 | |
| 166 | ## Reloading after a command |
| 167 | |
| 168 | When a command finishes, every open file is considered. |
| 169 | |
| 170 | | The file | What happens | |
| 171 | | --- | --- | |
| 172 | | Unmodified, and changed on disk | Re-read; its syntax is re-decided and its title refreshed | |
| 173 | | Unmodified, and unchanged on disk | Left alone, not counted | |
| 174 | | Has unsaved changes | Left alone and counted as skipped | |
| 175 | | Has never been named | Left alone | |
| 176 | | Has gone from disk | Left alone | |
| 177 | |
| 178 | The cursor stays where it was, clamped into whatever the file now holds. The undo history is discarded, because undoing back past a reload would restore text the file no longer has. |
| 179 | |
| 180 | The project tree is refreshed at the same moment — which is how `node_modules/` and `package-lock.json`, written by `npm install`, appear in it. |
| 181 | |
| 182 | | Status bar | When | |
| 183 | | --- | --- | |
| 184 | | `Running <command>` | The window opens | |
| 185 | | `Reloaded 2 files` | Two files were re-read, none skipped | |
| 186 | | `Reloaded 2 files; 1 file with unsaved changes left alone` | Some were skipped | |
| 187 | | `Command finished; 1 file with unsaved changes left alone` | Nothing was re-read, something was skipped | |
| 188 | |
| 189 | ## Errors |
| 190 | |
| 191 | | Message | Cause | |
| 192 | | --- | --- | |
| 193 | | `Cannot read tools` in the menu | The file is present but not valid TOML, or holds a tool with no name or no command | |
| 194 | | `Already there: .turbo-js/tools.toml` | Creating in a project that already has one. Unreachable from the menu, which greys the item out; still possible for a caller that is not a menu. | |
| 195 | | `This project has no .turbo-js/tools.toml yet.` | Opening in a project that has none, likewise | |
| 196 | | `Cannot tell which directory this is: …` | The working directory could not be read | |
| 197 | | `Terminal windows are not supported on this platform yet` | Running a command in a terminal needs a pseudo-terminal, which Linux, macOS and Windows have; see [Terminal windows](terminal.md) | |
| 198 | |
| 199 | ## Asking for a value |
| 200 | |
| 201 | A `{{label}}` anywhere in a command is a value the editor asks for before it runs, in a box titled after the tool. The text between the braces is what the box asks for. |
| 202 | |
| 203 | | Written | Asked for | Substituted | |
| 204 | | --- | --- | --- | |
| 205 | | `{{script, e.g. main.js}}` | `script, e.g. main.js` | shell-quoted | |
| 206 | | `{{arguments...}}` | `arguments` | verbatim | |
| 207 | |
| 208 | A value is **shell-quoted** by default, so a path with a space in it stays one argument. A trailing `...` inside the braces asks for it verbatim instead, which is how one field can stand for several arguments. |
| 209 | |
| 210 | ```toml |
| 211 | [[tool]] |
| 212 | name = "Run with ~a~rguments" |
| 213 | command = "node main.js {{arguments...}}" |
| 214 | output = "terminal" |
| 215 | ``` |
| 216 | |
| 217 | | Rule | Behaviour | |
| 218 | | --- | --- | |
| 219 | | Several placeholders | One box, one field each, in the order they appear in the command | |
| 220 | | The same label twice | One field; every occurrence gets what is typed into it | |
| 221 | | A label written both ways | Asked for once; each occurrence honours its own braces | |
| 222 | | Escape, or Cancel | The command does not run | |
| 223 | | A field left empty | Substituted as empty — the command reports its own complaint | |
| 224 | | Running the tool again | The box starts from what was typed last time, for this session only | |
| 225 | | More fields than fit on screen | Refused, with a message saying how many fit | |
| 226 | |
| 227 | **Double braces, not single.** `awk '{print $1}'` and `find . -exec rm {} +` are ordinary commands, and a single-brace syntax would read the first as a request for a value called `print $1`. |
| 228 | |
| 229 | Nothing is written to disk. A value somebody typed this afternoon is not a decision the project made, so it does not go in the project's own directory. |
| 230 | |
| 231 | ### Errors |
| 232 | |
| 233 | | Error | Cause | |
| 234 | | --- | --- | |
| 235 | | `tool "X": "{{script" is never closed` | An opening `{{` with no `}}` after it | |
| 236 | | `tool "X": {{}} asks for a value but does not say what it is` | A placeholder with no label, or one that is only `...` | |
| 237 | |
| 238 | Both are refused when the file is read, so a half-typed placeholder never reaches the shell with its braces still in it. |
| 239 | |
| 240 | ## See also |
| 241 | |
| 242 | - [How to run Node commands from the editor](../how-to/run-node-commands.md) |
| 243 | - [JavaScript tools](../explanation/javascript-tools.md) |
| 244 | - [Terminal windows](terminal.md) |