You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`ctx.$` is a tagged template literal function that provides a shell-like syntax for executing commands. It's also available as a standalone export `$` from foy.
384
+
381
385
```ts
382
-
import { logger, $ } from'foy'
386
+
import { $ } from'foy'
383
387
384
388
task('build', asyncctx=> {
389
+
// Basic usage - simple command
385
390
await$`tsc`
386
391
await$`node ./test.js`
392
+
393
+
// Using ctx.$ (same as standalone $)
394
+
awaitctx.$`tsc`
395
+
awaitctx.$`node ./test.js`
396
+
397
+
// Variable interpolation - values are automatically escaped
398
+
const file ='my file.txt'// note the space in filename
399
+
await$`cat ${file}`// safely escapes to: cat 'my file.txt'
400
+
401
+
const count =5
402
+
await$`echo ${count}`// outputs: 5
403
+
404
+
// Using with options
405
+
const { stdout } =await$`ls -la`
406
+
console.log(stdout)
407
+
408
+
// Complex example with multiple variables
409
+
const src ='./src'
410
+
const dest ='./dist'
411
+
await$`cp -r ${src} ${dest}`
412
+
})
413
+
```
414
+
415
+
**Key features of `ctx.$`:**
416
+
-**Automatic escaping**: Variables are safely escaped for shell execution
417
+
-**Template literal syntax**: More readable and familiar for shell commands
418
+
-**Returns execa result**: Access `stdout`, `stderr`, `exitCode`, etc.
419
+
-**Default options**: `stdio: 'inherit'` and `shell: true` are set by default
420
+
421
+
**Differences from `ctx.exec`:**
422
+
423
+
```ts
424
+
// ctx.$ - template literal syntax, auto-escaping
425
+
await$`echo ${userInput}`// safely escapes special characters
426
+
427
+
// ctx.exec - string syntax, no auto-escaping
428
+
awaitctx.exec(`echo ${userInput}`) // be careful with special characters!
0 commit comments