forked from jmapio/jmap-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
133 lines (120 loc) · 3.67 KB
/
build.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
/*global require, process, console */
"use strict";
var fs = require( 'fs' );
Array.prototype.include = function ( item ) {
var i, l;
for ( i = 0, l = this.length; i < l; i += 1 ) {
if ( this[i] === item ) {
return this;
}
}
this[l] = item;
return this;
};
var stripStrict = function ( string ) {
return string.replace( /^\s*"use strict"[;,]\n?/m, '' );
};
var groupIntoModules = function ( files ) {
var modules = {};
files.forEach( function ( file ) {
var moduleName = file.module;
if ( !moduleName ) {
throw new Error( 'File ' + file.src + ' belongs to no module!' );
}
var module = modules[ moduleName ] = ( modules[ moduleName ] || {
name: moduleName,
dependencies: [],
files: []
});
module.files.push( file );
file.dependencies = file.dependencies.filter( function ( dependency ) {
if ( dependency.slice( -3 ) !== '.js' ) {
module.dependencies.include( dependency );
return false;
}
return true;
});
});
var result = [];
for ( var m in modules ) {
result.push( modules[m] );
}
return result;
};
var sort = function ( array ) {
var tree = {};
array.forEach( function ( obj ) {
tree[ obj.name ] = {
obj: obj
};
});
array.forEach( function ( obj ) {
tree[ obj.name ].dependencies =
obj.dependencies.map( function ( name ) {
var dependency = tree[ name ];
if ( !dependency ) {
console.log( obj.name + ' requires ' + name +
' but we do not have it!' );
}
return dependency;
});
});
var result = [];
var output = function output( node ) {
if ( node.isOutput ) { return; }
node.dependencies.forEach( function ( dependency ) {
output( dependency );
});
node.isOutput = true;
result.push( node.obj );
};
for ( var key in tree ) {
if ( tree.hasOwnProperty( key ) ) {
output( tree[ key ] );
}
}
return result;
};
var sortByDependencies = function ( files ) {
var parsers = {
name: /^\/\/\sFile:([^\\]+)\\\\$/m,
module: /^\/\/\sModule:([^\\]+)\\\\$/m,
dependencies: /^\/\/\sRequires:([^\\]+)\\\\$/m
};
var parsed = files.map( function ( file ) {
var info = {
data: file
};
for ( var attr in parsers ) {
var value = parsers[ attr ].exec( file ) || '';
// Get first capture group and clean it.
if ( value ) { value = value[1].replace( /\s/g, '' ); }
if ( attr === 'dependencies' ) {
value = value ? value.split( ',' ) : [];
}
info[ attr ] = value;
}
return info;
});
var modules = sort( groupIntoModules( parsed ) );
return modules.reduce( function ( array, module ) {
sort( module.files ).forEach( function ( file ) {
array.push( file.data );
});
return array;
}, [] );
};
var makeModule = function ( inputs, output ) {
// Always keep in the same order.
inputs.sort();
var module = '"use strict";\n\n';
var jsData = inputs.map( function ( input ) {
return stripStrict( fs.readFileSync( input, 'utf8' ) );
});
module += sortByDependencies( jsData ).join( '\n\n' );
fs.writeFile( output, module );
};
var args = process.argv.slice( 2 ),
sourceFiles = args.slice( 0, -1 ),
outputFiles = args[ args.length - 1 ];
makeModule( sourceFiles, outputFiles );