---
# Playbook that installs and configures node dependencies
# follows the official instalation guide
# https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

# nodejs-legacy npm
# npm install -g coffee-script react-tools jsxhint coffee-react 

- name: Download nodejs 4.x setup script
  get_url: 
    url: "https://deb.nodesource.com/setup_4.x"
    dest: "/tmp/node_setup.sh"
    mode: 0700

- name: Setup nodejs repositories
  shell: "/tmp/node_setup.sh"
  become: yes

- name: Install nodejs and dependencies
  apt:
    name: "{{ item }}"
    state: installed
  with_items:
    - nodejs
    - build-essential

# Extracted from picoCTF-platform/scripts/web_setup.sh
# Review necessity [TODO]
- name: Install nodejs packages (globally)
  npm: 
    name: "{{ item }}"
    global: yes
  with_items:
    - coffee-script
    - react-tools
    - jsxhint
    - coffee-react

---
# Playbook that installs and configures node dependencies
# follows the official instalation guide
# https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

# nodejs-legacy npm
# npm install -g coffee-script react-tools jsxhint coffee-react 

- name: Check if nodejs is installed
  stat:
    path: /usr/bin/nodejs
  register: nodejs

- name: Download nodejs 4.x setup script
  get_url: 
    url: "https://deb.nodesource.com/setup_4.x"
    dest: "/tmp/node_setup.sh"
    mode: 0700

# Since shell commands are not idempotent we manually
# check above if already have the nodejs binary (implying
# all nodejs steps where previosuly sucessful)
- name: Setup nodejs repositories
  shell: "/tmp/node_setup.sh"
  become: yes
  when: nodejs.stat.exists == False

- name: Install nodejs and dependencies
  apt:
    name: "{{ item }}"
    state: installed
  with_items:
    - nodejs
    - build-essential

# Extracted from picoCTF-platform/scripts/web_setup.sh
# Review necessity [TODO]
- name: Install nodejs packages (globally)
  npm: 
    name: "{{ item }}"
    global: yes
  with_items:
    - coffee-script
    - react-tools
    - jsxhint
    - coffee-react

Optimize node install by checking before command
Optimize node install by checking before command

Currently the repository setup steps rely shelling out to the nodejs
installer. Ansible has no way of automatically knowing if this needs to
be done so it was running it every time.

Manually added a check for the resulting tools we want to allow this
step to be skipped in future runs.
