Package Events
Sometimes a package needs to perform custom actions when it is downloaded, installed, or uninstalled. To support such custom actions, Pak triggers events that can be hooked at various points of the package management lifecycle.
Event | Description |
---|---|
preinstall | Before installing and after caching package |
preupdate | Before updating cached packages |
preupgrade | Before upgrading installed packages |
install | After installing files locally |
postcache | After saving package to the cache |
postprune | After removing a package from the cache |
postuninstall | After uninstalling package locally |
uninstall | Before uninstalling package locally |
Hooking Events
Events may be hooked by defining event scripts via the scripts pak.json property. Event handlers can be defined inside the scripts collection. For example:
{ "scripts": { "postcache": { "script": "print('Any script in here')" } } }
Literal Scripts
Each event may define a literal script or define a script filename. If the event value is a string, it will be interpreted as a literal script to execute. For example:
{ "scripts": { "postcache": "run('npm install -g babel')" } }
This is quivalent to the following:
{ "scripts": { "postcache": { "script": "run('npm install -g babel')" } } }
Event Script Files
You can run a script file by using the path property inside the event definition.
{
"scripts": {
"postcache": {
"path": "/Users/guest/myscript.sh"
}
}
}
Shell Scripts
You can invoke shell scripts with arguments by using the run property inside a script definition. These run commands will be passed to the shell.
{
"scripts": {
"postcache": {
"run": "myscript.sh arg1 arg2 arg3"
}
}
}
Automatic MakeMe Events
If the package does not define event handlers, but provides a MakeMe file of the same name as the package: NAME.me, then then it will be invoked with the name of the event, prefixed with pak- as a target.
For example:
me -q --file PATH pak-postcache
This can be a convenient way of handling Pak events via MakeMe project files.