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.

EventDescription
preinstallBefore installing and after caching package
preupdateBefore updating cached packages
preupgradeBefore upgrading installed packages
installAfter installing files locally
postcacheAfter saving package to the cache
postpruneAfter removing a package from the cache
postuninstallAfter uninstalling package locally
uninstallBefore 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.

© Embedthis Software. All rights reserved.