This repository has been 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 pathindex.js
83 lines (65 loc) · 2.01 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { normalize } = require("path");
const debounceFn = require("debounce-fn");
const chokidar = require("chokidar");
const execa = require("execa");
const { logTitle } = require("./lib/helpers/logger");
const prepareFolders = require("./lib/steps/prepareFolders");
const copyPublicFiles = require("./lib/steps/copyPublicFiles");
const copyNextAssets = require("./lib/steps/copyNextAssets");
const setupPages = require("./lib/steps/setupPages");
const setupImageFunction = require("./lib/steps/setupImageFunction");
const setupRedirects = require("./lib/steps/setupRedirects");
const setupHeaders = require("./lib/steps/setupHeaders");
const {
NETLIFY_PUBLISH_PATH,
NETLIFY_FUNCTIONS_PATH,
SRC_FILES,
} = require("./lib/config");
const build = (functionsPath, publishPath) => {
const trackNextOnNetlifyFiles = prepareFolders({
functionsPath,
publishPath,
});
copyPublicFiles(publishPath);
copyNextAssets(publishPath);
setupPages({ functionsPath, publishPath });
setupImageFunction(functionsPath);
setupRedirects(publishPath);
setupHeaders(publishPath);
trackNextOnNetlifyFiles();
logTitle("✅ Success! All done!");
};
const watch = (functionsPath, publishPath) => {
logTitle(`👀 Watching source code for changes`);
const runBuild = debounceFn(
() => {
try {
execa.sync("next", ["build"], { stdio: "inherit" });
build(functionsPath, publishPath);
} catch (e) {
console.log(e);
}
},
{
wait: 3000,
}
);
chokidar.watch(SRC_FILES).on("all", runBuild);
};
/** options param:
* {
* functionsDir: string to path
* publishDir: string to path
* watch: { directory: string to path }
* }
*/
const nextOnNetlify = (options = {}) => {
const functionsPath = normalize(
options.functionsDir || NETLIFY_FUNCTIONS_PATH
);
const publishPath = normalize(options.publishDir || NETLIFY_PUBLISH_PATH);
options.watch
? watch(functionsPath, publishPath)
: build(functionsPath, publishPath);
};
module.exports = nextOnNetlify;