1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00

Add Pre And Post Hooks For Yarn Scripts as a JavaScript TIL

This commit is contained in:
jbranchaud
2022-08-19 10:38:06 -05:00
parent 2978c9493d
commit b29a9c1b44
2 changed files with 30 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1231 TILs and counting..._
_1232 TILs and counting..._
---
@@ -432,6 +432,7 @@ _1231 TILs and counting..._
- [Object Initialization With Shorthand Property Names](javascript/object-initialization-with-shorthand-property-names.md)
- [Obtain Undefined Value With The Void Operator](javascript/obtain-undefined-value-with-the-void-operator.md)
- [Parse A Date From A Timestamp](javascript/parse-a-date-from-a-timestamp.md)
- [Pre And Post Hooks For Yarn Scripts](javascript/pre-and-post-hooks-for-yarn-scripts.md)
- [Purge Null And Undefined Values From Object](javascript/purge-null-and-undefined-values-from-object.md)
- [Random Cannot Be Seeded](javascript/random-cannot-be-seeded.md)
- [Reach Into An Object For Nested Data With Get](javascript/reach-into-an-object-for-nested-data-with-get.md)

View File

@@ -0,0 +1,28 @@
# Pre And Post Hooks For Yarn Scripts
In [yarn](https://classic.yarnpkg.com/) v1, there is a special syntax for
script names that allows you to define pre and post hooks.
For instance, if you have a `build` script, you can define a `prebuild` and/or
a `postbuild` script as well. Then, anytime you invoke `yarn build`, the
`prebuild` script will be automatically run before `build` and the `postbuild`
script be automatically run after `build`.
This `pre{script}` and `post{script}` syntax works for any script.
```json
{
"scripts": {
"predeploy": "node pre-deploy-steps"
"deploy": "my-framework deploy",
"postdeploy": "node post-deploy-steps"
}
}
```
This syntax may lead to unexpected script invocations. For instance, a
`preserve` script will run ahead of a `serve` script even though those were
probably intended to be unrelated scripts. This is, in part, why this syntax is
no longer support in yarn v2.
[source](https://classic.yarnpkg.com/lang/en/docs/cli/run/#toc-yarn-run-script)