Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (await pathExists(searchIndexPath)) {
console.warn(
`searchIndex detected at ${searchIndexPath}, will overwrite for this build but this may indicate an accidental conflict`,
)
}
await makeDir(`${searchIndexPath}/..`)
let stringifiedIndex = JSON.stringify(searchIndex)
await pWriteFile(searchIndexPath, stringifiedIndex)
const searchIndexFunctionPath = path.join(FUNCTIONS_SRC, 'searchIndex')
await cpy(__dirname + '/functionTemplate', searchIndexFunctionPath)
// now we have copied it out to intermediate dir
// we may want to do some processing/templating
await pWriteFile(path.join(searchIndexFunctionPath, 'searchIndex.json'), stringifiedIndex)
// and then..
await zipFunctions(FUNCTIONS_SRC, FUNCTIONS_DIST)
console.log('Files copied!')
// done with generating functions
},
}
async function hashFns(dir, opts) {
opts = Object.assign(
{
concurrentHash: 100,
assetType: 'function',
hashAlgorithm: 'sha256',
// tmpDir,
statusCb: () => {}
},
opts
)
// early out if the functions dir is omitted
if (!dir) return { functions: {}, shaMap: {} }
if (!opts.tmpDir) throw new Error('Missing tmpDir directory for zipping files')
const functionZips = await zipIt.zipFunctions(dir, opts.tmpDir)
const fileObjs = functionZips.map(({ path: functionPath, runtime }) => ({
filepath: functionPath,
root: opts.tmpDir,
relname: path.relative(opts.tmpDir, functionPath),
basename: path.basename(functionPath),
extname: path.extname(functionPath),
type: 'file',
assetType: 'function',
normalizedPath: path.basename(functionPath, path.extname(functionPath)),
runtime
}))
const functionStream = fromArray.obj(fileObjs)
const hasher = hasherCtor(opts)
const onFunctionsPackage = async function({ constants: { FUNCTIONS_SRC, FUNCTIONS_DIST } }) {
if (!(await pathExists(FUNCTIONS_SRC))) {
return
}
console.log(`Packaging functions from ${FUNCTIONS_SRC}`)
await zipFunctions(FUNCTIONS_SRC, FUNCTIONS_DIST)
await logResults(FUNCTIONS_DIST)
}
const onFunctionsPackage = async function({ constants: { FUNCTIONS_SRC, FUNCTIONS_DIST } }) {
console.log(`Packaging functions from ${FUNCTIONS_SRC}`)
await zipFunctions(FUNCTIONS_SRC, FUNCTIONS_DIST)
await logResults(FUNCTIONS_DIST)
}
if (!src || !dst) {
if (!src)
this.log(
`${NETLIFYDEVERR} Error: You must specify a source folder with a --src flag or a functionsSource field in your config`
);
if (!dst)
this.log(
`${NETLIFYDEVERR} Error: You must specify a destination functions folder with a --functions flag or a functions field in your config`
);
process.exit(1);
}
fs.mkdirSync(dst, { recursive: true });
this.log(`${NETLIFYDEVLOG} Building functions`);
zipFunctions(src, dst, { skipGo: true });
this.log(`${NETLIFYDEVLOG} Functions built to `, dst);
}
}
`${NETLIFYDEVERR} Error: You must specify a destination functions folder with a --functions flag or a functions field in your config`
)
process.exit(1)
}
await this.config.runHook('analytics', {
eventName: 'command',
payload: {
command: 'functions:build'
}
})
fs.mkdirSync(dst, { recursive: true })
this.log(`${NETLIFYDEVLOG} Building functions`)
zipFunctions(src, dst, { skipGo: true })
this.log(`${NETLIFYDEVLOG} Functions built to `, dst)
}
}
async function packageFunction(functionPath, destFolder) {
await fs.ensureDir(destFolder)
await zipFunction(functionPath, destFolder)
}