Skip to content

Commit b29a9c1

Browse files
committed
Add Pre And Post Hooks For Yarn Scripts as a JavaScript TIL
1 parent 2978c94 commit b29a9c1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1231 TILs and counting..._
13+
_1232 TILs and counting..._
1414

1515
---
1616

@@ -432,6 +432,7 @@ _1231 TILs and counting..._
432432
- [Object Initialization With Shorthand Property Names](javascript/object-initialization-with-shorthand-property-names.md)
433433
- [Obtain Undefined Value With The Void Operator](javascript/obtain-undefined-value-with-the-void-operator.md)
434434
- [Parse A Date From A Timestamp](javascript/parse-a-date-from-a-timestamp.md)
435+
- [Pre And Post Hooks For Yarn Scripts](javascript/pre-and-post-hooks-for-yarn-scripts.md)
435436
- [Purge Null And Undefined Values From Object](javascript/purge-null-and-undefined-values-from-object.md)
436437
- [Random Cannot Be Seeded](javascript/random-cannot-be-seeded.md)
437438
- [Reach Into An Object For Nested Data With Get](javascript/reach-into-an-object-for-nested-data-with-get.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Pre And Post Hooks For Yarn Scripts
2+
3+
In [yarn](https://classic.yarnpkg.com/) v1, there is a special syntax for
4+
script names that allows you to define pre and post hooks.
5+
6+
For instance, if you have a `build` script, you can define a `prebuild` and/or
7+
a `postbuild` script as well. Then, anytime you invoke `yarn build`, the
8+
`prebuild` script will be automatically run before `build` and the `postbuild`
9+
script be automatically run after `build`.
10+
11+
This `pre{script}` and `post{script}` syntax works for any script.
12+
13+
```json
14+
{
15+
"scripts": {
16+
"predeploy": "node pre-deploy-steps"
17+
"deploy": "my-framework deploy",
18+
"postdeploy": "node post-deploy-steps"
19+
}
20+
}
21+
```
22+
23+
This syntax may lead to unexpected script invocations. For instance, a
24+
`preserve` script will run ahead of a `serve` script even though those were
25+
probably intended to be unrelated scripts. This is, in part, why this syntax is
26+
no longer support in yarn v2.
27+
28+
[source](https://classic.yarnpkg.com/lang/en/docs/cli/run/#toc-yarn-run-script)

0 commit comments

Comments
 (0)