---
- name: Install NodeJS and NPM
  shell: curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n && bash n lts
  args:
    warn: false
    chdir: "/tmp"

- name: Set permissions for folders
  file: path="{{ item }}" state=directory owner=azuracast group=www-data mode=0744
  with_items:
    - "{{ www_base }}/frontend/node_modules"
    - "{{ www_base }}/web/static"
    - "{{ www_base }}/web/static/dist"
    - "{{ www_base }}/web/static/webpack_dist"

- name: Pull Node Dependencies
  become: true
  become_user: azuracast
  command: >-
    npm ci
  args:
    chdir: "{{ www_base }}/frontend"

- name: Build AzuraCast Frontend Scripts
  become: true
  become_user: azuracast
  command: >-
    npm run build
  args:
    chdir: "{{ www_base }}/frontend"

- name: Set permissions of generated frontend content
  command: >-
    {{ azuracast_build_perm_command }}
  loop:
    - "chown -R azuracast:www-data {{ www_base }}"
    - "find {{ www_base }} -type d -exec chmod 755 {} \\;"
    - "find {{ www_base }} -type f -exec chmod 644 {} \\;"
  loop_control:
    loop_var: azuracast_build_perm_command
  # Note: Don't add warn: false here, it's incompatible with the earlier Ansibles used in 16.04/18.04.
