Skip to content

Commit fe8f5f4

Browse files
committed
Add script for removing license headers
1 parent 5828396 commit fe8f5f4

1 file changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2018 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
/*
24+
* This script removes license headers from a list of files.
25+
*/
26+
27+
// MODULES //
28+
29+
var CLI = require( '@stdlib/tools/cli' );
30+
var stdin = require( '@stdlib/process/read-stdin' );
31+
var RE_EOL = require( '@stdlib/regexp/eol' );
32+
var isRegExpString = require( '@stdlib/assert/is-regexp-string' );
33+
var reFromString = require( '@stdlib/utils/regexp-from-string' );
34+
var headerRegExp = require( '@stdlib/_tools/licenses/header-regexp' );
35+
var removeHeader = require( '@stdlib/_tools/licenses/remove-header-file-list' );
36+
37+
38+
// VARIABLES //
39+
40+
var SPDX = 'Apache-2.0';
41+
var LANGS = [
42+
[ 'awk', 'awk' ], // [ <file_extension>, <file_type> ]
43+
[ 'c', 'c' ],
44+
[ 'cli', 'javascript' ],
45+
[ 'cpp', 'cpp' ],
46+
[ 'css', 'css' ],
47+
[ 'f', 'fortran' ],
48+
[ 'gyp', 'gyp' ],
49+
[ 'gypi', 'gypi' ],
50+
[ 'h', 'h' ],
51+
[ 'hpp', 'hpp' ],
52+
[ 'html', 'html' ],
53+
[ 'ini', 'ini' ],
54+
[ 'jl', 'julia' ],
55+
[ 'js', 'javascript' ],
56+
[ 'makefile', 'make' ],
57+
[ 'md', 'markdown' ],
58+
[ 'mk', 'make' ],
59+
[ 'py', 'python' ],
60+
[ 'R', 'r' ],
61+
[ 'sh', 'bash' ],
62+
[ 'yml', 'yaml' ]
63+
];
64+
var OPTS = {
65+
'string': [
66+
'split'
67+
]
68+
};
69+
70+
71+
// FUNCTIONS //
72+
73+
/**
74+
* Callback invoked upon completion.
75+
*
76+
* @private
77+
* @param {(Error|null)} error - error object
78+
* @returns {void}
79+
*/
80+
function done( error ) {
81+
if ( error ) {
82+
process.exitCode = 1;
83+
return console.error( 'Error: %s', error.message ); // eslint-disable-line no-console
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
* @returns {void}
95+
*/
96+
function main() {
97+
var headers;
98+
var split;
99+
var flags;
100+
var args;
101+
var cli;
102+
var re;
103+
var i;
104+
105+
// Create a command-line interface:
106+
cli = new CLI({
107+
'options': OPTS
108+
});
109+
110+
// Get any provided command-line arguments:
111+
args = cli.args();
112+
113+
// Get any provided command-line flags:
114+
flags = cli.flags();
115+
116+
// Generate header regular expressions:
117+
headers = {};
118+
for ( i = 0; i < LANGS.length; i++ ) {
119+
re = headerRegExp( SPDX, LANGS[i][1] );
120+
re = new RegExp( re.source+'\n' ); // include additional padded line
121+
headers[ LANGS[i][0] ] = re;
122+
}
123+
124+
if ( !process.stdin.isTTY ) {
125+
// We are receiving data from `stdin`...
126+
if ( flags.split ) {
127+
if ( !isRegExpString( flags.split ) ) {
128+
flags.split = '/'+flags.split+'/';
129+
}
130+
split = reFromString( flags.split );
131+
} else {
132+
split = RE_EOL;
133+
}
134+
return stdin( onRead );
135+
}
136+
return removeHeader( args, headers, done );
137+
138+
/**
139+
* Callback invoked upon reading from `stdin`.
140+
*
141+
* @private
142+
* @param {(Error|null)} error - error object
143+
* @param {Buffer} data - data
144+
* @returns {void}
145+
*/
146+
function onRead( error, data ) {
147+
var lines;
148+
if ( error ) {
149+
return done( error );
150+
}
151+
lines = data.toString().split( split );
152+
153+
// Check if input data had a trailing newline...
154+
if ( lines[ lines.length-1 ] === '' ) {
155+
lines.length -= 1;
156+
}
157+
return removeHeader( lines, headers, done );
158+
}
159+
}
160+
161+
main();

0 commit comments

Comments
 (0)