-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetuptools_npm.py
97 lines (76 loc) · 2.74 KB
/
setuptools_npm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Plugin for setuptools providing npm commands."""
import os
import shlex
import subprocess
from distutils import log
from setuptools import Command
__version__ = '0.3'
# Taken from https://github.com/python-babel/babel/blob/master/babel/messages/frontend.py
def listify_value(arg, split=None):
"""
Make a list out of an argument.
Values from `distutils` argument parsing are always single strings;
values from `optparse` parsing may be lists of strings that may need
to be further split.
No matter the input, this function returns a flat list of whitespace-trimmed
strings, with `None` values filtered out.
>>> listify_value("foo bar")
['foo', 'bar']
>>> listify_value(["foo bar"])
['foo', 'bar']
>>> listify_value([["foo"], "bar"])
['foo', 'bar']
>>> listify_value([["foo"], ["bar", None, "foo"]])
['foo', 'bar', 'foo']
>>> listify_value("foo, bar, quux", ",")
['foo', 'bar', 'quux']
:param arg: A string or a list of strings
:param split: The argument to pass to `str.split()`.
:return:
"""
out = []
if not isinstance(arg, (list, tuple)):
arg = [arg]
for val in arg:
if val is None:
continue
if isinstance(val, (list, tuple)):
out.extend(listify_value(val, split=split))
continue
out.extend(s.strip() for s in str(val).split(split))
assert all(isinstance(val, str) for val in out)
return out
class npm_install(Command):
"""Custom command that runs npm install."""
description = 'npm install dependencies'
user_options = [
('no-clean', None, 'use npm install instead of npm clean-install'),
]
boolean_options = ['no-clean']
def initialize_options(self):
self.no_clean = False
def finalize_options(self):
pass
def run(self):
command = 'install' if self.no_clean else 'clean-install'
log.info("-> npm %s", command)
subprocess.run(['npm', command], check=True)
class npm_run(Command):
"""Custom command that runs npm run."""
description = 'npm run script'
user_options = [
('script=', None, 'semicolon or newline separated list of npm scripts to run'),
]
def initialize_options(self):
self.script = []
def finalize_options(self):
self.script = listify_value(listify_value(self.script, split=';'), '\n')
def run(self):
for script in self.script:
log.info("-> npm run %s", script)
subprocess.run(['npm', 'run', *shlex.split(script)], check=True)
def npm_not_skipped(build) -> bool:
"""Return whether npm command should be run.
This can be used as predicate in distutils sub_commands.
"""
return not bool(os.environ.get('SKIP_NPM', False))