Package Exports
- gatsby-core-utils
- gatsby-core-utils/ci
- gatsby-core-utils/cpu-core-count
- gatsby-core-utils/create-content-digest
- gatsby-core-utils/create-require-from-path
- gatsby-core-utils/dist/ci
- gatsby-core-utils/dist/cpu-core-count
- gatsby-core-utils/dist/create-content-digest
- gatsby-core-utils/dist/create-require-from-path
- gatsby-core-utils/dist/fetch-remote-file
- gatsby-core-utils/dist/filename-utils
- gatsby-core-utils/dist/get-config-store
- gatsby-core-utils/dist/get-gatsby-version
- gatsby-core-utils/dist/get-term-program
- gatsby-core-utils/dist/index
- gatsby-core-utils/dist/is-truthy
- gatsby-core-utils/dist/list-plugins
- gatsby-core-utils/dist/lock
- gatsby-core-utils/dist/match-path
- gatsby-core-utils/dist/mutex
- gatsby-core-utils/dist/page-data
- gatsby-core-utils/dist/page-html
- gatsby-core-utils/dist/parse-component-path
- gatsby-core-utils/dist/path
- gatsby-core-utils/dist/physical-cpu-count
- gatsby-core-utils/dist/remote-file-utils/fetch-file
- gatsby-core-utils/dist/service-lock
- gatsby-core-utils/dist/site-metadata
- gatsby-core-utils/dist/url
- gatsby-core-utils/dist/utils
- gatsby-core-utils/dist/utils/get-lmdb
- gatsby-core-utils/dist/utils/get-storage
- gatsby-core-utils/dist/uuid
- gatsby-core-utils/fetch-remote-file
- gatsby-core-utils/filename-utils
- gatsby-core-utils/get-config-store
- gatsby-core-utils/get-gatsby-version
- gatsby-core-utils/get-term-program
- gatsby-core-utils/index
- gatsby-core-utils/is-truthy
- gatsby-core-utils/list-plugins
- gatsby-core-utils/lock
- gatsby-core-utils/match-path
- gatsby-core-utils/mutex
- gatsby-core-utils/page-data
- gatsby-core-utils/page-html
- gatsby-core-utils/parse-component-path
- gatsby-core-utils/path
- gatsby-core-utils/physical-cpu-count
- gatsby-core-utils/remote-file-utils/fetch-file
- gatsby-core-utils/service-lock
- gatsby-core-utils/site-metadata
- gatsby-core-utils/url
- gatsby-core-utils/utils
- gatsby-core-utils/utils/get-lmdb
- gatsby-core-utils/utils/get-storage
- gatsby-core-utils/uuid
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (gatsby-core-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
gatsby-core-utils
Utilities used in multiple Gatsby packages.
Usage
npm install gatsby-core-utils
createContentDigest
Encrypts an input using md5 hash of hexadecimal digest.
const { createContentDigest } = require("gatsby-core-utils")
const options = {
key: "value",
foo: "bar",
}
const digest = createContentDigest(options)
// ...
cpuCoreCount
Calculate the number of CPU cores on the current machine
This function can be controlled by an env variable GATSBY_CPU_COUNT
setting the first argument to true.
value | description |
---|---|
Counts amount of real cores by running a shell command | |
logical-cores | require("os").cpus() to count all virtual cores |
any number | Sets cpu count to that specific number |
const { cpuCoreCount } = require("gatsby-core-utils")
const coreCount = cpuCoreCount(false)
// ...
const { cpuCoreCount } = require("gatsby-core-utils")
process.env.GATSBY_CPU_COUNT = "logical-cores"
const coreCount = cpuCoreCount()
// ...
joinPath
A utility that joins paths with a /
on windows and unix-type platforms. This can also be used for URL concatenation.
const { joinPath } = require("gatsby-core-utils")
const BASEPATH = "/mybase/"
const pathname = "./gatsby/is/awesome"
const url = joinPath(BASEPATH, pathname)
// ...
isCI
A utility that enhances isCI
from 'ci-info` with support for Vercel and Heroku detection
const { isCI } = require("gatsby-core-utils")
if (isCI()) {
// execute CI-specific code
}
// ...
getCIName
A utility that returns the name of the current CI environment if available, null
otherwise
const { getCIName } = require("gatsby-core-utils")
const CI_NAME = getCIName()
console.log({ CI_NAME })
// {CI_NAME: null}, or
// {CI_NAME: "Vercel"}
// ...
createRequireFromPath
A cross-version polyfill for Node's Module.createRequire
.
const { createRequireFromPath } = require("gatsby-core-utils")
const requireUtil = createRequireFromPath("../src/utils/")
// Require `../src/utils/some-tool`
requireUtil("./some-tool")
// ...
Mutex
When working inside workers or async operations you want some kind of concurrency control that a specific work load can only concurrent one at a time. This is what a Mutex does.
By implementing the following code, the code is only executed one at a time and the other threads/async workloads are awaited until the current one is done. This is handy when writing to the same file to disk.
const { createMutex } = require("gatsby-core-utils/mutex")
const mutex = createMutex("my-custom-mutex-key")
await mutex.acquire()
await fs.writeFile("pathToFile", "my custom content")
await mutex.release()