Skip to content
Merged
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
20 changes: 10 additions & 10 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fileignoreconfig:
allowed_patterns:
- "assertArray\\(response, key, `\\$\\{key\\} response`\\)"
- filename: src/environments/environment.inputs.ts
checksum: fd3a8f925444e44b0477d7c907d77526d5a4315d627bf29768912b6799a45c80
checksum: aad346ce288f2f72c7e88c3cea21e9bd6939dfc3c76d1cf997688cd31fcbf460
ignore_detectors:
- filename
- filename: src/environments/environment.builder.ts
Expand All @@ -127,11 +127,11 @@ fileignoreconfig:
- filename
checksum: 8b7e3207630f19170dd08ca639ebca8af6bc0d70db633b98e66eedde6d2ea641
- filename: src/environments/environment.update.ts
checksum: 85b625c571ef1a4e0c177b78fe5538f6c9506c4b692c6212d9faa63681d77ffe
checksum: 3c667fee5d8c93de351ab117527acd9170dcb0444228022275fe12849fd70ede
ignore_detectors:
- filename
- filename: src/environments/environment.update.test.ts
checksum: 451f42cd81d25088ec05f5ededc9375bfcac7cda6a29db60ccda792768afdd0e
checksum: 7e5aa33a6958e9141a2f7215df87dfc8111b9bba03edc5a84407f42601b7213d
ignore_detectors:
- filename
- filename: src/environments/environment.create.ts
Expand All @@ -145,19 +145,19 @@ fileignoreconfig:
ignore_detectors:
- filename
- filename: src/environments/environment.update.prompt.ts
checksum: 992d52bfb924105691c90fbdee53ca66fc0a585bb3273e7bcb23974ec2ef4c5c
checksum: 0c93fdf5d542a9ef2e1655a4591d5cea8f0fd5e404d204a999c4d5b6a292e895
ignore_detectors:
- filename
- filename: src/environments/environment.update.prompt.test.ts
checksum: 2be66b5b7a394e0f2440163ee94c46fdd3884e594673b03de9d8c2eef0ca1ed0
checksum: 6130ccd6446fe9dba5736fc53b8ebb8f02dbd3956c68ae2c4aa24e3a22dbe105
ignore_detectors:
- filename
- filename: src/environments/environment.presenter.ts
checksum: 3808d26ca5eb7f345568b56fe48f5756e14f0cdb15ef68b2ae72e9556bc6bab2
checksum: 505f5e76e34ab9081cb0d57d6c017799bf0c7d86a08bb77201c6737a1affbaef
ignore_detectors:
- filename
- filename: src/environments/environment.presenter.test.ts
checksum: 3e28f6188a450b47d874453a37e22193d9e5036762d34f55315118ed54a73c0a
checksum: 9120a7bc0ce7e7307be8bc76e5e7133f13c0ed186bcb932de9e9fba364a30c99
ignore_detectors:
- filename
- filename: src/environments/environment.resolver.ts
Expand Down Expand Up @@ -313,7 +313,7 @@ fileignoreconfig:
- filename: test/credential-guard.setup.ts
checksum: 519ccbeb0552fdb1d5f27308c56477c0258ee6cc0adec8f618e8e5bc047cbf6d
- filename: src/environments/types.ts
checksum: d5506fb4ea67ef8cac77b43724ad6b0e438503d7198d68db8fb2f6d718e89f00
checksum: c45a2ed513e295ef56da5a6a014e31cb7de3612fc699a3d3b157e525843e8303
- filename: src/commands/launch/env/get.test.ts
checksum: 6b79822734553c7417c0a8774fae221609d008be22834113661c9925f17017b9
- filename: test/integration/environments-delete.test.ts
Expand All @@ -327,13 +327,13 @@ fileignoreconfig:
- filename: src/core/prompt.test.ts
checksum: 7be5497acf4cee870667751d44aa230e309784df76dc9e95d5ec2c4e6e6a57e0
- filename: test/integration/environments-update.test.ts
checksum: 249f27ad679b68f40eac3af90c616229838bceffad57ba508dd298ab053fa51a
checksum: 312cbf02b41fd9fbfe2c20d78a92129f370753cc7172f0ecc244be3e3dd8eae7
- filename: src/commands/launch/env/update.ts
checksum: 981c3267005bca0453131b532c9b62ca501bc67cfd25511c261e99082677882a
- filename: src/commands/launch/env/update.test.ts
checksum: 82c76af721bfdfd8eb7cf49494ffbe15d98b988e98bca1015b9d34b42b5226bd
- filename: README.md
checksum: 6386fbf38075484d6c0eb32bb40ab246a1c94dff41a107e14713f944b4f1d2b5
checksum: 3ec134160b8ba10ad28c9fe0dc7f2b264461033a1d62c0d7d466a1c84d347000
allowed_patterns:
- "password-protection"
- "-p, --password"
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ Create an environment in a Launch project and follow its first deployment
```
USAGE
$ csdx launch:env:create [--config <value>] [-d <value>] [--org <value>] [--project <value>] [--name <value>]
[--branch <value>] [--framework <value>] [--build-cmd <value>] [--server-cmd <value>]
[--output-dir <value>] [--res-mode <value>] [--auto-deploy <value>] [--cs-auth <value>] [--var <value>...] [--dotenv
<value>...] [--from-stack] [-a <value>]
[--branch <value>] [--framework <value>] [--build-cmd <value>] [--server-cmd <value>] [--output-dir <value>]
[--res-mode <value>] [--auto-deploy <value>] [--cs-auth <value>] [--var <value>...] [--dotenv <value>...]
[--from-stack] [-a <value>]

FLAGS
-a, --alias=<value> Alias of a delivery token saved with csdx auth:tokens:add, used with --from-stack without a
Expand Down Expand Up @@ -439,9 +439,9 @@ Update an environment of a Launch project
```
USAGE
$ csdx launch:env:update [--config <value>] [-d <value>] [--org <value>] [--project <value>] [--env <value>] [--name
<value>] [--branch <value>] [--framework <value>] [--build-cmd <value>] [--server-cmd
<value>] [--output-dir <value>] [--res-mode <value>] [--auto-deploy <value>] [--cs-auth <value>] [--cache-priming
<value>] [--password-protection <value>] [-u <value>] [-p] [--yes]
<value>] [--branch <value>] [--framework <value>] [--build-cmd <value>] [--server-cmd <value>] [--output-dir
<value>] [--res-mode <value>] [--auto-deploy <value>] [--cs-auth <value>] [--cache-priming <value>]
[--password-protection <value>] [-u <value>] [-p] [--yes]

FLAGS
-d, --data-dir=<value> Current working directory
Expand Down
16 changes: 16 additions & 0 deletions src/environments/environment.inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export const TOGGLE_VALUES = ['enable', 'disable'] as const;

export type ToggleValue = (typeof TOGGLE_VALUES)[number];

export function toggleOf(value: boolean | null | undefined): ToggleValue | undefined {
Comment thread
SakshiKoli-CS marked this conversation as resolved.
Resolved
if (value === undefined || value === null) {
return undefined;
}

return value ? 'enable' : 'disable';
}

export function responseModeOf(streaming: boolean | null | undefined): ResponseMode | undefined {
if (streaming === undefined || streaming === null) {
return undefined;
}

return streaming ? 'streaming' : 'buffered';
}

export function frameworkPresetOf(value: string): FrameworkPreset {
return FRAMEWORK_PRESET_BY_LABEL[oneOf('framework', value, FRAMEWORK_CHOICES).toLowerCase()];
}
Expand Down
35 changes: 35 additions & 0 deletions src/environments/environment.presenter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ENVIRONMENT_VALUE_COLUMN,
environmentColumns,
environmentDeletedLine,
environmentAlreadyMatchesLine,
environmentDetailRows,
environmentNoChangesLine,
environmentNotDeletedLine,
Expand Down Expand Up @@ -94,6 +95,23 @@ describe('environmentDetailRows', () => {
]);
});

it('renders a placeholder, not "disabled", for a toggle or server command the API reports as null', () => {
const rows = environmentDetailRows({
serverCommand: null,
autoDeployOnPush: null,
isCachePrimingEnabled: null,
isContentstackAuthenticationEnabled: null,
});
const valueOf = (field: string) => rows.find((row) => row.field === field)?.value;

expect(['SERVER COMMAND', 'AUTO DEPLOY', 'CACHE PRIMING', 'CONTENTSTACK AUTHENTICATION'].map(valueOf)).toEqual([
'-',
'-',
'-',
'-',
]);
});

