UNPKG

1.5 kBJavaScriptView Raw
1'use strict'
2
3const { readFile } = require('fs')
4const { promisify } = require('util')
5
6const pReadFile = promisify(readFile)
7
8// Performance optimization when @netlify/config caller has already previously
9// called it and cached the result.
10// This is used by the buildbot which:
11// - first calls @netlify/config since it needs configuration property
12// - later calls @netlify/build, which runs @netlify/config under the hood
13// This is also used by Netlify CLI.
14const getCachedConfig = async function ({ cachedConfig, cachedConfigPath, token, api }) {
15 const parsedCachedConfig = await parseCachedConfig(cachedConfig, cachedConfigPath)
16 // The CLI does not print some properties when they are not serializable or
17 // are confidential. Those will be missing from `cachedConfig`. The caller
18 // must supply them again when using `cachedConfig`.
19 return parsedCachedConfig === undefined ? undefined : { token, ...parsedCachedConfig, api }
20}
21
22// `cachedConfig` is a plain object while `cachedConfigPath` is a file path to
23// a JSON file. The former is useful in programmatic usage while the second one
24// is useful in CLI usage, to avoid hitting the OS limits if the configuration
25// file is too big.
26const parseCachedConfig = async function (cachedConfig, cachedConfigPath) {
27 if (cachedConfig !== undefined) {
28 return cachedConfig
29 }
30
31 if (cachedConfigPath !== undefined) {
32 return JSON.parse(await pReadFile(cachedConfigPath, 'utf8'))
33 }
34}
35
36module.exports = { getCachedConfig }