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 pathhandleFileTracking.js
80 lines (69 loc) · 2.43 KB
/
handleFileTracking.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
const { join } = require("path");
const {
existsSync,
readdirSync,
readFileSync,
writeFileSync,
removeSync,
} = require("fs-extra");
const findCacheDir = require("find-cache-dir");
const { NETLIFY_PUBLISH_PATH, NETLIFY_FUNCTIONS_PATH } = require("../config");
const TRACKING_FILE_SEPARATOR = "---";
// Clean configured publish and functions folders and track next-on-netlify files
// for future cleans
const handleFileTracking = ({ functionsPath, publishPath }) => {
const isConfiguredFunctionsDir = functionsPath !== NETLIFY_FUNCTIONS_PATH;
const isConfiguredPublishDir = publishPath !== NETLIFY_PUBLISH_PATH;
const cacheDir = findCacheDir({ name: "next-on-netlify", create: true });
const trackingFilePath = join(cacheDir, ".nonfiletracking");
if (existsSync(trackingFilePath)) {
const trackingFile = readFileSync(trackingFilePath, "utf8");
const [trackedFunctions, trackedPublish] = trackingFile.split(
TRACKING_FILE_SEPARATOR
);
const cleanConfiguredFiles = (trackedFiles) => {
trackedFiles.forEach((file) => {
const filePath = join(publishPath, file);
if (file !== "" && existsSync(filePath)) {
removeSync(filePath);
}
});
};
if (isConfiguredPublishDir) {
cleanConfiguredFiles(trackedPublish.split("\n"));
}
if (isConfiguredFunctionsDir) {
cleanConfiguredFiles(trackedFunctions.split("\n"));
}
}
const functionsBeforeRun = existsSync(functionsPath)
? readdirSync(functionsPath)
: [];
const publishBeforeRun = existsSync(publishPath)
? readdirSync(publishPath)
: [];
// this callback will run at the end of nextOnNetlify()
const trackNewFiles = () => {
const functionsAfterRun = isConfiguredFunctionsDir
? readdirSync(functionsPath)
: functionsBeforeRun;
const publishAfterRun = isConfiguredPublishDir
? readdirSync(publishPath)
: publishBeforeRun;
const getDifference = (before, after) =>
after.filter((filePath) => !before.includes(filePath));
const newFunctionsFiles = getDifference(
functionsBeforeRun,
functionsAfterRun
);
const newPublishFiles = getDifference(publishBeforeRun, publishAfterRun);
const allTrackedFiles = [
...newFunctionsFiles,
TRACKING_FILE_SEPARATOR,
...newPublishFiles,
];
writeFileSync(trackingFilePath, allTrackedFiles.join("\n"));
};
return trackNewFiles;
};
module.exports = handleFileTracking;