-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (98 loc) · 2.95 KB
/
index.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
#! /usr/bin/env node
/* -------------------------------------------------- */
// DECLARE `ESLint` GLOBALS
/* -------------------------------------------------- */
// 'global' declarations below required to prevent ESLint errors.
/* global
process
console
*/
/* -------------------------------------------------- */
/* IMPORT MODULES */
/* -------------------------------------------------- */
// Node
const fs = require( 'fs' );
// Vendor
const recursive = require( 'recursive-readdir' );
const Promise = require( 'bluebird' );
// Project
const InputParser = require( './lib/input-parser' );
const FileParser = require( './lib/file-parser' );
const OutputParser = require( './lib/output-parser' );
/* -------------------------------------------------- */
/* DECLARE VARS */
/* -------------------------------------------------- */
let inputParser;
let fileParser;
let outputParser;
/* -------------------------------------------------- */
/* DECLARE FUNCTIONS */
/* -------------------------------------------------- */
function frontburner( args, opts ) {
// Ensure `args` data type is correct.
args = ( Array.isArray( args ) ) ? args : [ args ];
// Update global vars.
inputParser = new InputParser( args, opts );
fileParser = new FileParser();
outputParser = new OutputParser();
return new Promise( ( resolve, reject ) => {
init( args[ 0 ] )
.then( parse )
.then( log )
.then( resolve, reject );
} );
}
function init( target ) {
let filePaths = [];
return new Promise( function( resolve ) {
if ( !target ) {
throw new Error( 'Whoops! Frontburner requires at least one argument.' );
}
// Prepend current working directory if input is not an absolute path.
target = ( target.substring( 0, 1 ) !== '/' ) ? `${process.cwd()}/${target}` : target;
// Handle cases where `target` is a folder.
if ( fs.lstatSync( target ).isDirectory() ) {
recursive( target, inputParser.getSettings().excludes, function( err, files ) {
filePaths = files.map( function( path ) {
return [ path, fs.readFileSync( path, 'utf8' ) ];
} );
resolve( filePaths );
return;
} );
// Handle cases where `target` is a file.
} else {
filePaths.push( [ target, fs.readFileSync( target, 'utf8' ) ] );
resolve( filePaths );
return;
}
} );
}
function parse( filePaths ) {
let settings = inputParser.getSettings();
return new Promise( function( resolve ) {
filePaths.forEach( function( tuple ) {
let [ filePath, data ] = tuple;
fileParser.addFile( {
meta: {
path: filePath,
},
data: data,
} );
} );
resolve( fileParser.parse( settings ) );
} );
}
function log( output ) {
return new Promise( function( resolve ) {
if ( inputParser.getOption( 'display' ) ) {
console.log( output );
} else {
outputParser.writeLog( output );
}
resolve( output );
} );
}
/* -------------------------------------------------- */
/* PUBLIC API */
/* -------------------------------------------------- */
module.exports = frontburner;