Skip to content

Commit 34d830b

Browse files
committed
Refactor resolving licenses into separate modules and update Makefile recipes
1 parent 7310604 commit 34d830b

76 files changed

Lines changed: 2703 additions & 184 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tools/licenses/infer/README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Infer Licenses
2+
3+
> Infer license information from file content.
4+
5+
6+
<!-- <intro> -->
7+
8+
<!-- </intro> -->
9+
10+
11+
<!-- <usage> -->
12+
13+
## Usage
14+
15+
``` javascript
16+
var infer = require( '/path/to/stdlib/tools/licenses/infer' );
17+
```
18+
19+
#### infer( pkgs, pattern, clbk )
20+
21+
Infers license information from the content of files found using a provided glob `pattern`.
22+
23+
``` javascript
24+
var licenses = require( '/path/to/stdlib/tools/licenses/licenses' );
25+
26+
var pattern = '{readme*,licen[cs]e*,copying*}';
27+
28+
licenses( onResults );
29+
30+
function onResults( error, results ) {
31+
if ( error ) {
32+
throw error;
33+
}
34+
infer( results, pattern, onInfer ) );
35+
}
36+
37+
function onInfer( error, results ) {
38+
if ( error ) {
39+
throw error;
40+
}
41+
console.dir( results );
42+
}
43+
```
44+
45+
<!-- </usage> -->
46+
47+
48+
<!-- <examples> -->
49+
50+
<!-- ## Examples
51+
52+
``` javascript
53+
54+
``` -->
55+
56+
<!-- </examples> -->
57+
58+
59+
<!-- <cli> -->
60+
61+
---
62+
63+
## CLI
64+
65+
<!-- <usage> -->
66+
67+
### Usage
68+
69+
``` bash
70+
Usage: licenses-infer [options]
71+
72+
Options:
73+
74+
-h, --help Print this message.
75+
-V, --version Print the package version.
76+
--pattern pattern Glob pattern used to find files.
77+
```
78+
79+
<!-- </usage> -->
80+
81+
82+
<!-- <notes> -->
83+
84+
### Notes
85+
86+
* Use as part of a standard stream pipeline.
87+
88+
<!-- </notes> -->
89+
90+
91+
<!-- <examples> -->
92+
93+
### Examples
94+
95+
To pretty print license results,
96+
97+
``` bash
98+
$ licenses | licenses-infer
99+
```
100+
101+
Example output:
102+
103+
``` text
104+
105+
Package license information:
106+
107+
fs.realpath@1.0.0
108+
├── path: /path/to/node_modules/fs.realpath
109+
├── repo: https://github.com/isaacs/fs.realpath
110+
├── package.json: ISC
111+
├── license: ISC
112+
└── license: MIT
113+
114+
spdx-correct@1.0.2
115+
├── path: /path/to/node_modules/spdx-correct
116+
├── repo: https://github.com/kemitchell/spdx-correct.js
117+
├── package.json: Apache-2.0
118+
├── license: Apache-2.0
119+
└── readme: MIT
120+
121+
spdx-expression-parse@1.0.3
122+
├── path: /path/to/node_modules/spdx-expression-parse
123+
├── repo: https://github.com/kemitchell/spdx-expression-parse.js
124+
├── package.json: (MIT AND CC-BY-3.0)
125+
├── readme: MIT
126+
└── license: MIT
127+
```
128+
129+
To use as part of a pipeline,
130+
131+
``` bash
132+
$ licenses | licenses-infer | cat
133+
# => {"id":"...","parents":["..."],...,"licenses":{...}}
134+
# => {"id":"...","parents":["..."],...,"licenses":{...}}
135+
# => ...
136+
```
137+
138+
139+
<!-- </examples> -->
140+
141+
<!-- </cli> -->
142+
143+
144+
<!-- <links> -->
145+
146+
<!-- </links> -->

