Skip to content

Commit

Permalink
feat(versions): flag parity with API, copy fixes (#906)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this flips the `isPublic` flag to `hidden`.
  • Loading branch information
kanadgupta authored Sep 15, 2023
1 parent 3944722 commit d424d9f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ If you wish to automate the process of creating a new project version, and not h
For example, the following command contains all the flags to bypass the CLI prompts:

```sh
rdme versions:create <version> --fork={version-fork} --main={true|false} --beta={true|false} --deprecated={true|false} --isPublic={true|false}
rdme versions:create <version> --fork={version-fork} --main={true|false} --beta={true|false} --deprecated={true|false} --hidden={true|false}
```

See `rdme versions:create --help` for a full list of flags.
Expand Down
15 changes: 8 additions & 7 deletions __tests__/cmds/versions/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('rdme versions:create', () => {
});

it('should create a specific version', async () => {
prompts.inject([version, false, true, true]);
prompts.inject([version, false, true, true, false]);
const newVersion = '1.0.1';

const mockRequest = getAPIMock()
Expand All @@ -60,7 +60,8 @@ describe('rdme versions:create', () => {
is_stable: false,
is_beta: true,
from: '1.0.0',
is_hidden: false,
is_hidden: true,
is_deprecated: false,
})
.basicAuth({ user: key })
.reply(201, { version: newVersion });
Expand Down Expand Up @@ -96,7 +97,7 @@ describe('rdme versions:create', () => {
deprecated: 'false',
main: 'false',
codename: 'test',
isPublic: 'true',
hidden: 'false',
}),
).resolves.toBe(`Version ${newVersion} created successfully.`);

Expand All @@ -123,7 +124,7 @@ describe('rdme versions:create', () => {
fork: version,
beta: 'false',
main: 'true',
isPublic: 'false',
hidden: 'true',
deprecated: 'true',
}),
).resolves.toBe(`Version ${newVersion} created successfully.`);
Expand Down Expand Up @@ -174,7 +175,7 @@ describe('rdme versions:create', () => {
).rejects.toStrictEqual(new Error("Invalid option passed for 'deprecated'. Must be 'true' or 'false'."));
});

