-
Notifications
You must be signed in to change notification settings - Fork 10
/
readWrite.js
217 lines (179 loc) · 6.76 KB
/
readWrite.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const fs = require("fs-extra");
const path = require("path").posix;
const map_ = require("lodash/map");
const flowRight_ = require("lodash/flowRight");
const { readDirToJson } = require("corvid-dir-as-json");
const { logger } = require("corvid-local-logger");
const createAsyncQueue = require("./utils/asyncQueue");
const initWithBackup = require("./utils/backup");
const { deleteEmptySubFolders } = require("./utils/fileUtils");
const {
isPathOfCodeFile,
isPathOfDocumentFile,
isPathOfPageRealtedFile,
isPathOfEmptyByDefaultCodeFile,
pageCodeFilePath,
ROOT_PATHS,
DEFAULT_FILE_PATHS
} = require("./sitePaths");
const {
editorCodeIntelligenceToLocalTypingsFiles,
editorDocumentToLocalDocumentFiles,
localDocumentFilesToEditorDocument,
editorCodeFilesToLocalCodeFiles,
localCodeFilesToEditorCodeFiles,
editorCodePathToLocalCodePath,
updateLocalPageFilePath
} = require("./siteConverter");
const listFilesRecursive = async siteRootPath =>
Object.keys(
await readDirToJson(siteRootPath, {
delimiter: "/",
readFiles: false,
onlyFiles: true
})
);
const listLocalDocumentFiles = async siteRootPath => {
const allFilePaths = await listFilesRecursive(siteRootPath);
return allFilePaths.filter(filePath => isPathOfDocumentFile(filePath));
};
const listLocalCodeFiles = async siteRootPath => {
const allFilePaths = await listFilesRecursive(siteRootPath);
return allFilePaths.filter(filePath => isPathOfCodeFile(filePath));
};
const listLocalPageFiles = async siteRootPath => {
const allFilePaths = await listFilesRecursive(siteRootPath);
return allFilePaths.filter(filePath => isPathOfPageRealtedFile(filePath));
};
const readLocalFiles = async (siteRootPath, filePaths) => {
const filesWithContent = await Promise.all(
filePaths.map(async filePath => {
const content = await fs.readFile(
path.join(siteRootPath, filePath),
"utf8"
);
return { path: filePath, content };
})
);
return filesWithContent;
};
const readLocalDocumentFiles = async siteRootPath => {
const localDocumentFilePaths = await listLocalDocumentFiles(siteRootPath);
return readLocalFiles(siteRootPath, localDocumentFilePaths);
};
const readLocalCodeFiles = async siteRootPath => {
const localCodeFilePaths = await listLocalCodeFiles(siteRootPath);
return readLocalFiles(siteRootPath, localCodeFilePaths);
};
const readWrite = (siteRootPath, filesWatcher, backupPath) => {
const withBackup = initWithBackup(siteRootPath, backupPath, filesWatcher);
const ensureLocalFolderSkeleton = () =>
Promise.all([
...map_(ROOT_PATHS, dirOrFilePath =>
fs.ensureDir(path.join(siteRootPath, dirOrFilePath))
),
...map_(DEFAULT_FILE_PATHS, dirOrFilePath =>
fs.ensureFile(path.join(siteRootPath, dirOrFilePath))
)
]);
const syncExistingPageFolders = async newSiteDocumentPages => {
const existingPageFilePaths = await listLocalPageFiles(siteRootPath);
const updatedPageFilePaths = existingPageFilePaths.map(existingPath => ({
source: existingPath,
target: updateLocalPageFilePath(existingPath, newSiteDocumentPages)
}));
await Promise.all(
updatedPageFilePaths.map(({ source, target }) =>
target === null
? filesWatcher.ignoredDeleteFile(source)
: target !== source
? filesWatcher.ignoredMoveFile(source, target)
: Promise.resolve()
)
);
await map_(newSiteDocumentPages, page =>
filesWatcher.ignoredEnsureFile(pageCodeFilePath(page))
);
await deleteEmptySubFolders(path.join(siteRootPath, ROOT_PATHS.PAGES));
await deleteEmptySubFolders(path.join(siteRootPath, ROOT_PATHS.LIGHTBOXES));
};
const updateSiteDocument = async newEditorDocument => {
await ensureLocalFolderSkeleton();
await syncExistingPageFolders(newEditorDocument.pages);
const newLocalDocumentFiles = editorDocumentToLocalDocumentFiles(
newEditorDocument
);
await Promise.all(
newLocalDocumentFiles.map(localFile =>
filesWatcher.ignoredWriteFile(localFile.path, localFile.content)
)
);
};
const getSiteDocument = async () => {
const localDocumentFiles = await readLocalDocumentFiles(siteRootPath);
return localDocumentFilesToEditorDocument(localDocumentFiles);
};
const updateCode = async ({
modifiedFiles = [],
copiedFiles = [],
deletedFiles = []
} = {}) => {
await ensureLocalFolderSkeleton();
const existingLocalPageFilePaths = await listLocalPageFiles(siteRootPath);
const editorPathToLocalPath = editorPath =>
editorCodePathToLocalCodePath(editorPath, existingLocalPageFilePaths);
const localModifiedFiles = editorCodeFilesToLocalCodeFiles(
modifiedFiles,
existingLocalPageFilePaths
);
const modifications = localModifiedFiles.map(localFile =>
filesWatcher.ignoredWriteFile(localFile.path, localFile.content)
);
const copies = copiedFiles.map(({ source, target }) =>
filesWatcher.ignoredCopyFile(
editorPathToLocalPath(source.path),
editorPathToLocalPath(target.path)
)
);
const deletes = deletedFiles.map(deletedFile => {
const localFilePath = editorPathToLocalPath(deletedFile.path);
return isPathOfEmptyByDefaultCodeFile(localFilePath)
? filesWatcher.ignoredWriteFile(localFilePath, "")
: filesWatcher.ignoredDeleteFile(localFilePath);
});
await Promise.all([...modifications, ...copies, ...deletes]);
};
const getCodeFiles = async () => {
const localCodeFiles = await readLocalCodeFiles(siteRootPath);
return localCodeFilesToEditorCodeFiles(localCodeFiles);
};
const updateCodeIntelligence = async codeIntelligence => {
await ensureLocalFolderSkeleton();
const existingLocalPageFilePaths = await listLocalPageFiles(siteRootPath);
const newLocalCodeIntelligenceFiles = editorCodeIntelligenceToLocalTypingsFiles(
codeIntelligence.elementsMap,
existingLocalPageFilePaths
);
await Promise.all(
newLocalCodeIntelligenceFiles.map(localFile =>
filesWatcher.ignoredWriteFile(localFile.path, localFile.content)
)
);
};
const readWriteQueue = createAsyncQueue();
const withStartFinishLog = callback => async (...args) => {
logger.info(`${callback.name} started`);
const result = await callback(...args);
logger.info(`${callback.name} finished`);
return result;
};
const withLogsAndQueue = flowRight_(readWriteQueue, withStartFinishLog);
return {
updateSiteDocument: withLogsAndQueue(withBackup(updateSiteDocument)),
getSiteDocument: withLogsAndQueue(getSiteDocument),
getCodeFiles: withLogsAndQueue(getCodeFiles),
updateCode: withLogsAndQueue(updateCode),
updateCodeIntelligence: withLogsAndQueue(updateCodeIntelligence)
};
};
module.exports = readWrite;