tools/licenses/infer/bin/cli

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// MODULES //
5+
6+
var fs = require( 'fs' );
7+
var path = require( 'path' );
8+
var parseArgs = require( 'minimist' );
9+
var pkg = require( './../package.json' );
10+
var opts = require( './opts.json' );
11+
var prefix = require( './../lib/stdlib.js' );
12+
var stdin = require( prefix+'@stdlib/utils/read-stdin' );
13+
var infer = require( './../lib' );
14+
15+
16+
// FUNCTIONS //
17+
18+
/**
19+
* Prints usage information.
20+
*
21+
* @private
22+
* @example
23+
* help();
24+
* // => '...'
25+
*/
26+
function help() {
27+
var fpath = path.join( __dirname, 'usage.txt' );
28+
fs.createReadStream( fpath )
29+
.pipe( process.stdout )
30+
.on( 'close', onClose );
31+
32+
function onClose() {
33+
process.exit( 0 );
34+
}
35+
} // end FUNCTION help()
36+
37+
/**
38+
* Prints the package version.
39+
*
40+
* @private
41+
* @example
42+
* version();
43+
* // => '#.#.#'
44+
*/
45+
function version() {
46+
var msg = pkg.version.toString()+'\n';
47+
process.stdout.write( msg, 'utf8' );
48+
process.exit( 0 );
49+
} // end FUNCTION version()
50+
51+
/**
52+
* Callback invoked upon inferring license information.
53+
*
54+
* @private
55+
* @param {(Error|null)} error - error object
56+
* @param {(ObjectArray|EmptyArray)} results - results
57+
*/
58+
function onInfer( error, results ) {
59+
if ( error ) {
60+
throw error;
61+
}
62+
if ( process.stdout.isTTY ) {
63+
return prettyPrint( results );
64+
}
65+
printNDJSON( results );
66+
} // end FUNCTION onInfer()
67+
68+
/**
69+
* Pretty prints results.
70+
*
71+
* @private
72+
* @param {(ObjectArray|EmptyArray)} results - results
73+
*/
74+
function prettyPrint( results ) {
75+
var licenses;
76+
var license;
77+
var src;
78+
var sym;
79+
var i;
80+
var j;
81+
if ( results.length ) {
82+
console.log( '\nPackage license information:\n' );
83+
for ( i = 0; i < results.length; i++ ) {
84+
console.log( results[ i ].id );
85+
console.log( '├── path: %s', results[ i ].pkg );
86+
console.log( '├── repo: %s', results[ i ].repo || 'UNKNOWN' );
87+
licenses = results[ i ].licenses;
88+
if ( licenses.length ) {
89+
for ( j = 0; j < licenses.length; j++ ) {
90+
license = licenses[ j ];
91+
if ( /package\.json$/i.test( license.src ) ) {
92+
src = 'package.json';
93+
} else if ( /readme.*$/i.test( license.src ) ) {
94+
src = 'readme';
95+
} else {
96+
src = 'license';
97+
}
98+
if ( j < licenses.length-1 ) {
99+
sym = '├──';
100+
} else {
101+
sym = '└──';
102+
}
103+
console.log( '%s %s: %s', sym, src, license.name );
104+
}
105+
} else {
106+
console.log( '└── license: UNKNOWN' );
107+
}
108+
console.log( '' );
109+
}
110+
return process.exit( 0 );
111+
}
112+
console.log( 'No license information.' );
113+
process.exit( 0 );
114+
} // end FUNCTION prettyPrint()
115+
116+
/**
117+
* Prints results as newline-delimited JSON (NDJSON).
118+
*
119+
* @private
120+
* @param {(ObjectArray|EmptyArray)} results - results
121+
*/
122+
function printNDJSON( results ) {
123+
var i;
124+
for ( i = 0; i < results.length; i++ ) {
125+
console.log( JSON.stringify( results[ i ] ) );
126+
}
127+
} // end FUNCTION printNDJSON()
128+
129+
130+
// VARIABLES //
131+
132+
var pattern;
133+
var args;
134+
135+
136+
// INIT //
137+
138+
process.title = pkg.name;
139+
process.stdout.on( 'error', process.exit );
140+
141+
142+
// ARGUMENTS //
143+
144+
args = parseArgs( process.argv.slice( 2 ), opts );
145+
146+
if ( args.help ) {
147+
return help();
148+
}
149+
if ( args.version ) {
150+
return version();
151+
}
152+
if ( args.pattern ) {
153+
pattern = args.pattern;
154+
} else {
155+
pattern = '{readme*,licen[cs]e*,copying*}';
156+
}
157+
158+
159+
// MAIN //
160+
161+
stdin( 'utf8', onRead );
162+
163+
/**
164+
* Callback invoked after reading `stdin`.
165+
*
166+
* @private
167+
* @param {(Error|null)} error - error object
168+
* @param {string} data - data
169+
* @throws {Error} unexpected error
170+
*/
171+
function onRead( error, data ) {
172+
var results;
173+
var i;
174+
if ( error ) {
175+
throw error;
176+
}
177+
data = data.toString().split( /\r?\n/g );
178+
results = [];
179+
for ( i = 0; i < data.length; i++ ) {
180+
if ( data[ i ] ) {
181+
results.push( JSON.parse( data[ i ] ) );
182+
}
183+
}
184+
infer( results, pattern, onInfer );
185+
} // end FUNCTION onRead()

tools/licenses/infer/bin/opts.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"string": [
3+
"pattern"
4+
],
5+
"boolean": [
6+
"help",
7+
"version"
8+
],
9+
"alias": {
10+
"help": [
11+
"h"
12+
],
13+
"version": [
14+
"V"
15+
]
16+
}
17+
}

tools/licenses/infer/bin/usage.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Usage: licenses-infer [options]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
--pattern pattern Glob pattern used to find files.
9+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var licenses = require( './../../licenses/lib' );
4+
var infer = require( './../lib' );
5+
6+
var pattern = '{readme*,licen[cs]e*,copying*}';
7+
8+
licenses( onResults );
9+
10+
function onResults( error, results ) {
11+
if ( error ) {
12+
throw error;
13+
}
14+
infer( results, pattern, onInfer );
15+
}
16+
17+
function onInfer( error, results ) {
18+
if ( error ) {
19+
throw error;
20+
}
21+
console.dir( results );
22+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// MODULES //
44

5-
var debug = require( 'debug' )( 'licenses:db' );
5+
var debug = require( 'debug' )( 'licenses:infer:db' );
66
var join = require( 'path' ).join;
77
var prefix = require( './stdlib.js' );
88
var readFile = require( prefix+'@stdlib/fs/read-file' ).sync;

0 commit comments

Comments
 (0)