Skip to content

Commit 7cbe12b

Browse files
authored
feat: various changes to get us plugin-ready (#1216)
## 🧰 Changes this PR handles a lot of housekeeping to lay the groundwork for the upcoming internal plugins project. the biggest change is that we're now building declaration files and shipping types for consumption by other plugins. type imports will look something like this: ```ts import type { PageMetadata, PluginHooks } from 'rdme/types'; ``` beyond that, it's mostly little bug fixes, formatting tweaks, and test coverage enhancements. i also removed `@oclif/plugin-not-found` since the plugin makes it brutally slow if you typo something — figured we can add it back if folks complain but i'd be surprised if that was the case. ## 🧬 QA & Testing do tests/etc. pass?
1 parent 2caa832 commit 7cbe12b

File tree

19 files changed

+326
-39
lines changed

19 files changed

+326
-39
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
category:
3+
uri: category-slug
4+
title: This is the document title
5+
content:
6+
body: frontmatter body
7+
excerpt: frontmatter excerpt
8+
type: basic
9+
---
10+
11+
Body
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
category:
3+
uri: category-slug
4+
title: This is the document title
5+
content:
6+
excerpt: frontmatter excerpt
7+
link:
8+
url: https://example.com
9+
new_tab: true
10+
type: link
11+
---
12+
13+
Body

__tests__/commands/docs/__snapshots__/upload.test.ts.snap

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ exports[`rdme docs upload > given that the file path is a directory > given that
8585
},
8686
"stderr": " › Warning: This command is in an experimental alpha and is likely to change.
8787
Use at your own risk!
88-
- 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/multiple-docs...
89-
✔ 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/multiple-docs... 4 file(s) found!
88+
- 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/multiple-docs\` directory...
89+
✔ 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/multiple-docs\` directory... 4 file(s) found!
9090
- 🔬 Validating frontmatter data...
9191
✔ 🔬 Validating frontmatter data... no issues found!
9292
- 🚀 Uploading files to ReadMe...
@@ -124,8 +124,8 @@ exports[`rdme docs upload > given that the file path is a directory > should cre
124124
},
125125
"stderr": " › Warning: This command is in an experimental alpha and is likely to change.
126126
Use at your own risk!
127-
- 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/existing-docs...
128-
✔ 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/existing-docs... 2 file(s) found!
127+
- 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/existing-docs\` directory...
128+
✔ 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/existing-docs\` directory... 2 file(s) found!
129129
- 🔬 Validating frontmatter data...
130130
✔ 🔬 Validating frontmatter data... no issues found!
131131
- 🚀 Uploading files to ReadMe...
@@ -143,8 +143,8 @@ exports[`rdme docs upload > given that the file path is a directory > should err
143143
"error": [Error: The directory you provided (__tests__/__fixtures__/ref-oas) doesn't contain any of the following file extensions: .markdown, .md, .mdx.],
144144
"stderr": " › Warning: This command is in an experimental alpha and is likely to change.
145145
Use at your own risk!
146-
- 🔍 Looking for Markdown files in __tests__/__fixtures__/ref-oas...
147-
✖ 🔍 Looking for Markdown files in __tests__/__fixtures__/ref-oas... no files found.
146+
- 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/ref-oas\` directory...
147+
✖ 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/ref-oas\` directory... no files found.
148148
",
149149
"stdout": "",
150150
}
@@ -155,8 +155,8 @@ exports[`rdme docs upload > given that the file path is a directory > should han
155155
"error": [AggregateError: Multiple dry runs failed. To see more detailed errors for a page, run \`rdme docs upload <path-to-page.md>\` --dry-run.],
156156
"stderr": " › Warning: This command is in an experimental alpha and is likely to change.
157157
› Use at your own risk!
158-
- 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/mixed-docs...
159-
✔ 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/mixed-docs... 5 file(s) found!
158+
- 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/mixed-docs\` directory...
159+
✔ 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/mixed-docs\` directory... 5 file(s) found!
160160
- 🔬 Validating frontmatter data...
161161
⚠ 🔬 Validating frontmatter data... issues found in 2 file(s).
162162
› Warning: 1 file(s) have issues that cannot be fixed automatically. The
@@ -183,8 +183,8 @@ exports[`rdme docs upload > given that the file path is a directory > should han
183183
"error": [AggregateError: Multiple page uploads failed. To see more detailed errors for a page, run \`rdme docs upload <path-to-page.md>\`.],
184184
"stderr": " › Warning: This command is in an experimental alpha and is likely to change.
185185
› Use at your own risk!
186-
- 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/mixed-docs...
187-
✔ 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/mixed-docs... 5 file(s) found!
186+
- 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/mixed-docs\` directory...
187+
✔ 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/mixed-docs\` directory... 5 file(s) found!
188188
- 🔬 Validating frontmatter data...
189189
⚠ 🔬 Validating frontmatter data... issues found in 2 file(s).
190190
› Warning: 1 file(s) have issues that cannot be fixed automatically. The
@@ -206,6 +206,43 @@ exports[`rdme docs upload > given that the file path is a directory > should han
206206
}
207207
`;
208208
209+
exports[`rdme docs upload > given that the file path is a directory > should handle complex frontmatter 1`] = `
210+
{
211+
"result": {
212+
"created": [],
213+
"failed": [],
214+
"skipped": [],
215+
"updated": [
216+
{
217+
"filePath": "__tests__/__fixtures__/docs/complex-frontmatter/basic.md",
218+
"response": {},
219+
"result": "updated",
220+
"slug": "basic",
221+
},
222+
{
223+
"filePath": "__tests__/__fixtures__/docs/complex-frontmatter/link.md",
224+
"response": {},
225+
"result": "updated",
226+
"slug": "link",
227+
},
228+
],
229+
},
230+
"stderr": " › Warning: This command is in an experimental alpha and is likely to change.
231+
Use at your own risk!
232+
- 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/complex-frontmatter\` directory...
233+
✔ 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/complex-frontmatter\` directory... 2 file(s) found!
234+
- 🔬 Validating frontmatter data...
235+
✔ 🔬 Validating frontmatter data... no issues found!
236+
- 🚀 Uploading files to ReadMe...
237+
✔ 🚀 Uploading files to ReadMe... done!
238+
",
239+
"stdout": "🔄 Successfully updated 2 page(s) in ReadMe:
240+
- basic (__tests__/__fixtures__/docs/complex-frontmatter/basic.md)
241+
- link (__tests__/__fixtures__/docs/complex-frontmatter/link.md)
242+
",
243+
}
244+
`;
245+
209246
exports[`rdme docs upload > given that the file path is a directory > should update existing guides pages in ReadMe for each file in the directory and its subdirectories 1`] = `
210247
{
211248
"result": {
@@ -229,8 +266,8 @@ exports[`rdme docs upload > given that the file path is a directory > should upd
229266
},
230267
"stderr": " › Warning: This command is in an experimental alpha and is likely to change.
231268
Use at your own risk!
232-
- 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/existing-docs...
233-
✔ 🔍 Looking for Markdown files in __tests__/__fixtures__/docs/existing-docs... 2 file(s) found!
269+
- 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/existing-docs\` directory...
270+
✔ 🔍 Looking for Markdown files in the \`__tests__/__fixtures__/docs/existing-docs\` directory... 2 file(s) found!
234271
- 🔬 Validating frontmatter data...
235272
✔ 🔬 Validating frontmatter data... no issues found!
236273
- 🚀 Uploading files to ReadMe...

__tests__/commands/docs/upload.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,39 @@ describe('rdme docs upload', () => {
404404
mock.done();
405405
});
406406

407+
it('should handle complex frontmatter', async () => {
408+
const mock = getAPIv2Mock({ authorization })
409+
.head('/versions/stable/guides/basic')
410+
.reply(200)
411+
.patch('/versions/stable/guides/basic', {
412+
title: 'This is the document title',
413+
category: { uri: '/versions/stable/categories/guides/category-slug' },
414+
content: { body: 'frontmatter body', excerpt: 'frontmatter excerpt' },
415+
type: 'basic',
416+
})
417+
.reply(201, {})
418+
.head('/versions/stable/guides/link')
419+
.reply(200)
420+
.patch('/versions/stable/guides/link', {
421+
title: 'This is the document title',
422+
category: { uri: '/versions/stable/categories/guides/category-slug' },
423+
content: {
424+
body: '\nBody\n',
425+
excerpt: 'frontmatter excerpt',
426+
link: { url: 'https://example.com', new_tab: true },
427+
},
428+
type: 'link',
429+
})
430+
.reply(201, {});
431+
432+
const result = await run(['__tests__/__fixtures__/docs/complex-frontmatter', '--key', key]);
433+
434+
expect(result).toMatchSnapshot();
435+
expect(fs.writeFileSync).not.toHaveBeenCalled();
436+
437+
mock.done();
438+
});
439+
407440
describe('given that the directory contains parent/child docs', () => {
408441
it('should upload parents before children', async () => {
409442
const mock = getAPIv2Mock({ authorization })

__tests__/helpers/oclif.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export function setupOclifConfig() {
3939
export function runCommand(Command: CommandClass) {
4040
return async function runCommandAgainstArgs(args?: string[]) {
4141
const oclifConfig = await setupOclifConfig();
42+
// @ts-expect-error currently we have mismatching return types in our commands.
43+
// we can fix this later but it's not a priority right now.
4244
return captureOutput<string>(() => Command.run(args, oclifConfig), { testNodeEnv });
4345
};
4446
}

__tests__/lib/frontmatter.test.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import type { PageMetadata } from '../../src/lib/readPage.js';
12
import type nock from 'nock';
23
import type { SchemaObject } from 'oas/types';
34

4-
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
5+
import fs from 'node:fs';
6+
7+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
58

69
import DocsUploadCommand from '../../src/commands/docs/upload.js';
7-
import { fix } from '../../src/lib/frontmatter.js';
10+
import { fix, writeFixes } from '../../src/lib/frontmatter.js';
811
import { emptyMappings, fetchSchema } from '../../src/lib/readmeAPIFetch.js';
912
import { oasFetchMock } from '../helpers/get-api-mock.js';
1013
import { setupOclifConfig } from '../helpers/oclif.js';
@@ -309,3 +312,61 @@ describe('#fix', () => {
309312
`);
310313
});
311314
});
315+
316+
describe('#writeFixes', () => {
317+
let command: DocsUploadCommand;
318+
319+
const mockPageData: PageMetadata = {
320+
content: 'some content',
321+
data: {
322+
updated: false,
323+
},
324+
filePath: 'docs/page.md',
325+
hash: '',
326+
slug: 'some-page',
327+
};
328+
329+
beforeEach(async () => {
330+
const oclifConfig = await setupOclifConfig();
331+
command = new DocsUploadCommand([], oclifConfig);
332+
vi.spyOn(fs, 'existsSync').mockImplementation(() => false);
333+
vi.spyOn(fs, 'mkdirSync').mockImplementation(() => '');
334+
vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
335+
});
336+
337+
afterEach(() => {
338+
vi.restoreAllMocks();
339+
});
340+
341+
it('should write changes', () => {
342+
writeFixes.call(command, mockPageData, { updated: true });
343+
344+
expect(fs.writeFileSync).toHaveBeenCalledWith(mockPageData.filePath, expect.stringContaining('updated: true'), {
345+
encoding: 'utf-8',
346+
});
347+
});
348+
349+
it('should write changes to current directory', () => {
350+
writeFixes.call(command, mockPageData, { updated: true }, '.');
351+
352+
expect(fs.writeFileSync).toHaveBeenCalledWith(mockPageData.filePath, expect.stringContaining('updated: true'), {
353+
encoding: 'utf-8',
354+
});
355+
});
356+
357+
it('should write changes to specified output directory', () => {
358+
writeFixes.call(command, mockPageData, { updated: true }, 'out');
359+
360+
expect(fs.writeFileSync).toHaveBeenCalledWith('out/docs/page.md', expect.stringContaining('updated: true'), {
361+
encoding: 'utf-8',
362+
});
363+
});
364+
365+
it('should write changes to specified output directory (with trailing slash)', () => {
366+
writeFixes.call(command, mockPageData, { updated: true }, 'out/');
367+
368+
expect(fs.writeFileSync).toHaveBeenCalledWith('out/docs/page.md', expect.stringContaining('updated: true'), {
369+
encoding: 'utf-8',
370+
});
371+
});
372+
});

bin/set-version-output.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// @ts-check
33
import * as core from '@actions/core';
44

5-
// eslint-disable-next-line import/extensions
65
import { getNodeVersion, getMajorPkgVersion } from '../dist/lib/getPkg.js';
76

87
/**

knip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { KnipConfig } from 'knip';
22

33
const config: KnipConfig = {
4-
entry: ['src/index.ts', 'src/lib/help.ts', 'bin/*.js'],
4+
entry: ['src/index.ts', 'src/types.ts', 'src/lib/help.ts', 'bin/*.js'],
55
ignore: ['dist-gha/**'],
66
// we're choosing not to include semantic release deps in our main dev-deps
77
// since we're only using it in CI

0 commit comments

Comments
 (0)