@@ -22,6 +22,7 @@ class Npm(PackageManager):
22
22
23
23
dn_url = 'https://www.npmjs.com/package/'
24
24
input_file_name = 'tmp_npm_license_output.json'
25
+ tmp_custom_json = 'custom.json'
25
26
flag_tmp_node_modules = False
26
27
27
28
def __init__ (self , input_dir , output_dir ):
@@ -32,17 +33,18 @@ def __del__(self):
32
33
os .remove (self .input_file_name )
33
34
if self .flag_tmp_node_modules :
34
35
shutil .rmtree (node_modules , ignore_errors = True )
36
+ if os .path .exists (self .tmp_custom_json ):
37
+ os .remove (self .tmp_custom_json )
35
38
36
39
def run_plugin (self ):
37
40
ret = self .start_license_checker ()
38
41
return ret
39
42
40
43
def start_license_checker (self ):
41
44
ret = True
42
- tmp_custom_json = 'custom.json'
43
- license_checker_cmd = f'license-checker --excludePrivatePackages --production --json --out { self .input_file_name } '
45
+ license_checker_cmd = f'license-checker --production --json --out { self .input_file_name } '
44
46
custom_path_option = ' --customPath '
45
- npm_install_cmd = 'npm install --omit=dev '
47
+ npm_install_cmd = 'npm install --production '
46
48
47
49
if os .path .isdir (node_modules ) != 1 :
48
50
logger .info (f"node_modules directory is not existed. So it executes '{ npm_install_cmd } '." )
@@ -53,18 +55,18 @@ def start_license_checker(self):
53
55
return False
54
56
55
57
# customized json file for obtaining specific items with license-checker
56
- self .make_custom_json (tmp_custom_json )
58
+ self .make_custom_json (self . tmp_custom_json )
57
59
58
- cmd = license_checker_cmd + custom_path_option + tmp_custom_json
60
+ cmd = license_checker_cmd + custom_path_option + self . tmp_custom_json
59
61
cmd_ret = subprocess .call (cmd , shell = True )
60
62
if cmd_ret != 0 :
61
63
logger .error (f"It returns the error: { cmd } " )
62
64
logger .error ("Please check if the license-checker is installed.(sudo npm install -g license-checker)" )
63
- return False
65
+ ret = False
64
66
else :
65
67
self .append_input_package_list_file (self .input_file_name )
66
-
67
- os .remove (tmp_custom_json )
68
+ if os . path . exists ( self . tmp_custom_json ):
69
+ os .remove (self . tmp_custom_json )
68
70
69
71
return ret
70
72
@@ -111,21 +113,24 @@ def parse_transitive_relationship(self):
111
113
ret = False
112
114
if ret :
113
115
if result .returncode == 1 :
114
- logger .warning (f'npm ls returns an error code: { result .stderr } ' )
116
+ logger .warning (f"' { cmd } ' returns error code: { result .stderr } " )
115
117
116
118
try :
117
119
rel_json = json .loads (rel_tree )
118
120
if len (rel_json ) < 1 :
119
121
ret = False
120
122
else :
121
123
self .package_name = f'{ rel_json [_name ]} ({ rel_json [_version ]} )'
122
- self .parse_rel_dependencies (rel_json [_name ], rel_json [_version ], rel_json [_dependencies ])
124
+ if _dependencies in rel_json :
125
+ self .parse_rel_dependencies (rel_json [_name ], rel_json [_version ], rel_json [_dependencies ])
123
126
except Exception as e :
124
127
ret = False
125
128
err_msg = e
126
129
return ret , err_msg
127
130
128
131
def parse_direct_dependencies (self ):
132
+ if not self .direct_dep :
133
+ return
129
134
try :
130
135
if os .path .isfile (const .SUPPORT_PACKAE .get (self .package_manager_name )):
131
136
ret , err_msg = self .parse_transitive_relationship ()
@@ -145,6 +150,9 @@ def parse_oss_information(self, f_name):
145
150
146
151
sheet_list = []
147
152
comment = ''
153
+ _licenses = 'licenses'
154
+ _repository = 'repository'
155
+ _private = 'private'
148
156
149
157
keys = [key for key in json_data ]
150
158
@@ -153,31 +161,38 @@ def parse_oss_information(self, f_name):
153
161
oss_init_name = d ['name' ]
154
162
oss_name = self .package_manager_name + ":" + oss_init_name
155
163
156
- if d ['licenses' ]:
157
- license_name = d ['licenses' ]
164
+ if d [_licenses ]:
165
+ license_name = d [_licenses ]
158
166
else :
159
167
license_name = ''
160
168
161
169
oss_version = d ['version' ]
162
170
package_path = d ['path' ]
163
171
164
- if d [ 'repository' ]:
165
- dn_loc = d [ 'repository' ]
166
- else :
167
- dn_loc = f" { self . dn_url } { oss_init_name } /v/ { oss_version } "
172
+ private_pkg = False
173
+ if _private in d :
174
+ if d [ _private ] :
175
+ private_pkg = True
168
176
169
177
homepage = self .dn_url + oss_init_name
178
+ dn_loc = f"{ self .dn_url } { oss_init_name } /v/{ oss_version } "
179
+ if d [_repository ]:
180
+ dn_loc = d [_repository ]
181
+ elif private_pkg :
182
+ dn_loc = ''
170
183
171
184
comment_list = []
172
185
deps_list = []
173
- if self .direct_dep and len (self .relation_tree ) > 0 :
174
- if self .package_name == f'{ oss_init_name } ({ oss_version } )' :
175
- comment_list .append ('root package' )
186
+ if private_pkg :
187
+ homepage = dn_loc
188
+ comment_list .append ('private' )
189
+ if self .package_name == f'{ oss_init_name } ({ oss_version } )' :
190
+ comment_list .append ('root package' )
191
+ elif self .direct_dep and len (self .relation_tree ) > 0 :
192
+ if f'{ oss_init_name } ({ oss_version } )' in self .relation_tree [self .package_name ]:
193
+ comment_list .append ('direct' )
176
194
else :
177
- if f'{ oss_init_name } ({ oss_version } )' in self .relation_tree [self .package_name ]:
178
- comment_list .append ('direct' )
179
- else :
180
- comment_list .append ('transitive' )
195
+ comment_list .append ('transitive' )
181
196
182
197
if f'{ oss_init_name } ({ oss_version } )' in self .relation_tree :
183
198
rel_items = [f'npm:{ ri } ' for ri in self .relation_tree [f'{ oss_init_name } ({ oss_version } )' ]]
0 commit comments