---

- name: APT https transport is enabled (Debian <10)
  ansible.builtin.apt:
    name: apt-transport-https
    state: present
  tags:
    - system
    - packages
    - nodejs
  when: ansible_distribution_major_version is version('10', '<')

- name: NodeJS GPG key is installed
  ansible.builtin.copy:
    src: nodesource.asc
    dest: "{{ apt_keyring_dir }}/nodesource.asc"
    mode: "0644"
    owner: root
    group: root
  tags:
    - system
    - packages
    - nodejs

- name: Add NodeJS repository (Debian <12)
  ansible.builtin.apt_repository:
    repo: "deb [signed-by={{ apt_keyring_dir }}/nodesource.asc] https://deb.nodesource.com/{{ nodejs_apt_version }} {{ ansible_distribution_release }} main"
    filename: nodesource
    update_cache: yes
    state: present
  tags:
    - system
    - packages
    - nodejs
  when: ansible_distribution_major_version is version('12', '<')

- name: Add NodeJS repository (Debian >=12)
  ansible.builtin.template:
    src: nodesource.sources.j2
    dest: /etc/apt/sources.list.d/nodesource.sources
    state: present
  register: nodesource_sources
  tags:
    - system
    - packages
    - nodejs
  when: ansible_distribution_major_version is version('12', '>=')

- name: Update APT cache
  ansible.builtin.apt:
    update_cache: yes
  when: nodesource_sources is changed

- name: NodeJS is installed
  ansible.builtin.apt:
    name: nodejs
    state: present
  tags:
    - packages
    - nodejs

- ansible.builtin.import_tasks: yarn.yml
  when: nodejs_install_yarn | bool