it('should throw if non-boolean `isPublic` flag is passed', () => {
it('should throw if non-boolean `hidden` flag is passed', () => {
const newVersion = '1.0.1';

return expect(
Expand All @@ -183,9 +184,9 @@ describe('rdme versions:create', () => {
version: newVersion,
fork: version,
// @ts-expect-error deliberately passing a bad value here
isPublic: 'test',
hidden: 'test',
}),
).rejects.toStrictEqual(new Error("Invalid option passed for 'isPublic'. Must be 'true' or 'false'."));
).rejects.toStrictEqual(new Error("Invalid option passed for 'hidden'. Must be 'true' or 'false'."));
});

it('should throw if non-boolean `main` flag is passed', () => {
Expand Down
35 changes: 15 additions & 20 deletions __tests__/cmds/versions/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('rdme versions:update', () => {
it('should update a specific version object', async () => {
const versionToChange = '1.1.0';
const renamedVersion = '1.1.0-update';
prompts.inject([versionToChange, renamedVersion, false, true, true, false]);
prompts.inject([versionToChange, renamedVersion, false, true, false, false]);

const updatedVersionObject = {
version: renamedVersion,
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('rdme versions:update', () => {
beta: 'true',
main: 'false',
codename: 'updated-test',
isPublic: 'true',
hidden: 'false',
}),
).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
Expand All @@ -111,7 +111,7 @@ describe('rdme versions:update', () => {
version: renamedVersion,
is_beta: false,
is_deprecated: false,
is_hidden: false,
is_hidden: true,
is_stable: false,
};

Expand All @@ -135,7 +135,7 @@ describe('rdme versions:update', () => {
deprecated: 'false',
main: 'false',
codename: 'updated-test',
isPublic: 'true',
hidden: 'true',
}),
).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('rdme versions:update', () => {
newVersion: renamedVersion,
main: 'false',
codename: 'updated-test',
isPublic: 'true',
hidden: 'false',
}),
).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
Expand Down Expand Up @@ -210,7 +210,7 @@ describe('rdme versions:update', () => {
beta: 'false',
main: 'false',
codename: 'updated-test',
isPublic: 'true',
hidden: 'false',
}),
).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
Expand Down Expand Up @@ -245,33 +245,28 @@ describe('rdme versions:update', () => {
deprecated: 'true',
beta: 'false',
main: 'true',
isPublic: 'false',
hidden: 'true',
}),
).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
});

// Note: this test is a bit bizarre since the flag management
// in our version commands is really confusing to follow.
// I'm not sure if it's technically possible to demote a stable version
// with our current prompt/flag management flow, but that's not
// really the purpose of this test so I think it's fine as is.
it('should catch any put request errors', async () => {
const renamedVersion = '1.0.0-update';

const updatedVersionObject = {
version: renamedVersion,
is_beta: true,
is_deprecated: true,
is_hidden: true,
is_hidden: false,
is_stable: false,
};

prompts.inject([renamedVersion, true, false, true]);
prompts.inject([renamedVersion, false, true, false, true]);

const errorResponse = {
error: 'VERSION_CANT_DEMOTE_STABLE',
message: "You can't make a stable version non-stable",
error: 'VERSION_DUPLICATE',
message: 'The version already exists.',
suggestion: '...a suggestion to resolve the issue...',
help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".',
};
Expand All @@ -287,7 +282,7 @@ describe('rdme versions:update', () => {
.basicAuth({ user: key })
.reply(400, errorResponse);

await expect(updateVersion.run({ key, version, main: 'false' })).rejects.toStrictEqual(new APIError(errorResponse));
await expect(updateVersion.run({ key, version })).rejects.toStrictEqual(new APIError(errorResponse));
mockRequest.done();
});

Expand Down Expand Up @@ -336,7 +331,7 @@ describe('rdme versions:update', () => {
mockRequest.done();
});

it('should throw if non-boolean `isPublic` flag is passed', async () => {
it('should throw if non-boolean `hidden` flag is passed', async () => {
const versionToChange = '1.1.0';

const mockRequest = getAPIMock()
Expand All @@ -352,9 +347,9 @@ describe('rdme versions:update', () => {
key,
version: versionToChange,
// @ts-expect-error deliberately passing a bad value here
isPublic: 'hi',
hidden: 'hi',
}),
).rejects.toStrictEqual(new Error("Invalid option passed for 'isPublic'. Must be 'true' or 'false'."));
).rejects.toStrictEqual(new Error("Invalid option passed for 'hidden'. Must be 'true' or 'false'."));
mockRequest.done();
});

Expand Down
2 changes: 1 addition & 1 deletion __tests__/lib/prompts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('prompt test bed', () => {
newVersion: '1.2.1',
is_stable: false,
is_beta: true,
is_public: true,
is_hidden: true,
is_deprecated: false,
});
});
Expand Down
13 changes: 5 additions & 8 deletions src/cmds/versions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface CommonOptions {
beta?: 'true' | 'false';
codename?: string;
deprecated?: 'true' | 'false';
isPublic?: 'true' | 'false';
hidden?: 'true' | 'false';
main?: 'true' | 'false';
}

Expand Down Expand Up @@ -49,7 +49,7 @@ export default class CreateVersionCommand extends Command {
await super.run(opts);

let versionList;
const { key, version, fork, codename, main, beta, deprecated, isPublic } = opts;
const { key, version, fork, codename, main, beta, deprecated, hidden } = opts;

if (!version || !semver.valid(semver.coerce(version))) {
return Promise.reject(
Expand All @@ -64,26 +64,23 @@ export default class CreateVersionCommand extends Command {
}).then(handleRes);
}

const versionPrompt = promptHandler.versionPrompt(versionList || []);

prompts.override({
from: fork,
is_beta: castStringOptToBool(beta, 'beta'),
is_deprecated: castStringOptToBool(deprecated, 'deprecated'),
is_public: castStringOptToBool(isPublic, 'isPublic'),
is_hidden: castStringOptToBool(hidden, 'hidden'),
is_stable: castStringOptToBool(main, 'main'),
});

const promptResponse = await promptTerminal(versionPrompt);
const promptResponse = await promptTerminal(promptHandler.versionPrompt(versionList || []));

const body: Version = {
codename,
version,
from: promptResponse.from,
is_beta: promptResponse.is_beta,
is_deprecated: promptResponse.is_deprecated,
// if the "is public" question was never asked, we should omit that from the payload
is_hidden: typeof promptResponse.is_public === 'undefined' ? undefined : !promptResponse.is_public,
is_hidden: promptResponse.is_hidden,
is_stable: promptResponse.is_stable,
};

Expand Down
9 changes: 4 additions & 5 deletions src/cmds/versions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class UpdateVersionCommand extends Command {
async run(opts: AuthenticatedCommandOptions<Options>) {
await super.run(opts);

const { key, version, newVersion, codename, main, beta, isPublic, deprecated } = opts;
const { key, version, newVersion, codename, main, beta, hidden, deprecated } = opts;

const selectedVersion = await getProjectVersion(version, key);

Expand All @@ -56,7 +56,7 @@ export default class UpdateVersionCommand extends Command {
prompts.override({
is_beta: castStringOptToBool(beta, 'beta'),
is_deprecated: castStringOptToBool(deprecated, 'deprecated'),
is_public: castStringOptToBool(isPublic, 'isPublic'),
is_hidden: castStringOptToBool(hidden, 'hidden'),
is_stable: castStringOptToBool(main, 'main'),
newVersion,
});
Expand All @@ -65,12 +65,11 @@ export default class UpdateVersionCommand extends Command {

const body: Version = {
codename,
// fall back to current version if user didn't enter one
// fallback to existing version if user was prompted to rename the version but didn't enter anything
version: promptResponse.newVersion || version,
is_beta: promptResponse.is_beta,
is_deprecated: promptResponse.is_deprecated,
// if the "is public" question was never asked, we should omit that from the payload
is_hidden: typeof promptResponse.is_public === 'undefined' ? undefined : !promptResponse.is_public,
is_hidden: promptResponse.is_hidden,
is_stable: promptResponse.is_stable,
};

Expand Down
13 changes: 6 additions & 7 deletions src/lib/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,23 @@ export default class Command {
{
name: 'main',
type: String,
description:
"Should this version be the primary (default) version for your project? (Must be 'true' or 'false')",
description: "Should this be the main version for your project? (Must be 'true' or 'false')",
},
{
name: 'beta',
type: String,
description: "Is this version in beta? (Must be 'true' or 'false')",
description: "Should this version be in beta? (Must be 'true' or 'false')",
},
{
name: 'deprecated',
type: String,
description: "Would you like to deprecate this version? (Must be 'true' or 'false')",
description:
"Should this version be deprecated? The main version cannot be deprecated. (Must be 'true' or 'false')",
},
{
name: 'isPublic',
name: 'hidden',
type: String,
description:
"Would you like to make this version public? Any primary version must be public. (Must be 'true' or 'false')",
description: "Should this version be hidden? The main version cannot be hidden. (Must be 'true' or 'false')",
},
];
}
Expand Down
20 changes: 10 additions & 10 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ export function versionPrompt(
/** list of versions, used for prompt about which version to fork */
versionList: Version[],
/** existing version if we're performing an update */
isUpdate?: {
existingVersion?: {
is_stable: boolean;
},
): PromptObject[] {
return [
{
// only runs for versions:create command
type: isUpdate ? null : 'select',
type: existingVersion ? null : 'select',
name: 'from',
message: 'Which version would you like to fork from?',
choices: versionList.map(v => {
Expand All @@ -158,7 +158,7 @@ export function versionPrompt(
},
{
// only runs for versions:update command
type: !isUpdate ? null : 'text',
type: !existingVersion ? null : 'text',
name: 'newVersion',
message: 'What should the version be renamed to?',
hint: '1.0.0',
Expand All @@ -169,11 +169,11 @@ export function versionPrompt(
},
},
{
// if the existing version being updated is already the main version,
// we can't switch that so we skip this question
type: isUpdate?.is_stable ? null : 'confirm',
// if the user is already updating the main version
// we skip this question since it must remain the main version
type: existingVersion?.is_stable ? null : 'confirm',
name: 'is_stable',
message: 'Would you like to make this version the main version for this project?',
message: 'Should this be the main version for your project?',
},
{
type: 'confirm',
Expand All @@ -186,8 +186,8 @@ export function versionPrompt(
// it can't also be hidden.
return values.is_stable ? null : 'confirm';
},
name: 'is_public',
message: 'Would you like to make this version public?',
name: 'is_hidden',
message: 'Should this version be hidden?',
},
{
type: (prev, values) => {
Expand All @@ -196,7 +196,7 @@ export function versionPrompt(
return values.is_stable ? null : 'confirm';
},
name: 'is_deprecated',
message: 'Would you like to deprecate this version?',
message: 'Should this version be deprecated?',
},
];
}

0 comments on commit d424d9f

Please sign in to comment.