File size: 1,211 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const TRAILING_TIMESTAMP_PATTERN = /^\d+$/;

const getCommandKindLabel = (className: string): string => {
  if (className.endsWith('FastInstanceCommand')) {
    return '(instance fast)';
  }

  if (className.endsWith('SlowInstanceCommand')) {
    return '(instance slow)';
  }

  return '(workspace)';
};

const stripCommandSuffix = (className: string): string =>
  className
    .replace(/FastInstanceCommand$/, '')
    .replace(/SlowInstanceCommand$/, '')
    .replace(/Command$/, '');

export const formatUpgradeCommandName = (commandName: string): string => {
  const commandNameParts = commandName.split('_');

  if (commandNameParts.length < 3) {
    return commandName;
  }

  const version = commandNameParts[0];
  const lastPart = commandNameParts[commandNameParts.length - 1];
  const hasTrailingTimestamp = TRAILING_TIMESTAMP_PATTERN.test(lastPart);

  const className = commandNameParts.slice(1, -1).join('_');
  const friendlyCommandName = stripCommandSuffix(className);
  const kindLabel = getCommandKindLabel(className);

  if (hasTrailingTimestamp) {
    return `${friendlyCommandName} ${lastPart} (${version}) ${kindLabel}`;
  }

  return `${friendlyCommandName} (${version}) ${kindLabel}`;
};