-
Notifications
You must be signed in to change notification settings - Fork 31
/
filesystem.ts
218 lines (183 loc) · 5.41 KB
/
filesystem.ts
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
218
import { ConfluenceSettings } from "src/Settings";
import { BinaryFile, FilesToUpload, LoaderAdaptor, MarkdownFile } from ".";
import { lookup } from "mime-types";
import { existsSync, lstatSync } from "fs";
import * as fs from "fs/promises";
import * as path from "path";
import matter, { stringify } from "gray-matter";
import {
ConfluencePerPageAllValues,
ConfluencePerPageConfig,
conniePerPageConfig,
} from "../ConniePageConfig";
export class FileSystemAdaptor implements LoaderAdaptor {
settings: ConfluenceSettings;
constructor(settings: ConfluenceSettings) {
this.settings = settings;
if (!existsSync(settings.contentRoot)) {
throw new Error(`'${settings.contentRoot}' doesn't exist.`);
}
if (!lstatSync(settings.contentRoot).isDirectory()) {
throw new Error(`'${settings.contentRoot}' is not a directory.`);
}
}
async getFileContent(absoluteFilePath: string) {
const fileContent = await fs.readFile(absoluteFilePath, "utf-8");
const { data, content } = matter(fileContent);
return { data, content };
}
async updateMarkdownValues(
absoluteFilePath: string,
values: Partial<ConfluencePerPageAllValues>
): Promise<void> {
const actualAbsoluteFilePath = path.join(
this.settings.contentRoot,
absoluteFilePath
);
if (!(await fs.stat(actualAbsoluteFilePath)).isFile()) {
return;
}
const fileContent = await this.getFileContent(actualAbsoluteFilePath);
const config = conniePerPageConfig;
const fm: { [key: string]: unknown } = {};
for (const propertyKey in config) {
if (!config.hasOwnProperty(propertyKey)) {
continue;
}
const { key } =
config[propertyKey as keyof ConfluencePerPageConfig];
const value =
values[propertyKey as keyof ConfluencePerPageAllValues];
if (propertyKey in values) {
fm[key] = value;
}
}
const updatedData = stringify(fileContent, fm);
await fs.writeFile(actualAbsoluteFilePath, updatedData);
}
async loadMarkdownFile(absoluteFilePath: string): Promise<MarkdownFile> {
const { data, content: contents } = await this.getFileContent(
absoluteFilePath
);
const folderName = path.basename(path.parse(absoluteFilePath).dir);
const fileName = path.basename(absoluteFilePath);
const extension = path.extname(fileName);
const pageTitle = path.basename(fileName, extension);
return {
folderName,
absoluteFilePath: absoluteFilePath.replace(
this.settings.contentRoot,
""
),
fileName,
pageTitle,
contents,
frontmatter: data,
};
}
async loadMarkdownFiles(folderPath: string): Promise<MarkdownFile[]> {
const files: MarkdownFile[] = [];
const entries = await fs.readdir(folderPath, {
withFileTypes: true,
});
for (const entry of entries) {
const absoluteFilePath = path.join(folderPath, entry.name);
if (entry.isFile() && path.extname(entry.name) === ".md") {
const file = await this.loadMarkdownFile(absoluteFilePath);
files.push(file);
} else if (entry.isDirectory()) {
const subFiles = await this.loadMarkdownFiles(absoluteFilePath);
files.push(...subFiles);
}
}
return files;
}
async getMarkdownFilesToUpload(): Promise<FilesToUpload> {
const files = await this.loadMarkdownFiles(this.settings.contentRoot);
const filesToPublish = [];
for (const file of files) {
try {
const frontMatter = file.frontmatter;
if (
((file.absoluteFilePath.startsWith(
this.settings.folderToPublish
) ||
this.settings.folderToPublish === ".") &&
(!frontMatter ||
frontMatter["connie-publish"] !== false)) ||
(frontMatter && frontMatter["connie-publish"] === true)
) {
filesToPublish.push(file);
}
} catch {
//ignore
}
}
return filesToPublish;
}
async readBinary(
searchPath: string,
referencedFromFilePath: string
): Promise<BinaryFile | false> {
const absoluteFilePath = await this.findClosestFile(
searchPath,
path.dirname(
path.join(this.settings.contentRoot, referencedFromFilePath)
)
);
if (absoluteFilePath) {
const fileContents = await fs.readFile(absoluteFilePath);
const mimeType =
lookup(path.extname(absoluteFilePath)) ||
"application/octet-stream";
return {
contents: fileContents,
filePath: absoluteFilePath.replace(
this.settings.contentRoot,
""
),
filename: path.basename(absoluteFilePath),
mimeType: mimeType,
};
}
return false;
}
private async findClosestFile(
fileName: string,
startingDirectory: string
): Promise<string | null> {
const matchingFiles: string[] = [];
const directoriesToSearch: string[] = [startingDirectory];
while (directoriesToSearch.length > 0) {
const currentDirectory = directoriesToSearch.shift();
if (!currentDirectory) {
continue;
}
const entries = await fs.readdir(currentDirectory, {
withFileTypes: true,
});
for (const entry of entries) {
const fullPath = path.join(currentDirectory, entry.name);
if (
entry.isFile() &&
entry.name.toLowerCase() === fileName.toLowerCase()
) {
matchingFiles.push(fullPath);
} else if (
entry.isDirectory() &&
fullPath.startsWith(this.settings.contentRoot)
) {
directoriesToSearch.push(fullPath);
}
}
}
if (matchingFiles.length > 0) {
return matchingFiles[0];
}
const parentDirectory = path.dirname(startingDirectory);
if (parentDirectory === startingDirectory) {
return null;
}
return await this.findClosestFile(fileName, parentDirectory);
}
}