Using Husky with Yarn 3
Jump to tl;drWith Yarn 2, you could easily install Husky by setting your package to private and defining a `postinstall` script:
{
"private": true,
"scripts": {
"postinstall": "husky install"
},
"packageManager": "yarn@2.4.3"
}
{
"private": true,
"scripts": {
"postinstall": "husky install"
},
"packageManager": "yarn@2.4.3"
}
However, Yarn 3 doesntt seem to work with postinstall
. This means that you can't use Husky with Yarn 3 without some extra work.
plugin-install-husky
Wanna know what awesome feature Yarn 3 has? A plugin system! This means that you can write your own plugins to extend Yarn's functionality. This is exactly what we're going to do.
If you don't already use plugins, you'll have to create the plugins
directory in your .yarn
folder.
Yarn's 'provided' plugins go to .yarn/plugins/@yarnpkg
, so create a scoped-subfolder in .yarn/plugins
, like .yarn/plugins/@fykowo
.
Now, create your plugin in that folder. I'll call mine plugin-install-husky.cjs
(use .cjs
).
// Creative Commons (c) 2022 Spore, Inc.
const { exec } = require('child_process');
const plugin = {
default: {
hooks: {
afterAllInstalled: () =>
exec('yarn husky install'),
},
},
};
module.exports = {
name: `plugin-install-husky`,
factory: () => plugin,
};
// Creative Commons (c) 2022 Spore, Inc.
const { exec } = require('child_process');
const plugin = {
default: {
hooks: {
afterAllInstalled: () =>
exec('yarn husky install'),
},
},
};
module.exports = {
name: `plugin-install-husky`,
factory: () => plugin,
};
Finally, register your plugin by adding a plugin definition to .yarnrc.yml
:
plugins:
- path: .yarn/plugins/@fykowo/plugin-install-husky.cjs
plugins:
- path: .yarn/plugins/@fykowo/plugin-install-husky.cjs
Now, whenever you run yarn
, Husky will be installed automatically.
tl;dr
- Create your plugin file in
.yarn/plugins/@yourscope/plugin-install-husky.cjs
- >> the content of Code Block #1
- Add plugin to
.yarnrc.yml
(see Code Block #2)
Thanks to Truffle for letting me open-source this plugin!