Skip to content

Commit f8cd895

Browse files
authored
fix(openapi/upload): restore --working-directory (#1369)
| 🚥 Resolves #1350, CX-2286 | | :------------------- | ## 🧰 Changes restores the `--working-directory` (formerly known as `--workingDirectory`) flag for `openapi upload`. more chatter about this can be found [here](https://linear.app/readme-io/issue/CX-2286/cli-openapi-upload-command-not-respecting-relative-working-directory#comment-ee239f54). ## 🧬 QA & Testing do tests pass?
1 parent 3b84f42 commit f8cd895

File tree

13 files changed

+79
-82
lines changed

13 files changed

+79
-82
lines changed

__tests__/commands/openapi/__snapshots__/validate.test.ts.snap

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
"
6666
`;
6767
68-
exports[`rdme openapi validate > GHA onboarding E2E tests > should create GHA workflow if user passes in spec via opt (including workingDirectory) 1`] = `
68+
exports[`rdme openapi validate > GHA onboarding E2E tests > should create GHA workflow if user passes in spec via opt (including working directory) 1`] = `
6969
{
7070
"result": "
7171
Your GitHub Actions workflow file has been created!
@@ -88,7 +88,7 @@ Almost done! Push your newly created file (.github/workflows/validate-test-opt-s
8888
}
8989
`;
9090
91-
exports[`rdme openapi validate > GHA onboarding E2E tests > should create GHA workflow if user passes in spec via opt (including workingDirectory) 2`] = `
91+
exports[`rdme openapi validate > GHA onboarding E2E tests > should create GHA workflow if user passes in spec via opt (including working directory) 2`] = `
9292
"# This GitHub Actions workflow was auto-generated by the \`rdme\` cli on 2022-01-01T00:00:00.000Z
9393
# You can view our full documentation here: https://docs.readme.com/docs/rdme
9494
name: ReadMe GitHub Action 🦉
@@ -111,7 +111,7 @@ jobs:
111111
- name: Run \`openapi validate\` command 🚀
112112
uses: readmeio/rdme@v7
113113
with:
114-
rdme: openapi validate petstore.json --workingDirectory=./__tests__/__fixtures__/relative-ref-oas
114+
rdme: openapi validate petstore.json --working-directory=./__tests__/__fixtures__/relative-ref-oas
115115
"
116116
`;
117117
@@ -348,6 +348,17 @@ exports[`rdme openapi validate > should discover and upload an API definition if
348348
}
349349
`;
350350
351+
exports[`rdme openapi validate > should use specified working directory (legacy flag) 1`] = `
352+
{
353+
"result": "petstore.json is a valid OpenAPI API definition!",
354+
"stderr": " › Warning: The "--workingDirectory" flag has been deprecated. Use
355+
"--working-directory" instead.
356+
- Validating the API definition located at petstore.json...
357+
",
358+
"stdout": "",
359+
}
360+
`;
361+
351362
exports[`rdme openapi validate > should use specified working directory 1`] = `
352363
{
353364
"result": "petstore.json is a valid OpenAPI API definition!",

__tests__/commands/openapi/convert.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('rdme openapi convert', () => {
5858
await expect(
5959
run([
6060
spec,
61-
'--workingDirectory',
61+
'--working-directory',
6262
require.resolve(`@readme/oas-examples/2.0/json/${spec}`).replace(spec, ''),
6363
'--out',
6464
'output.json',
@@ -79,7 +79,7 @@ describe('rdme openapi convert', () => {
7979
await expect(
8080
run([
8181
spec,
82-
'--workingDirectory',
82+
'--working-directory',
8383
require.resolve(`@readme/oas-examples/3.0/${format}/${spec}`).replace(spec, ''),
8484
]),
8585
).resolves.toMatchSnapshot();

__tests__/commands/openapi/reduce.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('rdme openapi reduce', () => {
6262
prompts.inject(['tags', ['user'], 'output.json']);
6363

6464
await expect(
65-
run(['--workingDirectory', './__tests__/__fixtures__/relative-ref-oas']),
65+
run(['--working-directory', './__tests__/__fixtures__/relative-ref-oas']),
6666
).resolves.toMatchSnapshot();
6767

6868
expect(Object.keys(reducedSpec.paths)).toStrictEqual(['/user']);
@@ -71,7 +71,7 @@ describe('rdme openapi reduce', () => {
7171
it('should reduce with no prompts via opts', async () => {
7272
await expect(
7373
run([
74-
'--workingDirectory',
74+
'--working-directory',
7575
'./__tests__/__fixtures__/relative-ref-oas',
7676
'--tag',
7777
'user',
@@ -108,7 +108,7 @@ describe('rdme openapi reduce', () => {
108108
it('should reduce with no prompts via opts', async () => {
109109
await expect(
110110
run([
111-
'--workingDirectory',
111+
'--working-directory',
112112
'./__tests__/__fixtures__/relative-ref-oas',
113113
'--path',
114114
'/pet',
@@ -134,7 +134,7 @@ describe('rdme openapi reduce', () => {
134134

135135
await expect(
136136
run([
137-
'--workingDirectory',
137+
'--working-directory',
138138
'./__tests__/__fixtures__/relative-ref-oas',
139139
'--path',
140140
'/pet',

__tests__/commands/openapi/validate.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ describe('rdme openapi validate', () => {
4040
});
4141

4242
it('should discover and upload an API definition if none is provided', async () => {
43-
const result = await run(['--workingDirectory', './__tests__/__fixtures__/relative-ref-oas']);
43+
const result = await run(['--working-directory', './__tests__/__fixtures__/relative-ref-oas']);
4444
expect(result).toMatchSnapshot();
4545
});
4646

4747
it('should use specified working directory', async () => {
48+
const result = await run(['petstore.json', '--working-directory', './__tests__/__fixtures__/relative-ref-oas']);
49+
expect(result).toMatchSnapshot();
50+
});
51+
52+
it('should use specified working directory (legacy flag)', async () => {
4853
const result = await run(['petstore.json', '--workingDirectory', './__tests__/__fixtures__/relative-ref-oas']);
4954
expect(result).toMatchSnapshot();
5055
});
@@ -55,7 +60,7 @@ describe('rdme openapi validate', () => {
5560
'./__tests__/__fixtures__/nested-gitignored-oas/nest/petstore-ignored.json',
5661
);
5762

58-
const result = await run(['--workingDirectory', './__tests__/__fixtures__/nested-gitignored-oas']);
63+
const result = await run(['--working-directory', './__tests__/__fixtures__/nested-gitignored-oas']);
5964
expect(result).toMatchSnapshot();
6065
});
6166

@@ -138,13 +143,13 @@ describe('rdme openapi validate', () => {
138143
expect(fs.writeFileSync).toHaveBeenCalledWith(`.github/workflows/${fileName}.yml`, expect.any(String));
139144
});
140145

141-
it('should create GHA workflow if user passes in spec via opt (including workingDirectory)', async () => {
146+
it('should create GHA workflow if user passes in spec via opt (including working directory)', async () => {
142147
const spec = 'petstore.json';
143148
const fileName = 'validate-test-opt-spec-workdir-file';
144149
prompts.inject([true, 'validate-test-opt-spec-github-branch', fileName]);
145150

146151
await expect(
147-
run([spec, '--workingDirectory', './__tests__/__fixtures__/relative-ref-oas']),
152+
run([spec, '--working-directory', './__tests__/__fixtures__/relative-ref-oas']),
148153
).resolves.toMatchSnapshot();
149154

150155
expect(yamlOutput).toMatchSnapshot();

documentation/commands/openapi.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ Converts an API definition to OpenAPI and bundles any external references.
1616

1717
```
1818
USAGE
19-
$ rdme openapi convert [SPEC] [--out <value>] [--title <value>] [--workingDirectory <value>]
19+
$ rdme openapi convert [SPEC] [--out <value>] [--title <value>] [--working-directory <value>]
2020
2121
ARGUMENTS
2222
SPEC A path to your API definition — either a local file path or a URL. If your working directory and all
2323
subdirectories contain a single OpenAPI file, you can omit the path.
2424
2525
FLAGS
26-
--out=<value> Output file path to write converted file to
27-
--title=<value> An override value for the `info.title` field in the API definition
28-
--workingDirectory=<value> Working directory (for usage with relative external references)
26+
--out=<value> Output file path to write converted file to
27+
--title=<value> An override value for the `info.title` field in the API definition
28+
--working-directory=<value> Working directory (for usage with relative external references)
2929
3030
DESCRIPTION
3131
Converts an API definition to OpenAPI and bundles any external references.
@@ -54,7 +54,7 @@ Analyze an OpenAPI/Swagger definition for various OpenAPI and ReadMe feature usa
5454
USAGE
5555
$ rdme openapi inspect [SPEC] [--feature
5656
additionalProperties|callbacks|circularRefs|commonParameters|discriminators|links|style|polymorphism|serverVariables
57-
|webhooks|xml|xmlRequests|xmlResponses|xmlSchemas|readme...] [--workingDirectory <value>]
57+
|webhooks|xml|xmlRequests|xmlResponses|xmlSchemas|readme...] [--working-directory <value>]
5858
5959
ARGUMENTS
6060
SPEC A path to your API definition — either a local file path or a URL. If your working directory and all
@@ -67,7 +67,7 @@ FLAGS
6767
<options: additionalProperties|callbacks|circularRefs|commonParameters|discriminators|links|style|polymorphism|serve
6868
rVariables|webhooks|xml|xmlRequests|xmlResponses|xmlSchemas|readme>
6969
70-
--workingDirectory=<value>
70+
--working-directory=<value>
7171
Working directory (for usage with relative external references)
7272
7373
DESCRIPTION
@@ -102,19 +102,19 @@ Reduce an OpenAPI definition into a smaller subset.
102102
```
103103
USAGE
104104
$ rdme openapi reduce [SPEC] [--method <value>...] [--out <value>] [--path <value>...] [--tag <value>...] [--title
105-
<value>] [--workingDirectory <value>]
105+
<value>] [--working-directory <value>]
106106
107107
ARGUMENTS
108108
SPEC A path to your API definition — either a local file path or a URL. If your working directory and all
109109
subdirectories contain a single OpenAPI file, you can omit the path.
110110
111111
FLAGS
112-
--method=<value>... Methods to reduce by (can only be used alongside the `path` option)
113-
--out=<value> Output file path to write reduced file to
114-
--path=<value>... Paths to reduce by
115-
--tag=<value>... Tags to reduce by
116-
--title=<value> An override value for the `info.title` field in the API definition
117-
--workingDirectory=<value> Working directory (for usage with relative external references)
112+
--method=<value>... Methods to reduce by (can only be used alongside the `path` option)
113+
--out=<value> Output file path to write reduced file to
114+
--path=<value>... Paths to reduce by
115+
--tag=<value>... Tags to reduce by
116+
--title=<value> An override value for the `info.title` field in the API definition
117+
--working-directory=<value> Working directory (for usage with relative external references)
118118
119119
DESCRIPTION
120120
Reduce an OpenAPI definition into a smaller subset.
@@ -143,16 +143,16 @@ Resolves circular and recursive references in OpenAPI by replacing them with obj
143143

144144
```
145145
USAGE
146-
$ rdme openapi resolve [SPEC] [--out <value>] [--title <value>] [--workingDirectory <value>]
146+
$ rdme openapi resolve [SPEC] [--out <value>] [--title <value>] [--working-directory <value>]
147147
148148
ARGUMENTS
149149
SPEC A path to your API definition — either a local file path or a URL. If your working directory and all
150150
subdirectories contain a single OpenAPI file, you can omit the path.
151151
152152
FLAGS
153-
--out=<value> Output file path to write resolved file to
154-
--title=<value> An override value for the `info.title` field in the API definition
155-
--workingDirectory=<value> Working directory (for usage with relative external references)
153+
--out=<value> Output file path to write resolved file to
154+
--title=<value> An override value for the `info.title` field in the API definition
155+
--working-directory=<value> Working directory (for usage with relative external references)
156156
157157
DESCRIPTION
158158
Resolves circular and recursive references in OpenAPI by replacing them with object schemas.
@@ -186,20 +186,21 @@ Upload (or re-upload) your API definition to ReadMe.
186186
```
187187
USAGE
188188
$ rdme openapi upload [SPEC] --key <value> [--confirm-overwrite] [--slug <value>] [--title <value>]
189-
[--useSpecVersion | --branch <value>]
189+
[--useSpecVersion | --branch <value>] [--working-directory <value>]
190190
191191
ARGUMENTS
192192
SPEC A path to your API definition — either a local file path or a URL. If your working directory and all
193193
subdirectories contain a single OpenAPI file, you can omit the path.
194194
195195
FLAGS
196-
--key=<value> (required) ReadMe project API key
197-
--branch=<value> [default: stable] ReadMe project version
198-
--confirm-overwrite If set, file overwrites will be made without a confirmation prompt. This flag can be a useful in
199-
automated environments where prompts cannot be responded to.
200-
--slug=<value> Override the slug (i.e., the unique identifier) for your API definition.
201-
--title=<value> An override value for the `info.title` field in the API definition
202-
--useSpecVersion Use the OpenAPI `info.version` field for your ReadMe project version
196+
--key=<value> (required) ReadMe project API key
197+
--branch=<value> [default: stable] ReadMe project version
198+
--confirm-overwrite If set, file overwrites will be made without a confirmation prompt. This flag can be a
199+
useful in automated environments where prompts cannot be responded to.
200+
--slug=<value> Override the slug (i.e., the unique identifier) for your API definition.
201+
--title=<value> An override value for the `info.title` field in the API definition
202+
--useSpecVersion Use the OpenAPI `info.version` field for your ReadMe project version
203+
--working-directory=<value> Working directory (for usage with relative external references)
203204
204205
DESCRIPTION
205206
Upload (or re-upload) your API definition to ReadMe.
@@ -266,15 +267,15 @@ Validate your OpenAPI/Swagger definition.
266267

267268
```
268269
USAGE
269-
$ rdme openapi validate [SPEC] [--github] [--workingDirectory <value>]
270+
$ rdme openapi validate [SPEC] [--github] [--working-directory <value>]
270271
271272
ARGUMENTS
272273
SPEC A path to your API definition — either a local file path or a URL. If your working directory and all
273274
subdirectories contain a single OpenAPI file, you can omit the path.
274275
275276
FLAGS
276-
--github Create a new GitHub Actions workflow for this command.
277-
--workingDirectory=<value> Working directory (for usage with relative external references)
277+
--github Create a new GitHub Actions workflow for this command.
278+
--working-directory=<value> Working directory (for usage with relative external references)
278279
279280
DESCRIPTION
280281
Validate your OpenAPI/Swagger definition.

src/commands/openapi/convert.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class OpenAPIConvertCommand extends BaseCommand<typeof OpenAPICon
2828
static flags = {
2929
out: Flags.string({ description: 'Output file path to write converted file to' }),
3030
title: titleFlag,
31-
workingDirectory: workingDirectoryFlag,
31+
'working-directory': workingDirectoryFlag,
3232
};
3333

3434
static examples = [
@@ -45,13 +45,7 @@ export default class OpenAPIConvertCommand extends BaseCommand<typeof OpenAPICon
4545
];
4646

4747
async run() {
48-
const { out, workingDirectory } = this.flags;
49-
50-
if (workingDirectory) {
51-
const previousWorkingDirectory = process.cwd();
52-
process.chdir(workingDirectory);
53-
this.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
54-
}
48+
const { out } = this.flags;
5549

5650
const { preparedSpec, specPath, specType } = await prepareOas.call(this);
5751
const parsedPreparedSpec: OASDocument = JSON.parse(preparedSpec);

src/commands/openapi/inspect.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export default class OpenAPIInspectCommand extends BaseCommand<typeof OpenAPIIns
216216
multiple: true,
217217
options: getSupportedFeatures(),
218218
}),
219-
workingDirectory: workingDirectoryFlag,
219+
'working-directory': workingDirectoryFlag,
220220
};
221221

