Skip to content

Commit

Permalink
chore: random touch-ups from #1159 (#1160)
Browse files Browse the repository at this point in the history
## 🧰 Changes

cherry-picking over a few random refactors from
#1159 that are mostly unrelated to
the PR itself:

- [x] removes unused fixtures
- [x] smol copy tweaks for changelog command ("syncing" is reserved for
bidi, this is "uploading"
- [x] jsdoc tweaks, including deprecating APIv1-related cruft
- [x] various type enhancements
- [x] renaming a few things
- [x] adding `oclif`'s `this` context (so we can simplify functions and
add better debug logs)

## 🧬 QA & Testing

do tests still pass?
  • Loading branch information
kanadgupta authored Jan 31, 2025
1 parent e4874fc commit 2870c4b
Show file tree
Hide file tree
Showing 38 changed files with 153 additions and 184 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ npm run build && npm run build:docs
# Command Topics

* [`rdme autocomplete`](documentation/commands/autocomplete.md) - Display autocomplete installation instructions.
* [`rdme changelogs`](documentation/commands/changelogs.md) - Sync Markdown files to your ReadMe project as Changelog posts.
* [`rdme changelogs`](documentation/commands/changelogs.md) - Upload Markdown files to your ReadMe project as Changelog posts.
* [`rdme help`](documentation/commands/help.md) - Display help for rdme.
* [`rdme login`](documentation/commands/login.md) - Login to a ReadMe project.
* [`rdme logout`](documentation/commands/logout.md) - Logs the currently authenticated user out of ReadMe.
Expand Down
Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions __tests__/__fixtures__/custompages/failure-docs/new-doc.md

This file was deleted.

5 changes: 0 additions & 5 deletions __tests__/__fixtures__/custompages/new-docs-html/new-doc.html

This file was deleted.

6 changes: 0 additions & 6 deletions __tests__/__fixtures__/custompages/new-docs/new-doc.md

This file was deleted.

7 changes: 0 additions & 7 deletions __tests__/__fixtures__/custompages/slug-docs/new-doc-slug.md

This file was deleted.

4 changes: 0 additions & 4 deletions __tests__/__fixtures__/docs/delete-docs/some-doc.md

This file was deleted.

6 changes: 0 additions & 6 deletions __tests__/__fixtures__/docs/docs-with-parent-ids/child.md

This file was deleted.

5 changes: 0 additions & 5 deletions __tests__/__fixtures__/docs/docs-with-parent-ids/friend.md

This file was deleted.

5 changes: 0 additions & 5 deletions __tests__/__fixtures__/docs/docs-with-parent-ids/parent.md

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions __tests__/__fixtures__/docs/failure-docs/new-doc.md

This file was deleted.

6 changes: 0 additions & 6 deletions __tests__/__fixtures__/docs/multiple-docs-cycle/child.md

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions __tests__/__fixtures__/docs/multiple-docs-cycle/parent.md

This file was deleted.

6 changes: 0 additions & 6 deletions __tests__/__fixtures__/docs/multiple-docs-no-parents/child.md

This file was deleted.

This file was deleted.

6 changes: 4 additions & 2 deletions __tests__/helpers/get-gha-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import getGitRemoteMock from './get-git-mock.js';
/**
* A helper function for setting up tests for our GitHub Action onboarding.
*
* @param writeFileSyncCb the mock function that should be called in place of `fs.writeFileSync`.
* @see {@link __tests__/lib/createGHA.test.ts}
*/
export function before(writeFileSyncCb) {
export function before(
/** the mock function that should be called in place of `fs.writeFileSync` */
writeFileSyncCb: typeof fs.writeFileSync,
) {
fs.writeFileSync = vi.fn(writeFileSyncCb);

git.checkIsRepo = vi.fn(() => {
Expand Down
7 changes: 3 additions & 4 deletions __tests__/helpers/get-git-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { vi } from 'vitest';

/**
* Creates a mock function for testing `git.remote`.
*
* @param remote remote to return (usually `origin`)
* @param remoteUrl git URL for the given remote
* @param defaultBranch the HEAD branch
*/
export default function getGitRemoteMock(
/** remote to return (usually `origin`) */
remote = 'origin',
/** git URL for the given remote */
remoteUrl = 'https://github.com/readmeio/rdme.git',
/** the HEAD branch */
defaultBranch = 'main',
) {
return vi.fn(arr => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/helpers/oclif.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Command as OclifCommand } from '@oclif/core';
import type { CommandClass } from '../../src/index.js';

import path from 'node:path';

Expand Down Expand Up @@ -36,7 +36,7 @@ export function setupOclifConfig() {
*
* @example runCommand(LoginCommand)(['--email', '[email protected]', '--password', 'password'])
*/
export function runCommand<T extends typeof OclifCommand>(Command: T) {
export function runCommand(Command: CommandClass) {
return async function runCommandAgainstArgs(args?: string[]) {
const oclifConfig = await setupOclifConfig();
// @ts-expect-error this is the pattern recommended by the @oclif/test docs.
Expand All @@ -52,7 +52,7 @@ export function runCommand<T extends typeof OclifCommand>(Command: T) {
*
* @example runCommandAndReturnResult(LoginCommand)(['--email', '[email protected]', '--password', 'password'])
*/
export function runCommandAndReturnResult<T extends typeof OclifCommand>(Command: T) {
export function runCommandAndReturnResult(Command: CommandClass) {
return async function runCommandAgainstArgs(args?: string[]) {
const { error, result } = await runCommand(Command)(args);
if (error) {
Expand Down
8 changes: 4 additions & 4 deletions documentation/commands/changelogs.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
`rdme changelogs`
=================

Sync Markdown files to your ReadMe project as Changelog posts.
Upload Markdown files to your ReadMe project as Changelog posts.

* [`rdme changelogs PATH`](#rdme-changelogs-path)

## `rdme changelogs PATH`

Sync Markdown files to your ReadMe project as Changelog posts.
Upload Markdown files to your ReadMe project as Changelog posts.

```
USAGE
Expand All @@ -22,14 +22,14 @@ FLAGS
--key=<value> (required) ReadMe project API key
DESCRIPTION
Sync Markdown files to your ReadMe project as Changelog posts.
Upload Markdown files to your ReadMe project as Changelog posts.
Syncs Markdown files to the Changelog section of your ReadMe project. The path can either be a directory or a single
Markdown file. The Markdown files will require YAML front matter with certain ReadMe documentation attributes. Check
out our docs for more info on setting up your front matter: https://docs.readme.com/main/docs/rdme#markdown-file-setup
EXAMPLES
Passing in a path to a directory will also sync any Markdown files that are located in subdirectories. The path
Passing in a path to a directory will also upload any Markdown files that are located in subdirectories. The path
input can also be individual Markdown files:
$ rdme changelogs [path] --version={project-version}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/changelogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class ChangelogsCommand extends BaseCommand<typeof ChangelogsComm
// needed for unit tests, even though we also specify this in src/index.ts
static id = 'changelogs' as const;

static summary = 'Sync Markdown files to your ReadMe project as Changelog posts.';
static summary = 'Upload Markdown files to your ReadMe project as Changelog posts.';

static description =
'Syncs Markdown files to the Changelog section of your ReadMe project. The path can either be a directory or a single Markdown file. The Markdown files will require YAML front matter with certain ReadMe documentation attributes. Check out our docs for more info on setting up your front matter: https://docs.readme.com/main/docs/rdme#markdown-file-setup';
Expand All @@ -23,7 +23,7 @@ export default class ChangelogsCommand extends BaseCommand<typeof ChangelogsComm
static examples = [
{
description:
'Passing in a path to a directory will also sync any Markdown files that are located in subdirectories. The path input can also be individual Markdown files:',
'Passing in a path to a directory will also upload any Markdown files that are located in subdirectories. The path input can also be individual Markdown files:',
command: '<%= config.bin %> <%= command.id %> [path] --version={project-version}',
},
{
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ValueOf } from 'type-fest';

import ChangelogsCommand from './commands/changelogs.js';
import LoginCommand from './commands/login.js';
import LogoutCommand from './commands/logout.js';
Expand Down Expand Up @@ -36,6 +38,8 @@ export const COMMANDS = {
whoami: WhoAmICommand,
};

export type CommandClass = ValueOf<typeof COMMANDS>;

/**
* A type-safe way to get the command IDs in the CLI for a specific topic.
*
Expand Down
2 changes: 2 additions & 0 deletions src/lib/apiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type APIv2ErrorResponse = Partial<{

/**
* Error class for handling ReadMe API v1 errors.
*
* @deprecated Use {@link APIv2Error} instead.
*/
export class APIv1Error extends Error {
code: string;
Expand Down
12 changes: 8 additions & 4 deletions src/lib/createGHA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ type ParsedOpts = Record<string, unknown>;

/**
* Generates the key for storing info in `configstore` object.
* @param repoRoot The root of the repo
*/
export const getConfigStoreKey = (repoRoot: string) => `createGHA.${repoRoot}`;
export const getConfigStoreKey = (
/** the root of the repository */
repoRoot: string,
) => `createGHA.${repoRoot}`;
/**
* The directory where GitHub Actions workflow files are stored.
*
Expand All @@ -40,9 +42,11 @@ export const git = simpleGit();
/**
* Removes any non-file-friendly characters and adds
* the full path + file extension for GitHub Workflow files.
* @param fileName raw file name to clean up
*/
export const getGHAFileName = (fileName: string) => {
export const getGHAFileName = (
/** raw file name to clean up */
fileName: string,
) => {
return path.join(GITHUB_WORKFLOW_DIR, `${cleanFileName(fileName).toLowerCase()}.yml`);
};

Expand Down
11 changes: 8 additions & 3 deletions src/lib/getPkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@ export function getPkgVersion(this: Hook.Context | void): string {
/**
* The current `rdme` version
*
* @param npmDistTag the `npm` dist tag to retrieve. If this value is omitted,
* the version from the `package.json` is returned.
* @example "8.0.0"
* @see {@link https://docs.npmjs.com/adding-dist-tags-to-packages}
* @note we mock this function in our snapshots
* @see {@link https://stackoverflow.com/a/54245672}
*/
export async function getPkgVersionFromNPM(this: Hook.Context | void, npmDistTag?: npmDistTag): Promise<string> {
export async function getPkgVersionFromNPM(
this: Hook.Context | void,
/**
* The `npm` dist tag to retrieve. If this value is omitted,
* the version from the `package.json` is returned.
*/
npmDistTag?: npmDistTag,
): Promise<string> {
if (npmDistTag) {
return fetch(registryUrl)
.then(res => res.json() as Promise<{ 'dist-tags': Record<string, string> }>)
Expand Down
11 changes: 8 additions & 3 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ function oraOptions() {

/**
* Wrapper for warn statements.
* @param prefix Text that precedes the warning.
* This is *not* used in the GitHub Actions-formatted warning.
*/
function warn(input: string, prefix = 'Warning!') {
function warn(
/**
* Text that precedes the warning.
* This is *not* used in the GitHub Actions-formatted warning.
*/
input: string,
prefix = 'Warning!',
) {
/* istanbul ignore next */
if (isGHA() && !isTest()) return core.warning(input);
// eslint-disable-next-line no-console
Expand Down
11 changes: 8 additions & 3 deletions src/lib/loginFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ function loginFetch(body: LoginBody) {
* The prompt flow for logging a user in and writing the credentials to
* `configstore`. This is a separate lib function because we reuse it both
* in the `login` command as well as any time a user omits an API key.
* @param otp an optional one-time passcode, if the user passes one in
* via a flag to the the `login` command
*
* @returns A Promise-wrapped string with the logged-in user's credentials
*/
export default async function loginFlow(otp?: string) {
export default async function loginFlow(
/**
* An optional one-time passcode, if the user passes one in
* via a flag to the the `login` command
*/
otp?: string,
) {
const storedConfig = getCurrentConfig();
const { email, password, project } = await promptTerminal([
{
Expand Down
13 changes: 8 additions & 5 deletions src/lib/prepareOas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ const capitalizeSpecType = (type: string) =>

/**
* Normalizes, validates, and (optionally) bundles an OpenAPI definition.
*
* @param path Path to a spec file. If this is missing, the current directory is searched for
* certain file names.
* @param command The command context in which this is being run within (uploading a spec,
* validation, or reducing one).
*/
export default async function prepareOas(
/**
* Path to a spec file. If this is missing, the current directory is searched for
* certain file names.
*/
path: string | undefined,
/**
* The command context in which this is being run within (uploading a spec,
* validation, or reducing one).
*/
command: `openapi ${OpenAPIAction}`,
opts: {
/**
Expand Down
Loading

0 comments on commit 2870c4b

Please sign in to comment.