|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2019 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var path = require( 'path' ); |
| 24 | +var logger = require( 'debug' ); |
| 25 | +var resolveParentPath = require( '@stdlib/fs/resolve-parent-path' ).sync; |
| 26 | +var readFile = require( '@stdlib/fs/read-file' ).sync; |
| 27 | +var writeFile = require( '@stdlib/fs/write-file' ).sync; |
| 28 | +var RE_EOL = require( '@stdlib/regexp/eol' ); |
| 29 | +var trim = require( '@stdlib/string/trim' ); |
| 30 | + |
| 31 | + |
| 32 | +// VARIABLES // |
| 33 | + |
| 34 | +var debug = logger( 'repl:contributors:build' ); |
| 35 | + |
| 36 | +// Output file path: |
| 37 | +var OUTPUT_FILE = path.resolve( __dirname, '..', 'data', 'contributor.json' ); |
| 38 | + |
| 39 | +// Regular expression to match a comment line: |
| 40 | +var RE_COMMENT = /^#/; |
| 41 | + |
| 42 | +// Regular expression to capture a contributor name: |
| 43 | +var RE_CONTRIBUTOR = /^(.+)\s<.+>$/; |
| 44 | + |
| 45 | + |
| 46 | +// MAIN // |
| 47 | + |
| 48 | +/** |
| 49 | +* Main execution sequence. |
| 50 | +* |
| 51 | +* @private |
| 52 | +* @returns {void} |
| 53 | +*/ |
| 54 | +function main() { |
| 55 | + var fpath; |
| 56 | + var opts; |
| 57 | + var file; |
| 58 | + var json; |
| 59 | + var line; |
| 60 | + var err; |
| 61 | + var m; |
| 62 | + var i; |
| 63 | + |
| 64 | + debug( 'Resolving CONTRIBUTORS file.' ); |
| 65 | + opts = { |
| 66 | + 'dir': __dirname |
| 67 | + }; |
| 68 | + fpath = resolveParentPath( 'CONTRIBUTORS', opts ); |
| 69 | + if ( fpath === null ) { |
| 70 | + err = new Error( 'unexpected error. Unable to resolve a CONTRIBUTORS file.' ); |
| 71 | + debug( 'Error: %s', err.message ); |
| 72 | + console.error( 'Error: %s', err.message ); // eslint-disable-line no-console |
| 73 | + return; |
| 74 | + } |
| 75 | + debug( 'Successfully resolved a CONTRIBUTORS file: %s', fpath ); |
| 76 | + |
| 77 | + debug( 'Reading file.' ); |
| 78 | + opts = { |
| 79 | + 'encoding': 'utf8' |
| 80 | + }; |
| 81 | + fpath = path.resolve( __dirname, fpath ); |
| 82 | + file = readFile( fpath, opts ); |
| 83 | + if ( file instanceof Error ) { |
| 84 | + debug( 'Error: %s', file.message ); |
| 85 | + console.error( 'Error: %s', file.message ); // eslint-disable-line no-console |
| 86 | + } |
| 87 | + debug( 'Successfully read file.' ); |
| 88 | + |
| 89 | + debug( 'Extracting contributors.' ); |
| 90 | + file = file.split( RE_EOL ); |
| 91 | + json = []; |
| 92 | + for ( i = 0; i < file.length; i++ ) { |
| 93 | + line = file[ i ]; |
| 94 | + |
| 95 | + // Skip comment lines... |
| 96 | + if ( RE_COMMENT.test( line ) ) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + // Skip empty lines... |
| 100 | + if ( trim( line ) === '' ) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + // Extract contributor... |
| 104 | + m = line.match( RE_CONTRIBUTOR ); |
| 105 | + if ( m ) { |
| 106 | + json.push( m[ 1 ] ); |
| 107 | + } else { |
| 108 | + debug( 'Unable to extract contributor. Line: %s', line ); |
| 109 | + } |
| 110 | + } |
| 111 | + debug( 'Contributor list: %s', JSON.stringify( json ) ); |
| 112 | + |
| 113 | + debug( 'Writing to file.' ); |
| 114 | + opts = { |
| 115 | + 'encoding': 'utf8' |
| 116 | + }; |
| 117 | + writeFile( OUTPUT_FILE, JSON.stringify( json )+'\n', opts ); |
| 118 | +} |
| 119 | + |
| 120 | +main(); |
0 commit comments