7

I implemented a python web client that I would like to test.

The server is hosted in npm registry. The server gets ran locally with node before running my functional tests.

How can I install properly the npm module from my setup.py script?

Here is my current solution inspired from this post:

class CustomInstallCommand(install):
    def run(self):
        arguments = [
            'npm',
            'install',
            '--prefix',
            'test/functional',
            'promisify'
        ]
        subprocess.call(arguments, shell=True)
        install.run(self)

setup(
    cmdclass={'install': CustomInstallCommand},
Community
  • 1
  • 1
Maxime Helen
  • 1,382
  • 7
  • 16

2 Answers2

9
from setuptools.command.build_py import build_py

class NPMInstall(build_py):
    def run(self):
        self.run_command('npm install --prefix test/functional promisify')
        build_py.run(self)

OR

from distutils.command.build import build

class NPMInstall(build):
    def run(self):
        self.run_command("npm install --prefix test/functional promisify")
        build.run(self)

finally:

setuptools.setup(
    cmdclass={
        'npm_install': NPMInstall
    },
    # Usual setup() args.
    # ...
)

Also look here

pramod
  • 1,453
  • 1
  • 14
  • 16
  • and if you'd like to do it as part of the build command, here's how: https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/ (same as this answer but use `build_py` as the keyword in `cmdclass`) – Milimetric Dec 20 '18 at 16:17
  • When using the above I get... running npm_install running npm install --prefix app/site/static error: invalid command 'npm install --prefix app/site/static' I already have a package.json file in the static directory. Any thoughts? – Dave L Jan 11 '20 at 08:00
2

You are very close, Here is a simple function that does just that, you can remove "--global" option is you want to install the package for the current project only, keep in mind the the command shell=True could present security risks

import subprocess
def npm_install(args=["npm","--global", "install", "search-index"])
  subprocess.Popen(args, shell=True)
SEDaradji
  • 1,348
  • 15
  • 18