-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
155 lines (113 loc) · 3.36 KB
/
list.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var path = require('path');
var fs = require('fs');
var child_process = require('child_process');
var Promise = require('Promise');
var traverse = require('traverse');
var jf = require('jsonfile');
var project_root = '/Users/jonbennett/projects/uService';
var projects = get_directories( project_root );
/*
Scan output...
*/
var packages = {};
get_all_packages( projects ).then( function(result){
var output_file = process.cwd() + '/license_list.json';
jf.writeFileSync(output_file , packages );
//Dump it as csv for Excel
var csv_output = process.cwd() + '/license_list.csv';
var package_names = [];
for ( var i in packages ){
var entry = packages[i];
package_names.push( entry.name );
}
package_names = package_names.sort();
package_names.forEach( function( package_name ) {
var entry = packages[package_name];
var line = entry.name + ', ' + entry.version + ', ' + entry.license + ', ' + entry.repository + '\n';
fs.appendFileSync(csv_output, line);
});
console.log( "Done...");
process.exit(0);
});
/**
Find all projects in a given directory
**/
function get_directories( path ){
var files = fs.readdirSync(path);
var directories = [];
files.forEach( function(entry){
var filepath = path + "/" + entry;
var stat = fs.statSync(filepath);
if ( stat.isDirectory() ){
directories.push( filepath);
}
});
return directories;
}
function get_all_packages( directories ){
var p = directories.reduce(
function( previous, next ){
return previous.then( function(result ){
return scan_directory( next );
});
}, Promise.resolve() );
return p;
}
function scan_directory( location ){
var command = 'npm';
var options = ['ll', '-json'];
var environment = {cwd: location};
console.log( 'Scanning: ' + location);
var p = execute_async( command, options, environment );
p.then( function(result){
var object = JSON.parse(result);
traverse_json(object);
});
return p;
}
function traverse_json( object ){
traverse(object).forEach(function (x) {
if ( x.dependencies ){
process_dependencies(x.dependencies);
}
});
}
function process_dependencies( object ){
for ( var i in object ){
console.log ( 'Processing: ' + object[i].name );
var description = {
name : object[i].name,
version : object[i].version,
license : object[i].license,
repository : object[i].repository ? object[i].repository.url : '---'
};
//Anything without a git repo is one of ours..
if ( description.repository == '---' ){
return;
}
//Handle complex license fields
description.license = (description.license === '[object Object]') ? 'undefined' : description.license;
packages[description.name] = description;
}
}
function execute_async( command, options, environment ){
console.log( 'Executing: ' + command + ' : ' + options.toString() );
var cf_promise = new Promise(function (resolve, reject){
var output = "";
var cf_result = child_process.spawn( command, options, environment );
cf_result.stdout.on('data', function(data) {
output = output + data.toString();
console.log( '\t' + data.toString());
});
cf_result.on('exit', function(code) {
console.log( '..command complete (' + command + ' : ' + options + ') code is: ' + code );
if ( code === 0 ){
resolve(output);
}
else{
reject(code);
}
});
});
return cf_promise;
}