Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Reusable functions: internal PR for clarity #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

bahmutov
Copy link

@bahmutov bahmutov commented Nov 12, 2019

Factor out reusable functions

Typical use

const { restoreCache, saveCache } = require('cache/lib/index')
const hasha = require('hasha')

// let's cache NPM modules ~/.npm
const packageLockHash = hasha.fromFileSync('./package-lock.json')
const platformAndArch = `${process.platform}-${process.arch}`

const NPM_CACHE = (() => {
  const o = {
    inputPath: '~/.npm',
    restoreKeys: `npm-${platformAndArch}-`
  }
  o.primaryKey = o.restoreKeys + packageLockHash
  return o
})()

const restoreCachedNpm = () => {
  console.log('trying to restore cached NPM modules')
  return restoreCache(
    NPM_CACHE.inputPath,
    NPM_CACHE.primaryKey,
    NPM_CACHE.restoreKeys
  )
}

const saveCachedNpm = () => {
  console.log('saving NPM modules')
  return saveCache(NPM_CACHE.inputPath, NPM_CACHE.primaryKey)
}

Note

  • ignore lib files - these are just transpiled from src to allow us directly installing this module from GitHub using a tag without going through NPM

@bahmutov bahmutov changed the title Reusable functions Reusable functions: internal PR for clarity Nov 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant