Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove workingDirectory argument #904

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,6 @@ You can add `--update` to the command so if there's only one API definition for
rdme openapi [url-or-local-path-to-file] --version={project-version} --update
```

##### Override the Working Directory

By default, `rdme` bundles all [references](https://swagger.io/docs/specification/using-ref/) with paths based on the directory that `rdme` is being run in. You can override the working directory using the `--workingDirectory` option, which can be helpful for bundling certain external references (see [here](__tests__/__fixtures__/relative-ref-oas/petstore.json) for an example file).

```sh
rdme openapi petstore.json --workingDirectory=[path to directory]
```

#### Validating an API Definition

You can also perform a local validation of your API definition (no ReadMe account required!), which can be useful when constructing or editing your API definition.
Expand Down
18 changes: 18 additions & 0 deletions __tests__/cmds/openapi/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ describe('rdme openapi:convert', () => {
expect(Object.keys(reducedSpec.paths)).toStrictEqual(['/pet/{petId}']);
expect(Object.keys(reducedSpec.paths['/pet/{petId}'])).toStrictEqual(['get', 'post', 'delete']);
});

it('should convert with no prompts via opts', async () => {
let reducedSpec;
fs.writeFileSync = vi.fn((fileName, data) => {
reducedSpec = JSON.parse(data as string);
});

await expect(
convert.run({
spec: require.resolve('@readme/oas-examples/2.0/json/petstore-simple.json'),
out: 'output.json',
}),
).resolves.toBe(successfulConversion());

expect(fs.writeFileSync).toHaveBeenCalledWith('output.json', expect.any(String));
expect(Object.keys(reducedSpec.paths)).toStrictEqual(['/pet/{petId}']);
expect(Object.keys(reducedSpec.paths['/pet/{petId}'])).toStrictEqual(['get', 'post', 'delete']);
});
});

describe('error handling', () => {
Expand Down
46 changes: 0 additions & 46 deletions __tests__/single-threaded/openapi/convert.test.ts

This file was deleted.

10 changes: 1 addition & 9 deletions src/cmds/openapi/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { validateFilePath } from '../../lib/validatePromptInput.js';
interface Options {
out?: string;
spec?: string;
workingDirectory?: string;
}

export default class OpenAPIConvertCommand extends Command {
Expand All @@ -39,20 +38,13 @@ export default class OpenAPIConvertCommand extends Command {
type: String,
description: 'Output file path to write converted file to',
},
this.getWorkingDirArg(),
];
}

async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, workingDirectory } = opts;

if (workingDirectory) {
const previousWorkingDirectory = process.cwd();
process.chdir(workingDirectory);
Command.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
}
const { spec } = opts;

const { preparedSpec, specPath, specType } = await prepareOas(spec, 'openapi:convert', { convertToLatest: true });
const parsedPreparedSpec: OASDocument = JSON.parse(preparedSpec);
Expand Down
10 changes: 1 addition & 9 deletions src/cmds/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ interface Options {
update?: boolean;
useSpecVersion?: boolean;
version?: string;
workingDirectory?: string;
}

export default class OpenAPICommand extends Command {
Expand All @@ -54,7 +53,6 @@ export default class OpenAPICommand extends Command {
type: String,
defaultOption: true,
},
this.getWorkingDirArg(),
{
name: 'useSpecVersion',
type: Boolean,
Expand Down Expand Up @@ -90,7 +88,7 @@ export default class OpenAPICommand extends Command {
async run(opts: AuthenticatedCommandOptions<Options>) {
await super.run(opts);

const { dryRun, key, id, spec, create, raw, title, useSpecVersion, version, workingDirectory, update } = opts;
const { dryRun, key, id, spec, create, raw, title, useSpecVersion, version, update } = opts;

let selectedVersion = version;
let isUpdate: boolean;
Expand All @@ -111,12 +109,6 @@ export default class OpenAPICommand extends Command {
);
}

if (workingDirectory) {
const previousWorkingDirectory = process.cwd();
process.chdir(workingDirectory);
Command.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
}

if (version && id) {
Command.warn(
"We'll be using the version associated with the `--id` option, so the `--version` option will be ignored.",
Expand Down
10 changes: 1 addition & 9 deletions src/cmds/openapi/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import SoftError from '../../lib/softError.js';
interface Options {
feature?: string[];
spec?: string;
workingDirectory?: string;
}

export default class OpenAPIInspectCommand extends Command {
Expand All @@ -40,7 +39,6 @@ export default class OpenAPIInspectCommand extends Command {
type: String,
defaultOption: true,
},
this.getWorkingDirArg(),
{
name: 'feature',
type: String,
Expand Down Expand Up @@ -218,7 +216,7 @@ export default class OpenAPIInspectCommand extends Command {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, workingDirectory, feature: features } = opts;
const { spec, feature: features } = opts;

// If we have features we should validate that they're supported.
if (features?.length) {
Expand All @@ -232,12 +230,6 @@ export default class OpenAPIInspectCommand extends Command {
}
}

if (workingDirectory) {
const previousWorkingDirectory = process.cwd();
process.chdir(workingDirectory);
Command.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
}

const { preparedSpec, definitionVersion } = await prepareOas(spec, 'openapi:inspect', { convertToLatest: true });
this.definitionVersion = definitionVersion.version;
const parsedPreparedSpec: OASDocument = JSON.parse(preparedSpec);
Expand Down
10 changes: 1 addition & 9 deletions src/cmds/openapi/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface Options {
spec?: string;
tag?: string[];
title?: string;
workingDirectory?: string;
}

export default class OpenAPIReduceCommand extends Command {
Expand Down Expand Up @@ -66,20 +65,13 @@ export default class OpenAPIReduceCommand extends Command {
description: 'Output file path to write reduced file to',
},
this.getTitleArg(),
this.getWorkingDirArg(),
];
}

async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, title, workingDirectory } = opts;

if (workingDirectory) {
const previousWorkingDirectory = process.cwd();
process.chdir(workingDirectory);
Command.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
}
const { spec, title } = opts;

const { preparedSpec, specPath, specType } = await prepareOas(spec, 'openapi:reduce', { title });
const parsedPreparedSpec: OASDocument = JSON.parse(preparedSpec);
Expand Down
10 changes: 1 addition & 9 deletions src/cmds/openapi/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import prepareOas from '../../lib/prepareOas.js';

export interface Options {
spec?: string;
workingDirectory?: string;
}

export default class OpenAPIValidateCommand extends Command {
Expand All @@ -27,21 +26,14 @@ export default class OpenAPIValidateCommand extends Command {
type: String,
defaultOption: true,
},
this.getWorkingDirArg(),
this.getGitHubArg(),
];
}

async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, workingDirectory } = opts;

if (workingDirectory) {
const previousWorkingDirectory = process.cwd();
process.chdir(workingDirectory);
Command.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
}
const { spec } = opts;

const { specPath, specType } = await prepareOas(spec, 'openapi:validate');
return Promise.resolve(chalk.green(`${specPath} is a valid ${specType} API definition!`)).then(msg =>
Expand Down
11 changes: 0 additions & 11 deletions src/lib/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,6 @@ export default class Command {
];
}

/**
* Used in the `openapi` family of commands where `workingDirectory` is an option.
*/
getWorkingDirArg(): OptionDefinition {
return {
name: 'workingDirectory',
type: String,
description: 'Working directory (for usage with relative external references)',
};
}

static debug(msg: string) {
debug(msg);
}
Expand Down