npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gasket/plugin-metadata

v7.0.6

Published

Adds metadata to gasket lifecycles

Downloads

1,090

Readme

@gasket/plugin-metadata

Metadata is the information about the register plugins and presets, available to plugin lifecycle hooks. This data can be used in various was for plugins, most notably the @gasket/plugin-docs which uses it to collate docs for an app.

Installation

This is a default plugin in the Gasket CLI and is always available for use.

Actions

getMetadata

Get all the metadata for the configured plugins and modules.

const metadata = await gasket.actions.getMetadata();

Lifecycles

metadata

Describe metadata for a plugin and optionally its supporting modules.

Example

This plugin implements the metadata lifecycle, which plugins can use to modify its own metadata at runtime. Whatever is returned will replace the existing metadata.

// gasket-plugin-example.js
export default {
  name: 'example',
  hooks: {
    /**
     * @param {Gasket} gasket - The Gasket API
     * @param {PluginData} data - This plugin's initial metadata
     * @returns {Object}
     */
    async metadata(gasket, data) {
      return {
        ...data,
        // adding extra data to this plugin's metadata
        extra: 'information',
        // add metadata for details of this plugin
        lifecycles: [{
          name: 'some-data',
          description: 'Allows plugins to do something with data',
          method: 'exec',
          parent: 'start'
        }],
        // Metadata for these modules will be loaded
        // Declare as strings or objects with additional data
        modules: [
          'left-pad',
          { name: 'right-pad', extra: 'data', link: 'DOC.md' }
        ]
      }
    },
    /**
     * An example lifecycle hook which utilizes metadata
     *
     * @param {Gasket} gasket - The Gasket API
     */
    async example(gasket) {
      const { metadata } = gasket;

      if (metadata.plugins.find(pluginData => pluginData.name === 'gasket-plugin-something')) {
        // only perform some action if a certain plugin is also registered
      }
    }
  }
}

Usage

Beside the lifecycles available to plugins, metadata can also be described for modules.

Modules

Lastly, modules can describe metadata by defining a gasket.metadata property in the package.json which will get expanded to the ModuleData:

{
  "name": "example",
  "version": "1.2.3",
  "gasket": {
    "metadata": {
      "guides": [
        {
          "name": "Example Guide",
          "description": "How to do something",
          "link": "docs/example.md"
        }
      ]
    }
  }
}

This is especially useful to surface guides with Gasket docs for supporting packages that are intended to be used with Gasket, but are not plugins.

Access

Plugins and apps can read from the metadata object by accessing gasket.metadata in most lifecycles.

Access example

Back to our example plugin, let's see how we can access details about an installed module, and put is some conditional logic.

// gasket-plugin-example.js
import semver from 'semver';

export default {
  name: 'example',
  hooks: {
    // Because metadata is collected during the init lifecycle, we must 
    // adjust our init hook to occur after in order to read the metadata
    init: {
      timing: {
        after: ['@gasket/metadata']
      },
      handler: function initHook(gasket) {
        const { metadata } = gasket;

        // Find the ModuleData for a package
        const moduleData = metadata.modules.find(mod => mod.name === 'some-package');
        
        // If it is installed, and meets requires
        if(moduleData && semver.satisfies(moduleData.version, '13.x')) {
          // Do something special
        } else {
          // Skip and issue warning about upgrading
        }

        // Find the PluginData for a plugin
        const pluginData = metadata.plugins.find(mod => mod.name === 'gasket-plugin-feature');
        if(pluginData) {
          // Fallback for when a certain plugin is not installed
        }
      }
    }
  }
};

How it works

Metadata begins with the info objects from the Loader of [@gasket/resolve] and builds data objects for plugins, and presets, and supporting modules. Any functions preset will be redacted, as metadata is not intended to be executed, but rather to is made available to read and inform plugins. This data can be added to, by hooking the metadata lifecycle in a plugin.

Metadata provides insights to a plugin's shape and package information. Additional detail info of plugins can added in the metadata lifecycle, such as what commands, lifecycles, or structures, a plugin provides. The metadata object be accessed in lifecycle hooks from gasket.metadata.

Additionally, ModuleData for all the top-level app's dependencies are loaded by default, and is available from gasket.metadata.app.modules. Plugins can choose to bring in metadata for more modules, or augment what has already been loaded for the app. These, along with the app's modules, will be flattened and available from gasket.metadata.modules.

License

MIT