222222
static examples = [
@@ -239,19 +239,13 @@ export default class OpenAPIInspectCommand extends BaseCommand<typeof OpenAPIIns
239239
];
240240

241241
async run() {
242-
const { workingDirectory, feature: features } = this.flags;
242+
const { feature: features } = this.flags;
243243

244244
const tableBorder = Object.entries(getBorderCharacters('norc'))
245245
.map(([border, char]) => ({ [border]: chalk.gray(char) }))
246246
// biome-ignore lint/performance/noAccumulatingSpread: @todo can this be improved?
247247
.reduce((prev, next) => Object.assign(prev, next));
248248

249-
if (workingDirectory) {
250-
const previousWorkingDirectory = process.cwd();
251-
process.chdir(workingDirectory);
252-
this.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
253-
}
254-
255249
const { preparedSpec, definitionVersion } = await prepareOas.call(this);
256250
const parsedPreparedSpec: OASDocument = JSON.parse(preparedSpec);
257251

src/commands/openapi/reduce.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class OpenAPIReduceCommand extends BaseCommand<typeof OpenAPIRedu
3838
path: Flags.string({ description: 'Paths to reduce by', multiple: true }),
3939
tag: Flags.string({ description: 'Tags to reduce by', multiple: true }),
4040
title: titleFlag,
41-
workingDirectory: workingDirectoryFlag,
41+
'working-directory': workingDirectoryFlag,
4242
};
4343

