diff --git a/changelogs/fragments/2146-npm-add_no_bin_links_option.yaml b/changelogs/fragments/2146-npm-add_no_bin_links_option.yaml new file mode 100644 index 0000000000..651af80186 --- /dev/null +++ b/changelogs/fragments/2146-npm-add_no_bin_links_option.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - npm - add ``no_bin_links`` option (https://github.com/ansible-collections/community.general/issues/2128). diff --git a/plugins/modules/packaging/language/npm.py b/plugins/modules/packaging/language/npm.py index 8cde63f48c..62121297d7 100644 --- a/plugins/modules/packaging/language/npm.py +++ b/plugins/modules/packaging/language/npm.py @@ -82,6 +82,12 @@ options: type: bool default: no version_added: 2.0.0 + no_bin_links: + description: + - Use the C(--no-bin-links) flag when installing. + type: bool + default: no + version_added: 2.5.0 requirements: - npm installed in bin path (recommended /usr/local/bin) ''' @@ -151,6 +157,7 @@ class Npm(object): self.unsafe_perm = kwargs['unsafe_perm'] self.state = kwargs['state'] self.no_optional = kwargs['no_optional'] + self.no_bin_links = kwargs['no_bin_links'] if kwargs['executable']: self.executable = kwargs['executable'].split(' ') @@ -181,6 +188,8 @@ class Npm(object): cmd.append(self.registry) if self.no_optional: cmd.append('--no-optional') + if self.no_bin_links: + cmd.append('--no-bin-links') # If path is specified, cd into that path and run the command. cwd = None @@ -259,6 +268,7 @@ def main(): unsafe_perm=dict(default=False, type='bool'), ci=dict(default=False, type='bool'), no_optional=dict(default=False, type='bool'), + no_bin_links=dict(default=False, type='bool'), ) arg_spec['global'] = dict(default=False, type='bool') module = AnsibleModule( @@ -278,6 +288,7 @@ def main(): unsafe_perm = module.params['unsafe_perm'] ci = module.params['ci'] no_optional = module.params['no_optional'] + no_bin_links = module.params['no_bin_links'] if not path and not glbl: module.fail_json(msg='path must be specified when not using global') @@ -286,7 +297,7 @@ def main(): npm = Npm(module, name=name, path=path, version=version, glbl=glbl, production=production, executable=executable, registry=registry, ignore_scripts=ignore_scripts, - unsafe_perm=unsafe_perm, state=state, no_optional=no_optional) + unsafe_perm=unsafe_perm, state=state, no_optional=no_optional, no_bin_links=no_bin_links) changed = False if ci: diff --git a/tests/integration/targets/npm/tasks/no_bin_links.yml b/tests/integration/targets/npm/tasks/no_bin_links.yml new file mode 100644 index 0000000000..fdbc88c4eb --- /dev/null +++ b/tests/integration/targets/npm/tasks/no_bin_links.yml @@ -0,0 +1,64 @@ +--- +- name: 'Remove any node modules' + file: + path: '{{ remote_dir }}/node_modules' + state: absent + +- vars: + # sample: node-v8.2.0-linux-x64.tar.xz + node_path: '{{ remote_dir }}/{{ nodejs_path }}/bin' + package: 'ncp' + block: + - shell: npm --version + environment: + PATH: '{{ node_path }}:{{ ansible_env.PATH }}' + register: npm_version + + - debug: + var: npm_version.stdout + + - name: 'Install simple package with no_bin_links disabled' + npm: + path: '{{ remote_dir }}' + executable: '{{ node_path }}/npm' + state: present + name: '{{ package }}' + no_bin_links: false + environment: + PATH: '{{ node_path }}:{{ ansible_env.PATH }}' + register: npm_install_no_bin_links_disabled + + - name: 'Make sure .bin folder has been created' + stat: + path: "{{ remote_dir }}/node_modules/.bin" + register: npm_dotbin_folder_disabled + + - name: 'Remove any node modules' + file: + path: '{{ remote_dir }}/node_modules' + state: absent + + - name: 'Install simple package with no_bin_links enabled' + npm: + path: '{{ remote_dir }}' + executable: '{{ node_path }}/npm' + state: present + name: '{{ package }}' + no_bin_links: true + environment: + PATH: '{{ node_path }}:{{ ansible_env.PATH }}' + register: npm_install_no_bin_links_enabled + + - name: 'Make sure .bin folder has not been created' + stat: + path: "{{ remote_dir }}/node_modules/.bin" + register: npm_dotbin_folder_enabled + + - assert: + that: + - npm_install_no_bin_links_disabled is success + - npm_install_no_bin_links_disabled is changed + - npm_install_no_bin_links_enabled is success + - npm_install_no_bin_links_enabled is changed + - npm_dotbin_folder_disabled.stat.exists + - not npm_dotbin_folder_enabled.stat.exists diff --git a/tests/integration/targets/npm/tasks/run.yml b/tests/integration/targets/npm/tasks/run.yml index 53b374fa63..c82e7e4e37 100644 --- a/tests/integration/targets/npm/tasks/run.yml +++ b/tests/integration/targets/npm/tasks/run.yml @@ -1,2 +1,3 @@ - include_tasks: setup.yml - include_tasks: test.yml +- include_tasks: no_bin_links.yml