This repository was archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathsetupNetlifyFunctionForPage.js
62 lines (55 loc) · 1.72 KB
/
setupNetlifyFunctionForPage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { copySync } = require("fs-extra");
const { join } = require("path");
const {
NEXT_DIST_DIR,
TEMPLATES_DIR,
FUNCTION_TEMPLATE_PATH,
} = require("../config");
const getNetlifyFunctionName = require("./getNetlifyFunctionName");
const copyDynamicImportChunks = require("./copyDynamicImportChunks");
const { logItem } = require("./logger");
// Create a Netlify Function for the page with the given file path
const setupNetlifyFunctionForPage = ({
filePath,
functionsPath,
isApiPage,
}) => {
// Set function name based on file path
const functionName = getNetlifyFunctionName(filePath, isApiPage);
const functionDirectory = join(functionsPath, functionName);
if (isApiPage && functionName.endsWith("-background")) {
logItem(
`👁 Setting up API page ${functionName} as a Netlify background function`
);
}
// Copy function templates
const functionTemplateCopyPath = join(
functionDirectory,
`${functionName}.js`
);
copySync(FUNCTION_TEMPLATE_PATH, functionTemplateCopyPath, {
overwrite: false,
errorOnExist: true,
});
// Copy function helpers
const functionHelpers = [
"renderNextPage.js",
"createRequestObject.js",
"createResponseObject.js",
];
functionHelpers.forEach((helper) => {
copySync(join(TEMPLATES_DIR, helper), join(functionDirectory, helper), {
overwrite: false,
errorOnExist: true,
});
});
// Copy any dynamic import chunks
copyDynamicImportChunks(functionDirectory);
// Copy page
const nextPageCopyPath = join(functionDirectory, "nextPage", "index.js");
copySync(join(NEXT_DIST_DIR, "serverless", filePath), nextPageCopyPath, {
overwrite: false,
errorOnExist: true,
});
};
module.exports = setupNetlifyFunctionForPage;