This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
59 lines (47 loc) · 2.26 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
// require modules
var fs = require( 'fs' ),
util = require( 'util' ),
path = require( 'path' ),
jsp = require( 'uglify-js' ).parser,
pro = require( 'uglify-js' ).uglify,
mkdirp = require( 'mkdirp' ),
Templ8 = require( 'Templ8' );
fs.readFile( process.argv[2] || './build.json', 'utf-8', function( err, data ) {
if ( err ) throw err;
processFiles( JSON.parse( data ) );
} );
function processFiles( params ) {
Array.isArray( params.src.files ) || ( params.src.files = params.src.files.split( /,\s?|\s+/g ) );
params.encoding || ( params.encoding = 'UTF-8' );
params.file_err || ( params.file_err = 'break' );
params.ext = params.ext.replace( /^\.+(.*)/, '$1' );
var out_dir = path.resolve( util.format( '%s/%s', process.cwd(), params.out.dir || '.' ) ) + '/',
out_file,
src_dir = path.resolve( util.format( '%s/%s', process.cwd(), params.src.dir || '.' ) ) + '/',
src = '',
src_ast;
fs.existsSync( out_dir ) || mkdirp.sync( out_dir, 0777 );
params.src.files = ( src_dir + params.src.files.join( Templ8.format( '.{0}, {1}', params.ext, src_dir ) ) + '.' + params.ext ).split( ', ' );
params.src.files.forEach( function( file ) {
console.log( timestamp(), '- processing file: ', file );
if ( !fs.existsSync( file ) ) {
console.log( 'file: ', file, ' does not exist.' );
switch ( params.file_err ) {
case 'break' : console.log( timestamp(), ' - terminating build.' ); return;
case 'continue' : break;
}
}
src += Templ8.format( '\n{0}', fs.readFileSync( file, params.encoding ) );
} );
out_file = Templ8.format( '{0}{1}.{2}', out_dir, ( params.out.file || 'out' ), params.ext );
console.log( timestamp(), '- writing file: ', out_file );
src_ast = jsp.parse( src );
fs.writeFileSync( out_file, src, params.encoding );
// fs.writeFileSync( out_file, pro.gen_code( src_ast, { beautify : true, space_colon : true } ), params.encoding );
if ( params.out.min ) {
out_file = Templ8.format( '{0}{1}.{2}', out_dir, params.out.min, params.ext );
console.log( timestamp(), '- writing minified file: ', out_file );
fs.writeFileSync( out_file, pro.gen_code( pro.ast_squeeze( pro.ast_mangle( src_ast ) ) ), params.encoding );
}
}
function timestamp() { return ( new Date() ).toISOString(); }