---
# file: roles/nodejs/tasks/main.yml

- name: "make sure /etc/apt/keyrings exists"
  file:
    path: "/etc/apt/keyrings"
    state: directory
  tags: nodejs

- name: "download modern signature key"
  get_url:
    url: "https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key"
    dest: "/dev/shm/nodesource-repo.gpg.key"
  changed_when: false
  tags: nodejs

- name: "install modern signature key"
  shell:
    cmd: "cat /dev/shm/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg"
    creates: "/etc/apt/keyrings/nodesource.gpg"
  tags: nodejs

- name: "repository file"
  copy:
    content: "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_{{ nodejs_version }}.x nodistro main\n"
    dest: "/etc/apt/sources.list.d/nodesource.list"
  register: repo
  tags: nodejs

- name: "refresh apt if repo was modified"
  apt:
    update_cache: yes
  when: repo.changed
  tags: nodejs

- name: "/etc/apt/preferences.d/"
  template:
    src: "{{ item }}"
    dest: "/etc/apt/preferences.d/{{ item }}"
  loop:
    - 91_nodejs
    - 92_nsolid
  tags: nodejs

- name: "apt install nodejs (includes npm)"
  apt:
    name:
      - nodejs
  tags: nodejs
