-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use
zip-it-and-ship-it
to list Functions (#1478)
- Loading branch information
Showing
6 changed files
with
74 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,29 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { listFunctions } = require('@netlify/zip-it-and-ship-it') | ||
|
||
const { findModuleDir, findHandler } = require('./finders') | ||
const { fileExistsAsync } = require('../lib/fs') | ||
|
||
const getUrlPath = (functionName) => { | ||
return `/.netlify/functions/${functionName}` | ||
} | ||
|
||
const BACKGROUND = '-background' | ||
|
||
const isBackground = (functionPath) => { | ||
const filename = path.basename(functionPath, path.extname(functionPath)) | ||
return filename.endsWith(BACKGROUND) | ||
const addFunctionProps = ({ mainFile, name, runtime }) => { | ||
const urlPath = getUrlPath(name) | ||
const isBackground = name.endsWith(BACKGROUND) | ||
return { mainFile, name, runtime, urlPath, isBackground } | ||
} | ||
|
||
module.exports = { | ||
getFunctions(dir) { | ||
const functions = {} | ||
if (fs.existsSync(dir)) { | ||
fs.readdirSync(dir).forEach((file) => { | ||
if (dir === 'node_modules') { | ||
return | ||
} | ||
const functionPath = path.resolve(path.join(dir, file)) | ||
const handlerPath = findHandler(functionPath) | ||
if (!handlerPath) { | ||
return | ||
} | ||
if (path.extname(functionPath) === '.js') { | ||
functions[file.replace(/\.js$/, '')] = { | ||
functionPath, | ||
moduleDir: findModuleDir(functionPath), | ||
isBackground: isBackground(functionPath), | ||
} | ||
} else if (fs.lstatSync(functionPath).isDirectory()) { | ||
functions[file] = { | ||
functionPath: handlerPath, | ||
moduleDir: findModuleDir(functionPath), | ||
isBackground: isBackground(handlerPath), | ||
} | ||
} | ||
}) | ||
} | ||
return functions | ||
}, | ||
const JS = 'js' | ||
|
||
const getFunctions = async (functionsSrcDir) => { | ||
if (!(await fileExistsAsync(functionsSrcDir))) { | ||
return [] | ||
} | ||
|
||
const functions = await listFunctions(functionsSrcDir) | ||
const functionsWithProps = functions.filter(({ runtime }) => runtime === JS).map((func) => addFunctionProps(func)) | ||
return functionsWithProps | ||
} | ||
|
||
module.exports = { getFunctions } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters