Skip to content

Commit

Permalink
Merge next
Browse files Browse the repository at this point in the history
  • Loading branch information
kemmerle committed Nov 22, 2024
2 parents 1ad3821 + eefb1e0 commit 55dd555
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 68 deletions.
9 changes: 4 additions & 5 deletions acceptance-tests/tests/workflows/secretsFlow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ describe('Secrets Flow', () => {

describe('hs secrets delete', () => {
it('should delete the secret', async () => {
await testState.cli.executeWithTestConfig([
'secrets',
'delete',
SECRET.name,
]);
await testState.cli.executeWithTestConfig(
['secrets', 'delete', SECRET.name],
['Y', ENTER]
);
});
});

Expand Down
5 changes: 1 addition & 4 deletions commands/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-nocheck
const { addConfigOptions, addAccountOptions } = require('../lib/commonOpts');

const addSecretCommand = require('./secrets/addSecret');
const listSecretsCommand = require('./secrets/listSecrets');
Expand All @@ -9,12 +8,10 @@ const { i18n } = require('../lib/lang');

const i18nKey = 'commands.secrets';

exports.command = 'secrets';
exports.command = ['secret', 'secrets'];
exports.describe = i18n(`${i18nKey}.describe`);

exports.builder = yargs => {
addConfigOptions(yargs);
addAccountOptions(yargs);
yargs
.command(listSecretsCommand)
.command(addSecretCommand)
Expand Down
33 changes: 30 additions & 3 deletions commands/secrets/addSecret.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// @ts-nocheck
import { EXIT_CODES } from '../../lib/enums/exitCodes';
import { fetchSecrets } from '@hubspot/local-dev-lib/api/secrets';
import { uiCommandReference } from '../../lib/ui';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { logError, ApiErrorContext } = require('../../lib/errorHandlers/index');
const { addSecret } = require('@hubspot/local-dev-lib/api/secrets');
Expand All @@ -12,22 +16,45 @@ const {
addUseEnvironmentOptions,
} = require('../../lib/commonOpts');
const { uiAccountDescription } = require('../../lib/ui');
const { secretValuePrompt } = require('../../lib/prompts/secretPrompt');
const {
secretValuePrompt,
secretNamePrompt,
} = require('../../lib/prompts/secretPrompt');
const { i18n } = require('../../lib/lang');

const i18nKey = 'commands.secrets.subcommands.add';

exports.command = 'add <name>';
exports.command = 'add [name]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { name: secretName, derivedAccountId } = options;
const { name, derivedAccountId } = options;
let secretName = name;

await loadAndValidateOptions(options);

trackCommandUsage('secrets-add', null, derivedAccountId);

try {
if (!secretName) {
const { secretName: name } = await secretNamePrompt();
secretName = name;
}

const {
data: { results: secrets },
} = await fetchSecrets(derivedAccountId);

if (secrets.includes(secretName)) {
logger.error(
i18n(`${i18nKey}.errors.alreadyExists`, {
secretName,
command: uiCommandReference('hs secret update'),
})
);
process.exit(EXIT_CODES.ERROR);
}

const { secretValue } = await secretValuePrompt();

await addSecret(derivedAccountId, secretName, secretValue);
Expand Down
55 changes: 48 additions & 7 deletions commands/secrets/deleteSecret.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// @ts-nocheck
import { secretListPrompt } from '../../lib/prompts/secretPrompt';
import { confirmPrompt } from '../../lib/prompts/promptUtils';
import { EXIT_CODES } from '../../lib/enums/exitCodes';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { ApiErrorContext, logError } = require('../../lib/errorHandlers/index');
const { deleteSecret } = require('@hubspot/local-dev-lib/api/secrets');
const {
deleteSecret,
fetchSecrets,
} = require('@hubspot/local-dev-lib/api/secrets');

const { loadAndValidateOptions } = require('../../lib/validation');
const { trackCommandUsage } = require('../../lib/usageTracking');
Expand All @@ -16,17 +23,46 @@ const { i18n } = require('../../lib/lang');

const i18nKey = 'commands.secrets.subcommands.delete';

exports.command = 'delete <name>';
exports.command = 'delete [name]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { name: secretName, derivedAccountId } = options;
const { name, derivedAccountId, force } = options;
let secretName = name;

await loadAndValidateOptions(options);

trackCommandUsage('secrets-delete', null, derivedAccountId);

try {
const {
data: { results: secrets },
} = await fetchSecrets(derivedAccountId);

if (secretName && !secrets.includes(secretName)) {
logger.error(i18n(`${i18nKey}.errors.noSecret`, { secretName }));
process.exit(EXIT_CODES.ERROR);
}

if (!secretName) {
const { secretToModify } = await secretListPrompt(
secrets,
i18n(`${i18nKey}.selectSecret`)
);
secretName = secretToModify;
}

const confirmDelete =
force ||
(await confirmPrompt(i18n(`${i18nKey}.confirmDelete`, { secretName }), {
defaultAnswer: false,
}));

if (!confirmDelete) {
logger.success(i18n(`${i18nKey}.deleteCanceled`));
process.exit(EXIT_CODES.SUCCESS);
}

await deleteSecret(derivedAccountId, secretName);
logger.success(
i18n(`${i18nKey}.success.delete`, {
Expand Down Expand Up @@ -54,9 +90,14 @@ exports.builder = yargs => {
addConfigOptions(yargs);
addAccountOptions(yargs);
addUseEnvironmentOptions(yargs);
yargs.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
});
yargs
.positional('name', {
describe: i18n(`${i18nKey}.positionals.name.describe`),
type: 'string',
})
.options('force', {
describe: 'Force the deletion',
type: 'boolean',
});
return yargs;
};
34 changes: 30 additions & 4 deletions commands/secrets/updateSecret.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// @ts-nocheck
import { EXIT_CODES } from '../../lib/enums/exitCodes';

const { logger } = require('@hubspot/local-dev-lib/logger');
const { ApiErrorContext, logError } = require('../../lib/errorHandlers/index');
const { updateSecret } = require('@hubspot/local-dev-lib/api/secrets');
const {
updateSecret,
fetchSecrets,
} = require('@hubspot/local-dev-lib/api/secrets');

const { loadAndValidateOptions } = require('../../lib/validation');
const { trackCommandUsage } = require('../../lib/usageTracking');
Expand All @@ -12,22 +17,43 @@ const {
addAccountOptions,
addUseEnvironmentOptions,
} = require('../../lib/commonOpts');
const { secretValuePrompt } = require('../../lib/prompts/secretPrompt');
const {
secretValuePrompt,
secretListPrompt,
} = require('../../lib/prompts/secretPrompt');
const { i18n } = require('../../lib/lang');

const i18nKey = 'commands.secrets.subcommands.update';

exports.command = 'update <name>';
exports.command = 'update [name]';
exports.describe = i18n(`${i18nKey}.describe`);

exports.handler = async options => {
const { name: secretName, derivedAccountId } = options;
const { name, derivedAccountId } = options;

let secretName = name;
await loadAndValidateOptions(options);

trackCommandUsage('secrets-update', null, derivedAccountId);

try {
const {
data: { results: secrets },
} = await fetchSecrets(derivedAccountId);

if (secretName && !secrets.includes(secretName)) {
logger.error(i18n(`${i18nKey}.errors.noSecret`, { secretName }));
process.exit(EXIT_CODES.ERROR);
}

if (!secretName) {
const { secretToModify } = await secretListPrompt(
secrets,
i18n(`${i18nKey}.selectSecret`)
);
secretName = secretToModify;
}

const { secretValue } = await secretValuePrompt();

await updateSecret(derivedAccountId, secretName, secretValue);
Expand Down
63 changes: 48 additions & 15 deletions commands/theme/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ const determineSrcAndDest = async options => {

exports.handler = async options => {
const {
derivedAccountId,
notify,
skipUpload,
noSsl,
port,
debug,
derivedAccountId,
resetSession,
port,
generateFieldsTypes,
} = options;

await loadAndValidateOptions(options);
Expand Down Expand Up @@ -181,17 +180,51 @@ exports.handler = async options => {

trackCommandUsage('preview', derivedAccountId);

preview(derivedAccountId, absoluteSrc, dest, {
notify,
filePaths,
skipUpload,
noSsl,
port,
debug,
resetSession,
startProgressBar,
handleUserInput,
});
let createUnifiedDevServer;
try {
require.resolve('@hubspot/cms-dev-server');
const { createDevServer } = await import('@hubspot/cms-dev-server');
createUnifiedDevServer = createDevServer;
} catch (e) {
logger.warn(
'Unified dev server requires node 20 to run. Defaulting to legacy preview.'
);
}

const isUngatedForUnified = await hasFeature(
derivedAccountId,
'cms:react:unifiedThemePreview'
);
if (isUngatedForUnified && createUnifiedDevServer) {
if (port) {
process.env['PORT'] = port;
}
createUnifiedDevServer(
absoluteSrc,
false,
false,
false,
!noSsl,
generateFieldsTypes,
{
filePaths,
resetSession,
startProgressBar,
handleUserInput,
dest,
}
);
} else {
preview(derivedAccountId, absoluteSrc, dest, {
notify,
filePaths,
noSsl,
port,
resetSession,
startProgressBar,
handleUserInput,
});
}
};

exports.builder = yargs => {
Expand Down
20 changes: 15 additions & 5 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -804,35 +804,42 @@ en:
account:
describe: "Account name or id to delete"
secrets:
describe: "Manage HubSpot secrets."
describe: "Commands for managing secrets."
subcommands:
add:
describe: "Add a HubSpot secret"
describe: "Create a new secret."
errors:
add: "The secret \"{{ secretName }}\" was not added"
alreadyExists: "The secret \"{{ secretName }}\" already exists, it's value can be modified with {{ command }}"
positionals:
name:
describe: "Name of the secret"
success:
add: "The secret \"{{ secretName }}\" was added to the HubSpot account: {{ accountIdentifier }}"
delete:
describe: "Delete a HubSpot secret"
describe: "Delete a secret."
selectSecret: "Select the secret you want to delete"
deleteCanceled: "Delete canceled"
confirmDelete: "Are you sure you want to delete the secret \"{{ secretName }}\"?"
errors:
delete: "The secret \"{{ secretName }}\" was not deleted"
noSecret: "Unable to delete secret with name \"{{ secretName }}\", it does not exist"
positionals:
name:
describe: "Name of the secret"
success:
delete: "The secret \"{{ secretName }}\" was deleted from the HubSpot account: {{ accountIdentifier }}"
list:
describe: "List all HubSpot secrets"
describe: "List all secrets."
errors:
list: "The secrets could not be listed"
groupLabel: "Secrets for account {{ accountIdentifier }}:"
update:
describe: "Update an existing HubSpot secret"
describe: "Update an existing secret."
selectSecret: "Select the secret you want to update"
errors:
update: "The secret \"{{ secretName }}\" was not updated"
noSecret: "Unable to update secret with name \"{{ secretName }}\", it does not exist"
positionals:
name:
describe: "Name of the secret to be updated"
Expand Down Expand Up @@ -1280,6 +1287,9 @@ en:
invalidType: "[--type] Could not find type {{ type }}. Please choose an available type."
secretPrompt:
enterValue: "Enter a value for your secret: "
enterName: "Enter a name for your secret: "
selectSecretUpdate: "Select the secret you want to update"
selectSecretDelete: "Select the secret you want to delete"
errors:
invalidValue: "You entered an invalid value. Please try again."
sandboxesPrompt:
Expand Down
4 changes: 2 additions & 2 deletions lib/prompts/promptUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export async function confirmPrompt(
message: string,
options: { defaultAnswer?: boolean; when?: boolean | (() => boolean) } = {}
): Promise<boolean> {
const { defaultAnswer, when } = options;
const { defaultAnswer = true, when } = options;
const { choice } = await promptUser([
{
name: 'choice',
type: 'confirm',
message,
default: defaultAnswer || true,
default: defaultAnswer,
when,
},
]);
Expand Down
Loading

0 comments on commit 55dd555

Please sign in to comment.