-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsyncPagePath.ts
435 lines (377 loc) · 15 KB
/
syncPagePath.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import type { PageMetadata } from './readPage.js';
import type { GuidesRequestRepresentation, ProjectRepresentation } from './types/index.js';
import type DocsUploadCommand from '../commands/docs/upload.js';
import fs from 'node:fs/promises';
import path from 'node:path';
import chalk from 'chalk';
import ora from 'ora';
import toposort from 'toposort';
import { APIv2Error } from './apiError.js';
import { fix, writeFixes } from './frontmatter.js';
import isCI from './isCI.js';
import { oraOptions } from './logger.js';
import promptTerminal from './promptWrapper.js';
import readdirRecursive from './readdirRecursive.js';
import { fetchMappings, fetchSchema } from './readmeAPIFetch.js';
import readPage from './readPage.js';
import { categoryUriRegexPattern, parentUriRegexPattern } from './types/index.js';
/**
* Commands that use this file for syncing Markdown via APIv2.
*
* Note that the `changelogs` command is not included here
* because it is backed by APIv1.
*/
export type CommandsThatSyncMarkdown = DocsUploadCommand;
type PageRepresentation = GuidesRequestRepresentation;
interface FailedPushResult {
error: APIv2Error | Error;
filePath: string;
result: 'failed';
slug: string;
}
type PushResult =
| FailedPushResult
| {
filePath: string;
response: PageRepresentation | null;
result: 'created' | 'skipped' | 'updated';
slug: string;
};
/**
* Reads the contents of the specified Markdown or HTML file
* and creates/updates the corresponding page in ReadMe
*/
async function pushPage(
this: CommandsThatSyncMarkdown,
/** the file data */
fileData: PageMetadata,
): Promise<PushResult> {
const { key, 'dry-run': dryRun, version } = this.flags;
const { content, filePath, slug } = fileData;
const data = fileData.data;
let route = `/${this.route}`;
if (version) {
route = `/versions/${version}${route}`;
}
const headers = new Headers({ authorization: `Bearer ${key}`, 'Content-Type': 'application/json' });
if (!Object.keys(data).length) {
this.debug(`No frontmatter attributes found for ${filePath}, not syncing`);
return { response: null, filePath, result: 'skipped', slug };
}
const payload: PageRepresentation = {
...data,
content: {
...(typeof data.content === 'object' ? data.content : {}),
body: content,
},
slug,
};
try {
// normalize the category uri
if (payload.category?.uri) {
const regex = new RegExp(categoryUriRegexPattern);
if (!regex.test(payload.category.uri)) {
let uri = payload.category.uri;
this.debug(`normalizing category uri ${uri} for ${filePath}`);
// remove leading and trailing slashes
uri = uri.replace(/^\/|\/$/g, '');
payload.category.uri = `/versions/${version}/categories/${this.route}/${uri}`;
}
}
// normalize the parent uri
if (payload.parent?.uri) {
const regex = new RegExp(parentUriRegexPattern);
if (!regex.test(payload.parent.uri)) {
let uri = payload.parent.uri;
this.debug(`normalizing parent uri ${uri} for ${filePath}`);
// remove leading and trailing slashes
uri = uri.replace(/^\/|\/$/g, '');
payload.parent.uri = `/versions/${version}/${this.route}/${uri}`;
}
}
const createPage = (): Promise<PushResult> | PushResult => {
if (dryRun) {
return { filePath, result: 'created', response: null, slug };
}
return this.readmeAPIFetch(
route,
{ method: 'POST', headers, body: JSON.stringify(payload) },
{ filePath, fileType: 'path' },
)
.then(res => this.handleAPIRes(res))
.then(res => {
return { filePath, result: 'created', response: res, slug };
});
};
const updatePage = (): Promise<PushResult> | PushResult => {
if (dryRun) {
return { filePath, result: 'updated', response: null, slug };
}
// omits the slug from the PATCH payload since this would lead to unexpected behavior
delete payload.slug;
return this.readmeAPIFetch(
`${route}/${slug}`,
{
method: 'PATCH',
headers,
body: JSON.stringify(payload),
},
{ filePath, fileType: 'path' },
)
.then(res => this.handleAPIRes(res))
.then(res => {
return { filePath, result: 'updated', response: res, slug };
});
};
return this.readmeAPIFetch(`${route}/${slug}`, {
method: 'HEAD',
headers,
}).then(async res => {
await this.handleAPIRes(res, true);
if (!res.ok) {
if (res.status !== 404) throw new APIv2Error(res);
this.debug(`error retrieving data for ${slug}, creating page`);
return createPage();
}
this.debug(`data received for ${slug}, updating page`);
return updatePage();
});
} catch (e) {
return { error: e, filePath, result: 'failed', slug };
}
}
const byParentPage = (left: PageMetadata<PageRepresentation>, right: PageMetadata<PageRepresentation>) => {
return (right.data.parent?.uri ? 1 : 0) - (left.data.parent?.uri ? 1 : 0);
};
/**
* Sorts files based on their `parent.uri` attribute. If a file has a `parent.uri` attribute,
* it will be sorted after the file it references.
*
* @see {@link https://github.com/readmeio/rdme/pull/973}
* @returns An array of sorted PageMetadata objects
*/
function sortFiles(files: PageMetadata<PageRepresentation>[]): PageMetadata<PageRepresentation>[] {
const filesBySlug = files.reduce<Record<string, PageMetadata<PageRepresentation>>>((bySlug, obj) => {
// eslint-disable-next-line no-param-reassign
bySlug[obj.slug] = obj;
return bySlug;
}, {});
const dependencies = Object.values(filesBySlug).reduce<
[PageMetadata<PageRepresentation>, PageMetadata<PageRepresentation>][]
>((edges, obj) => {
if (obj.data.parent?.uri && filesBySlug[obj.data.parent.uri]) {
edges.push([filesBySlug[obj.data.parent.uri], filesBySlug[obj.slug]]);
}
return edges;
}, []);
return toposort.array(files, dependencies);
}
/**
* Takes a path (either to a directory of files or to a single file)
* and syncs those (either via POST or PATCH) to ReadMe.
* @returns An array of objects with the results
*/
export default async function syncPagePath(this: CommandsThatSyncMarkdown) {
const { path: pathInput }: { path: string } = this.args;
const { key, 'dry-run': dryRun, 'skip-validation': skipValidation } = this.flags;
const allowedFileExtensions = ['.markdown', '.md'];
const stat = await fs.stat(pathInput).catch(err => {
if (err.code === 'ENOENT') {
throw new Error("Oops! We couldn't locate a file or directory at the path you provided.");
}
throw err;
});
// check whether or not the project has bidirection syncing enabled
// if it is, validations must be skipped to prevent frontmatter from being overwritten
const headers = new Headers({ authorization: `Bearer ${key}` });
const projectMetadata: ProjectRepresentation = await this.readmeAPIFetch('/projects/me', {
method: 'GET',
headers,
}).then(res => {
return this.handleAPIRes(res);
});
const biDiConnection = projectMetadata?.data?.git?.connection?.status === 'active';
if (biDiConnection && !skipValidation) {
throw new Error(
"Bi-directional syncing is enabled for this project. Uploading these docs will overwrite what's currently synced from Git. To proceed with the upload, re-run this command with the `--skip-validation` flag.",
);
}
if (skipValidation) {
this.warn('Skipping pre-upload validation of the Markdown file(s). This is not recommended.');
}
let files: string[];
if (stat.isDirectory()) {
const fileScanningSpinner = ora({ ...oraOptions() }).start(
`🔍 Looking for Markdown files in ${chalk.underline(pathInput)}...`,
);
// Filter out any files that don't match allowedFileExtensions
files = readdirRecursive(pathInput).filter(file =>
allowedFileExtensions.includes(path.extname(file).toLowerCase()),
);
if (!files.length) {
fileScanningSpinner.fail(`${fileScanningSpinner.text} no files found.`);
throw new Error(
`The directory you provided (${pathInput}) doesn't contain any of the following file extensions: ${allowedFileExtensions.join(
', ',
)}.`,
);
}
fileScanningSpinner.succeed(`${fileScanningSpinner.text} ${files.length} file(s) found!`);
} else {
const fileExtension = path.extname(pathInput).toLowerCase();
if (!allowedFileExtensions.includes(fileExtension)) {
throw new Error(
`Invalid file extension (${fileExtension}). Must be one of the following: ${allowedFileExtensions.join(', ')}`,
);
}
files = [pathInput];
}
this.debug(`number of files: ${files.length}`);
let unsortedFiles = files.map(file => readPage.call(this, file));
if (!skipValidation) {
const validationSpinner = ora({ ...oraOptions() }).start('🔬 Validating frontmatter data...');
const schema = await fetchSchema.call(this);
const mappings = await fetchMappings.call(this);
// validate the files, prompt user to fix if necessary
const validationResults = unsortedFiles.map(file => {
this.debug(`validating frontmatter for ${file.filePath}`);
return fix.call(this, file.data, schema, mappings);
});
const filesWithIssues = validationResults.filter(result => result.hasIssues);
const filesWithFixableIssues = filesWithIssues.filter(result => result.changeCount);
const filesWithUnfixableIssues = filesWithIssues.filter(result => result.unfixableErrors.length);
if (filesWithIssues.length) {
validationSpinner.warn(`${validationSpinner.text} issues found in ${filesWithIssues.length} file(s).`);
if (filesWithFixableIssues.length) {
if (isCI()) {
throw new Error(
`${filesWithIssues.length} file(s) have issues that should be fixed before uploading to ReadMe. Please run \`${this.config.bin} ${this.id} ${pathInput} --dry-run\` in a non-CI environment to fix them.`,
);
}
const { confirm } = await promptTerminal([
{
type: 'confirm',
name: 'confirm',
message: `${filesWithFixableIssues.length} file(s) have issues that can be fixed automatically. Would you like to make these changes and continue with the upload to ReadMe?`,
},
]);
if (!confirm) {
throw new Error('Aborting upload due to user input.');
}
const updatedFiles = unsortedFiles.map((file, index) => {
return writeFixes.call(this, file, validationResults[index].updatedData);
});
unsortedFiles = updatedFiles;
}
// also inform the user if there are files with issues that can't be fixed
if (filesWithUnfixableIssues.length) {
this.warn(
`${filesWithUnfixableIssues.length} file(s) have issues that cannot be fixed automatically. The upload will proceed but we recommend addressing these issues. Please get in touch with us at [email protected] if you need a hand.`,
);
}
} else {
validationSpinner.succeed(`${validationSpinner.text} no issues found!`);
}
}
const uploadSpinner = ora({ ...oraOptions() }).start(
dryRun
? "🎭 Uploading files to ReadMe (but not really because it's a dry run)..."
: '🚀 Uploading files to ReadMe...',
);
// topological sort the files
const sortedFiles = sortFiles((unsortedFiles as PageMetadata<PageRepresentation>[]).sort(byParentPage));
// push the files to ReadMe
const rawResults = await Promise.allSettled(sortedFiles.map(async fileData => pushPage.call(this, fileData)));
const results = rawResults.reduce<{
created: PushResult[];
failed: FailedPushResult[];
skipped: PushResult[];
updated: PushResult[];
}>(
(acc, result, index) => {
if (result.status === 'fulfilled') {
const pushResult = result.value;
if (pushResult.result === 'created') {
acc.created.push(pushResult);
} else if (pushResult.result === 'updated') {
acc.updated.push(pushResult);
} else if (pushResult.result === 'failed') {
acc.failed.push(pushResult);
} else {
acc.skipped.push(pushResult);
}
} else {
// we're ignoring these ones for now since errors are handled in the catch block
acc.failed.push({
error: result.reason,
filePath: sortedFiles[index].filePath,
result: 'failed',
slug: sortedFiles[index].slug,
});
}
return acc;
},
{ created: [], updated: [], skipped: [], failed: [] },
);
if (results.failed.length) {
uploadSpinner.fail(`${uploadSpinner.text} ${results.failed.length} file(s) failed.`);
} else {
uploadSpinner.succeed(`${uploadSpinner.text} done!`);
}
if (results.created.length) {
this.log(
dryRun
? `🌱 The following ${results.created.length} page(s) do not currently exist in ReadMe and will be created:`
: `🌱 Successfully created ${results.created.length} page(s) in ReadMe:`,
);
results.created.forEach(({ filePath, slug }) => {
this.log(` - ${slug} (${chalk.underline(filePath)})`);
});
}
if (results.updated.length) {
this.log(
dryRun
? `🔄 The following ${results.updated.length} page(s) already exist in ReadMe and will be updated:`
: `🔄 Successfully updated ${results.updated.length} page(s) in ReadMe:`,
);
results.updated.forEach(({ filePath, slug }) => {
this.log(` - ${slug} (${chalk.underline(filePath)})`);
});
}
if (results.skipped.length) {
this.log(
dryRun
? `⏭️ The following ${results.skipped.length} page(s) will be skipped due to no frontmatter data:`
: `⏭️ Skipped ${results.skipped.length} page(s) in ReadMe due to no frontmatter data:`,
);
results.skipped.forEach(({ filePath, slug }) => {
this.log(` - ${slug} (${chalk.underline(filePath)})`);
});
}
if (results.failed.length) {
this.log(
dryRun
? `🚨 Unable to fetch data about the following ${results.failed.length} page(s):`
: `🚨 Received errors when attempting to upload ${results.failed.length} page(s):`,
);
results.failed.forEach(({ error, filePath }) => {
let errorMessage = error.message || 'unknown error';
if (error instanceof APIv2Error && error.response.title) {
errorMessage = error.response.title;
}
this.log(` - ${chalk.underline(filePath)}: ${errorMessage}`);
});
if (results.failed.length === 1) {
throw results.failed[0].error;
} else {
const errors = results.failed.map(({ error }) => error);
throw new AggregateError(
errors,
dryRun
? `Multiple dry runs failed. To see more detailed errors for a page, run \`${this.config.bin} ${this.id} <path-to-page.md>\` --dry-run.`
: `Multiple page uploads failed. To see more detailed errors for a page, run \`${this.config.bin} ${this.id} <path-to-page.md>\`.`,
);
}
}
return results;
}