All files / app/assets/javascripts/packages_and_registries/package_registry/components/details npm_installation.vue

100% Statements 14/14
100% Branches 5/5
100% Functions 8/8
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                                  3x                                     18x             15x     17x     4x     6x     19x           19x   19x     23x   23x       23x 17x     6x                                                                                                                                                                              
<script>
import { GlLink, GlSprintf, GlFormRadioGroup } from '@gitlab/ui';
import { s__ } from '~/locale';
 
import InstallationTitle from '~/packages_and_registries/package_registry/components/details/installation_title.vue';
import {
  TRACKING_ACTION_COPY_NPM_INSTALL_COMMAND,
  TRACKING_ACTION_COPY_NPM_SETUP_COMMAND,
  TRACKING_ACTION_COPY_YARN_INSTALL_COMMAND,
  TRACKING_ACTION_COPY_YARN_SETUP_COMMAND,
  TRACKING_LABEL_CODE_INSTRUCTION,
  NPM_PACKAGE_MANAGER,
  YARN_PACKAGE_MANAGER,
  PROJECT_PACKAGE_ENDPOINT_TYPE,
  INSTANCE_PACKAGE_ENDPOINT_TYPE,
  NPM_HELP_PATH,
} from '~/packages_and_registries/package_registry/constants';
import CodeInstruction from '~/vue_shared/components/registry/code_instruction.vue';
 
export default {
  name: 'NpmInstallation',
  components: {
    InstallationTitle,
    CodeInstruction,
    GlLink,
    GlSprintf,
    GlFormRadioGroup,
  },
  inject: ['npmInstanceUrl'],
  props: {
    packageEntity: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      instructionType: NPM_PACKAGE_MANAGER,
      packageEndpointType: INSTANCE_PACKAGE_ENDPOINT_TYPE,
    };
  },
  computed: {
    npmCommand() {
      return this.npmInstallationCommand(NPM_PACKAGE_MANAGER);
    },
    npmSetup() {
      return this.npmSetupCommand(NPM_PACKAGE_MANAGER, this.packageEndpointType);
    },
    yarnCommand() {
      return this.npmInstallationCommand(YARN_PACKAGE_MANAGER);
    },
    yarnSetupCommand() {
      return this.npmSetupCommand(YARN_PACKAGE_MANAGER, this.packageEndpointType);
    },
    showNpm() {
      return this.instructionType === NPM_PACKAGE_MANAGER;
    },
  },
  methods: {
    npmInstallationCommand(type) {
      // eslint-disable-next-line @gitlab/require-i18n-strings
      const instruction = type === NPM_PACKAGE_MANAGER ? 'npm i' : 'yarn add';
 
      return `${instruction} ${this.packageEntity.name}`;
    },
    npmSetupCommand(type, endpointType) {
      const scope = this.packageEntity.name.substring(0, this.packageEntity.name.indexOf('/'));
      const npmPathForEndpoint =
        endpointType === INSTANCE_PACKAGE_ENDPOINT_TYPE
          ? this.npmInstanceUrl
          : this.packageEntity.npmUrl;
 
      if (type === NPM_PACKAGE_MANAGER) {
        return `echo ${scope}:registry=${npmPathForEndpoint}/ >> .npmrc`;
      }
 
      return `echo \\"${scope}:registry\\" \\"${npmPathForEndpoint}/\\" >> .yarnrc`;
    },
  },
  packageManagers: {
    NPM_PACKAGE_MANAGER,
  },
  tracking: {
    TRACKING_ACTION_COPY_NPM_INSTALL_COMMAND,
    TRACKING_ACTION_COPY_NPM_SETUP_COMMAND,
    TRACKING_ACTION_COPY_YARN_INSTALL_COMMAND,
    TRACKING_ACTION_COPY_YARN_SETUP_COMMAND,
    TRACKING_LABEL_CODE_INSTRUCTION,
  },
  i18n: {
    helpText: s__(
      'PackageRegistry|You may also need to setup authentication using an auth token. %{linkStart}See the documentation%{linkEnd} to find out more.',
    ),
  },
  links: { NPM_HELP_PATH },
  installOptions: [
    { value: NPM_PACKAGE_MANAGER, label: s__('PackageRegistry|Show NPM commands') },
    { value: YARN_PACKAGE_MANAGER, label: s__('PackageRegistry|Show Yarn commands') },
  ],
  packageEndpointTypeOptions: [
    { value: INSTANCE_PACKAGE_ENDPOINT_TYPE, text: s__('PackageRegistry|Instance-level') },
    { value: PROJECT_PACKAGE_ENDPOINT_TYPE, text: s__('PackageRegistry|Project-level') },
  ],
};
</script>
 
<template>
  <div>
    <installation-title
      :package-type="$options.packageManagers.NPM_PACKAGE_MANAGER"
      :options="$options.installOptions"
      @change="instructionType = $event"
    />
 
    <code-instruction
      v-if="showNpm"
      :instruction="npmCommand"
      :copy-text="s__('PackageRegistry|Copy npm command')"
      :tracking-action="$options.tracking.TRACKING_ACTION_COPY_NPM_INSTALL_COMMAND"
      :tracking-label="$options.tracking.TRACKING_LABEL_CODE_INSTRUCTION"
    />
 
    <code-instruction
      v-else
      :instruction="yarnCommand"
      :copy-text="s__('PackageRegistry|Copy yarn command')"
      :tracking-action="$options.tracking.TRACKING_ACTION_COPY_YARN_INSTALL_COMMAND"
      :tracking-label="$options.tracking.TRACKING_LABEL_CODE_INSTRUCTION"
    />
 
    <h3 class="gl-font-lg">{{ __('Registry setup') }}</h3>
 
    <gl-form-radio-group
      :options="$options.packageEndpointTypeOptions"
      :checked="packageEndpointType"
      @change="packageEndpointType = $event"
    />
 
    <code-instruction
      v-if="showNpm"
      :instruction="npmSetup"
      :copy-text="s__('PackageRegistry|Copy npm setup command')"
      :tracking-action="$options.tracking.TRACKING_ACTION_COPY_NPM_SETUP_COMMAND"
      :tracking-label="$options.tracking.TRACKING_LABEL_CODE_INSTRUCTION"
    />
 
    <code-instruction
      v-else
      :instruction="yarnSetupCommand"
      :copy-text="s__('PackageRegistry|Copy yarn setup command')"
      :tracking-action="$options.tracking.TRACKING_ACTION_COPY_YARN_SETUP_COMMAND"
      :tracking-label="$options.tracking.TRACKING_LABEL_CODE_INSTRUCTION"
    />
 
    <span class="gl-text-secondary">
      <gl-sprintf :message="$options.i18n.helpText">
        <template #link="{ content }">
          <gl-link :href="$options.links.NPM_HELP_PATH" target="_blank">{{ content }}</gl-link>
        </template>
      </gl-sprintf>
    </span>
  </div>
</template>