Skip to content
Merged
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
29 changes: 20 additions & 9 deletions packages/contentstack/src/hooks/init/opt-in-plugin-guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ import { cliux } from '@contentstack/cli-utilities';

/**
* Commands whose plugins used to ship bundled with the CLI but are now opt-in.
* Keyed by oclif topic (the segment before the first `:` in a command id).
*
* `match` tests the full command id rather than just the oclif topic (the segment
* before the first `:`) because not every demoted plugin owns its topic: launch's
* commands all share the `launch` topic, but migrate-rte's only command is
* `cm:entries:migrate-html-rte` — topic `cm`, which is also used by dozens of
* unrelated commands (`cm:stacks:export`, etc.), so it can't be keyed by topic.
*/
const DEMOTED_PLUGINS: Record<string, string> = {
launch: '@contentstack/cli-launch',
};
const DEMOTED_PLUGINS: Array<{ match: (id: string) => boolean; label: string; pluginName: string }> = [
{ match: (id) => id.split(':')[0] === 'launch', label: 'launch', pluginName: '@contentstack/cli-launch' },
{
match: (id) => id === 'cm:entries:migrate-html-rte',
label: 'cm:entries:migrate-html-rte',
pluginName: '@contentstack/cli-cm-migrate-rte',
},
];

/**
* When a user runs a command belonging to a demoted (now opt-in) plugin that
Expand All @@ -18,18 +28,19 @@ const DEMOTED_PLUGINS: Record<string, string> = {
const hook: Hook<'init'> = async function (opts): Promise<void> {
if (!opts.id) return;

const topic = opts.id.split(':')[0];
const pluginName = DEMOTED_PLUGINS[topic];
if (!pluginName) return;
const demoted = DEMOTED_PLUGINS.find(({ match }) => match(opts.id as string));
if (!demoted) return;

const { label, pluginName } = demoted;

// If the plugin is already installed, let normal command resolution proceed.
if (this.config.plugins.has(pluginName)) return;

cliux.print(
`\nWarning: "${topic}" is now an opt-in plugin and is not installed, so "${topic}:*" commands are unavailable.`,
`\nWarning: "${label}" is now an opt-in plugin and is not installed, so this command is unavailable.`,
{ color: 'yellow' },
);
cliux.print('\nInstall it to enable these commands:', { color: 'yellow' });
cliux.print('\nInstall it to enable this command:', { color: 'yellow' });
cliux.print(` csdx plugins:install ${pluginName}\n`, { color: 'green' });

this.exit(127);
Expand Down
Loading