@netlify/cache-utils
Utility for caching files in Netlify Build
Last updated 18 days ago by ehmicky .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @netlify/cache-utils 
SYNC missed versions from official npm registry.

Coverage Status Build

Utility for caching files in Netlify Build.

Examples

Simple

module.exports = {
  // Restore file/directory cached in previous builds.
  // Does not do anything if:
  //  - the file/directory already exists locally
  //  - the file/directory has not been cached yet
  async onPreBuild({ utils }) {
    await utils.cache.restore('./path/to/file')
  }
  // Cache file/directory for future builds.
  // Does not do anything if:
  //  - the file/directory does not exist locally
  async onPostBuild({ utils }) {
    await utils.cache.save('./path/to/file')
  }
}

Multiple directories

// Restore/cache several files/directories
module.exports = {
  async onPreBuild({ utils }) {
    await utils.cache.restore(['./path/to/file', './path/to/other'])
  }
  async onPostBuild({ utils }) {
    await utils.cache.save(['./path/to/file', './path/to/other'])
  }
}

API

save(path, options?)

path: string
options: object
Returns: Promise<Boolean>

Cache a file/directory.

Skipped if the file/directory does not exist locally.

Returns false if the file/directory does not exist. Returns true otherwise.

options

ttl

Type: number (in seconds)
Default: undefined

Only cache the file/directory for a specific amount of time.

// Only cache the following file/directory for 1 hour
module.exports = {
  async onPreBuild({ utils }) {
    await utils.cache.restore('./path/to/file')
  }
  async onPostBuild({ utils }) {
    await utils.cache.save('./path/to/file', { ttl: 3600 })
  }
}

digests

Type: string[]
Default: []

Paths to lock files or manifest files that can be used to check if the directory to cache has changed. Using this option speeds up caching.

// If that directory has a lockfile or a manifest file, use it to check if its
// contents has changed. This will speed up cache saving.
// For example, `package-lock.json` and `yarn.lock` are digest files for the
// `node_modules` directory.
module.exports = {
  async onPreBuild({ utils }) {
    await utils.cache.restore('node_modules')
  }
  async onPostBuild({ utils }) {
    await utils.cache.save('node_modules', {
      digests: ['package-lock.json', 'yarn.lock']
    })
  }
}

cwd

Type: string
Default: process.cwd()

Current directory used to resolve relative paths.

restore(path, options?)

path: string
options: object
Returns: Promise<Boolean>

Restore a file/directory previously cached. Skipped if the file/directory already exists locally or if it has not been cached yet.

Please be careful: this overwrites the local file/directory if any exists.

Returns false if the file/directory was not cached yet. Returns true otherwise.

options

cwd

Type: string
Default: process.cwd()

Current directory used to resolve relative paths.

remove(path, options?)

path: string
Returns: Promise<Boolean>

Remove a file/directory from the cache. Useful for cache invalidation.

Returns false if the file/directory was not cached yet. Returns true otherwise.

module.exports = {
  async onPostBuild({ utils }) {
    await utils.cache.remove('./path/to/file')
  },
}

options

cwd

Type: string
Default: process.cwd()

Current directory used to resolve relative paths.

has(path, options?)

path: string
Returns: Promise<Boolean>

Returns whether a file/directory is currently cached.

// Conditional logic can be applied depending on whether the file has been
// previously cached or not
const path = './path/to/file'

module.exports = {
  async onPreBuild({ utils }) {
    if (!(await utils.cache.has(path))) {
      console.log(`File ${path} not cached`)
      return
    }

    console.log(`About to restore cached file ${path}...`)
    if (await utils.cache.restore('./path/to/file')) {
      console.log(`Restored cached file ${path}`)
    }
  },
  async onPostBuild({ utils }) {
    if (await utils.cache.save('./path/to/file')) {
      console.log(`Saved cached file ${path}`)
    }
  },
}

options

cwd

Type: string
Default: process.cwd()

Current directory used to resolve relative paths.

list(options?)

Returns: Promise<string[]>

Returns the absolute paths of the files currently cached. Those are the paths of the files before being saved (or after being restored), not while being cached.

module.exports = {
  async onPreBuild({ utils }) {
    const files = await utils.cache.list()
    console.log('Cached files', files)
  },
}

options

cwd

Type: string
Default: process.cwd()

Current directory used to resolve relative paths.

depth

Type: number
Default: 1

Number of subdirectories to include. 0 means only top-level directories will be included.

Current Tags

  • 1.0.1                                ...           latest (18 days ago)

18 Versions

  • 1.0.1                                ...           18 days ago
  • 1.0.0                                ...           a month ago
  • 0.4.2                                ...           2 months ago
  • 0.4.1                                ...           4 months ago
  • 0.4.0                                ...           4 months ago
  • 0.3.0                                ...           4 months ago
  • 0.2.8                                ...           4 months ago
  • 0.2.7                                ...           4 months ago
  • 0.2.6                                ...           4 months ago
  • 0.2.5                                ...           4 months ago
  • 0.2.4                                ...           4 months ago
  • 0.2.3                                ...           4 months ago
  • 0.2.2                                ...           6 months ago
  • 0.2.1                                ...           6 months ago
  • 0.2.0                                ...           6 months ago
  • 0.1.2                                ...           7 months ago
  • 0.1.1                                ...           7 months ago
  • 0.1.0                                ...           8 months ago
Downloads
Today 0
This Week 65
This Month 70
Last Day 7
Last Week 59
Last Month 248
Dependencies (9)
Dev Dependencies (2)

Copyright 2014 - 2016 © taobao.org |