4444
static examples = [
@@ -61,13 +61,6 @@ export default class OpenAPIReduceCommand extends BaseCommand<typeof OpenAPIRedu
6161

6262
async run() {
6363
const opts = this.flags;
64-
const { workingDirectory } = opts;
65-
66-
if (workingDirectory) {
67-
const previousWorkingDirectory = process.cwd();
68-
process.chdir(workingDirectory);
69-
this.debug(`switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
70-
}
7164

7265
const { preparedSpec, specPath, specType } = await prepareOas.call(this);
7366
const parsedPreparedSpec: OASDocument = JSON.parse(preparedSpec);

src/commands/openapi/resolve.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class OpenAPIResolveCommand extends BaseCommand<typeof OpenAPIRes
5353
static flags = {
5454
out: Flags.string({ description: 'Output file path to write resolved file to' }),
5555
title: titleFlag,
56-
workingDirectory: workingDirectoryFlag,
56+
'working-directory': workingDirectoryFlag,
5757
};
5858

5959
/**
@@ -345,13 +345,7 @@ export default class OpenAPIResolveCommand extends BaseCommand<typeof OpenAPIRes
345345
}
346346

347347
async run() {
348-
const { out, workingDirectory } = this.flags;
349-
350-
if (workingDirectory) {
351-
const previousWorkingDirectory = process.cwd();
352-
process.chdir(workingDirectory);
353-
this.debug(`Switching working directory from ${previousWorkingDirectory} to ${process.cwd()}`);
354-
}
348+
const { out } = this.flags;
355349

356350
const { preparedSpec, specPath, specType } = await prepareOas.call(this);
357351
if (specType !== 'OpenAPI') {

src/commands/openapi/upload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import prompts from 'prompts';
1414
import slugify from 'slugify';
1515

1616
import BaseCommand from '../../lib/baseCommand.js';
17-
import { branchFlag, keyFlag, specArg, titleFlag } from '../../lib/flags.js';
17+
import { branchFlag, keyFlag, specArg, titleFlag, workingDirectoryFlag } from '../../lib/flags.js';
1818
import isCI, { isTest } from '../../lib/isCI.js';
1919
import { oraOptions } from '../../lib/logger.js';
2020
import prepareOas from '../../lib/prepareOas.js';
@@ -66,6 +66,7 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
6666
'If included, use the version specified in the `info.version` field in your OpenAPI definition for your ReadMe project version. This flag is mutually exclusive with `--branch`.',
6767
exclusive: ['branch'],
6868
}),
69+
'working-directory': workingDirectoryFlag,
6970
};
7071

7172
static examples = [

0 commit comments

Comments
 (0)