it('renders a placeholder for every field an environment arrives without, and a zero variable count', () => {
expect(environmentDetailRows({}).map((row) => row.value)).toEqual([
'-',
Expand Down Expand Up @@ -169,6 +187,23 @@ describe('environment delete wording', () => {
});
});

describe('environmentAlreadyMatchesLine', () => {
it('names one matching flag in the singular, in yellow on a terminal and plain otherwise', () => {
expect(environmentAlreadyMatchesLine(['cs-auth'], true)).toBe(
'\u001b[33mEnvironment not updated: --cs-auth already matches the current settings.\u001b[39m',
);
expect(environmentAlreadyMatchesLine(['cs-auth'], false)).toBe(
'Environment not updated: --cs-auth already matches the current settings.',
);
});

it('names several matching flags in the plural, in the order given', () => {
expect(environmentAlreadyMatchesLine(['cs-auth', 'cache-priming'], false)).toBe(
'Environment not updated: --cs-auth, --cache-priming already match the current settings.',
);
});
});

describe('environmentNoChangesLine', () => {
it('says nothing was changed in yellow on a terminal and plain otherwise', () => {
expect(environmentNoChangesLine(true)).toBe('\u001b[33mEnvironment not updated. No changes were entered.\u001b[39m');
Expand Down
15 changes: 11 additions & 4 deletions src/environments/environment.presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export interface DetailRow {
value: string;
}

function text(value: string | undefined): string {
return value === undefined || isAbsent(value) ? '-' : value;
function text(value: string | null | undefined): string {
return value === undefined || value === null || isAbsent(value) ? '-' : value;
}

function toggle(value: boolean | undefined): string {
if (value === undefined) {
function toggle(value: boolean | null | undefined): string {
if (value === undefined || value === null) {
return '-';
}

Expand Down Expand Up @@ -124,3 +124,10 @@ export function environmentDeploymentFailureMessage(failure: EnvironmentDeployme
export function environmentNoChangesLine(outputIsTTY: boolean): string {
return styled('Environment not updated. No changes were entered.', 'yellow', outputIsTTY);
}

export function environmentAlreadyMatchesLine(flags: readonly string[], outputIsTTY: boolean): string {
const named = flags.map((flag) => `--${flag}`).join(', ');
const verb = flags.length === 1 ? 'matches' : 'match';

return styled(`Environment not updated: ${named} already ${verb} the current settings.`, 'yellow', outputIsTTY);
}
30 changes: 28 additions & 2 deletions src/environments/environment.update.prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,39 @@ describe('promptForEnvironmentUpdate', () => {
expect(asked[1].default).toBe('NextJs');
});

it('asks the response mode and always sends it, since the API never reports the current one', async () => {
it('lists streaming first and starts on it when the API does not report the current mode, sending the answer', async () => {
const { services, asked } = servicesAnswering({ [SETTINGS_QUESTION]: ['resMode'], 'Response mode': 'buffered' });

await expect(promptForEnvironmentUpdate(services, githubContext())).resolves.toEqual({ resMode: 'buffered' });
expect(asked[1].default).toBe('buffered');
expect(asked[1].choices).toEqual([
{ name: 'streaming', value: 'streaming' },
{ name: 'buffered', value: 'buffered' },
]);
expect(asked[1].default).toBe('streaming');
});

it('sends the Contentstack Authentication answer even when it matches the current setting', async () => {
const { services } = servicesAnswering({ [SETTINGS_QUESTION]: ['csAuth'], 'Contentstack Authentication': 'disable' });

await expect(promptForEnvironmentUpdate(services, githubContext())).resolves.toEqual({ csAuth: 'disable' });
});

it.each([
[true, 'streaming', {}],
[true, 'buffered', { resMode: 'buffered' }],
[false, 'buffered', {}],
])(
'defaults the response mode to the current one when the API reports it (streaming %p) and drops an unchanged %s',
async (isStreamingEnabled, answer, expected) => {
const { services, asked } = servicesAnswering({ [SETTINGS_QUESTION]: ['resMode'], 'Response mode': answer });

await expect(
promptForEnvironmentUpdate(services, githubContext({ ...CURRENT, isStreamingEnabled })),
).resolves.toEqual(expected);
expect(asked[1].default).toBe(isStreamingEnabled ? 'streaming' : 'buffered');
},
);

it('skips the server command, saying why, when the framework it would run under takes none', async () => {
const { services, asked, lines } = servicesAnswering({ [SETTINGS_QUESTION]: ['serverCmd'] });

Expand Down
41 changes: 27 additions & 14 deletions src/environments/environment.update.prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { askChoices, askOption, askOptionalText, askText } from '../core/prompt'
import type { ServiceContext } from '../core/service-context';
import { askBranch } from '../git/git.prompt';
import { frameworkLabelOf } from './environment.builder';
import type { ToggleValue } from './environment.inputs';
import {
ENVIRONMENT_NAME_MAX_LENGTH,
RESPONSE_MODES,
TOGGLE_VALUES,
frameworkPresetOf,
responseModeOf,
toggleOf,
} from './environment.inputs';
import type { ProjectRepository } from './environment.rules';
import { gitScopeOf } from './environment.rules';
Expand All @@ -17,6 +18,8 @@ import type { Environment } from './types';

export const SETTINGS_QUESTION = 'Select the fields to update:';

const STREAMING_FIRST = [...RESPONSE_MODES].reverse();

export type EnvironmentChanges = Partial<
Pick<
EnvironmentUpdateRequest,
Expand Down Expand Up @@ -57,14 +60,6 @@ const SETTINGS: { setting: Setting; label: string; repositoryOnly?: boolean }[]
{ setting: 'passwordProtection', label: 'Password protection' },
];

function toggleOf(value: boolean | undefined): ToggleValue | undefined {
if (value === undefined) {
return undefined;
}

return value ? 'enable' : 'disable';
}

function changed<T>(answer: T | undefined, current: T | undefined): T | undefined {
return answer === current ? undefined : answer;
}
Expand Down Expand Up @@ -120,11 +115,11 @@ class SettingAsker {
case 'outputDir':
return this.text(askText(ux, 'Output directory', current.outputDirectory), current.outputDirectory);
case 'resMode':
return askOption(ux, 'Response mode', RESPONSE_MODES.map((value) => ({ name: value, value })), 'buffered');
return this.responseMode();
case 'autoDeploy':
return this.toggle('Auto deploy', current.autoDeployOnPush);
case 'csAuth':
return this.toggle('Contentstack Authentication', current.isContentstackAuthenticationEnabled);
return this.toggle('Contentstack Authentication', current.isContentstackAuthenticationEnabled, true);
case 'cachePriming':
return this.toggle('Cache priming', current.isCachePrimingEnabled);
default:
Expand All @@ -136,6 +131,18 @@ class SettingAsker {
return changed(await answer, current);
}

private async responseMode(): Promise<string | undefined> {
const current = responseModeOf(this.context.current.isStreamingEnabled);
const answer = await askOption(
this.services.ux,
'Response mode',
STREAMING_FIRST.map((value) => ({ name: value, value })),
current ?? 'streaming',
);

return changed(answer, current);
}

private async framework(): Promise<string | undefined> {
const current = this.context.current.frameworkPreset;
const label = await askOption(
Expand All @@ -161,7 +168,9 @@ class SettingAsker {
return undefined;
}

return this.text(askOptionalText(this.services.ux, 'Server command', current.serverCommand), current.serverCommand);
const serverCommand = current.serverCommand ?? undefined;

return this.text(askOptionalText(this.services.ux, 'Server command', serverCommand), serverCommand);
}

private async passwordProtection(current: boolean | undefined): Promise<string | undefined> {
Expand All @@ -170,7 +179,11 @@ class SettingAsker {
return answer ?? (current === true ? 'enable' : undefined);
}

private async toggle(message: string, current: boolean | undefined): Promise<string | undefined> {
private async toggle(
message: string,
current: boolean | null | undefined,
sendUnchanged = false,
): Promise<string | undefined> {
const state = toggleOf(current);
const answer = await askOption(
this.services.ux,
Expand All @@ -179,6 +192,6 @@ class SettingAsker {
state,
);

return changed(answer, state);
return sendUnchanged ? answer : changed(answer, state);
}
}
Loading
Loading