/
output.js
123 lines (114 loc) · 3.91 KB
/
output.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
/*
anvil.output - A core anvil extension that pushes complete files to output
version: 0.1.2
author: Alex Robson <alex@sharplearningcurve.com> (http://sharplearningcurve.com)
copyright: 2011 - 2012
license: Dual licensed
MIT (http://www.opensource.org/licenses/mit-license)
GPL (http://www.opensource.org/licenses/gpl-license)
*/
var path = require( "path" );
module.exports = function( _, anvil ) {
anvil.plugin( {
name: "anvil.output",
activity: "push",
output: {},
commander: [
[ "--clean", "Cleans all output and working directories" ]
],
clean: function( done ) {
var self = this;
anvil.scheduler.parallel( this.output.full, function( directory, done ) {
directory = directory.replace( /[{]relative[}]/, "" );
anvil.log.event( "cleaning " + directory );
anvil.fs.cleanDirectory( directory, function( err ) {
if( err ) {
anvil.log.error( "Error cleaning " + directory + " : " + err.stack );
}
done();
} );
}, function() {
anvil.fs.cleanDirectory( anvil.config.working, function( err ) {
done();
} );
} );
},
configure: function( config, command, done ) {
var self = this;
this.normalizeConfig();
if( command[ "clean" ] ) {
this.clean( done );
} else {
anvil.on( "file.deleted", function( args ) {
var change = args.change,
path = args.path,
base = args.base;
if( base === anvil.config.source ) {
self['delete']( path );
}
} );
done();
}
},
copy: function( done ) {
var self = this,
list = this.getOutputList();
anvil.scheduler.parallel( list, function( spec, done ) {
var files = self.getFilesForPattern( spec.pattern );
anvil.scheduler.parallel( spec.directories, function( directory, done ) {
anvil.scheduler.parallel( files, function( file, written ) {
var relativePath = directory.replace( "{relative}", file.relativePath );
anvil.log.debug( "copying " + file.name + " to " + relativePath );
file.state = "done";
anvil.fs.copy( [ file.workingPath, file.name ], [ relativePath, file.name ], written );
}, done );
}, done );
}, done );
},
"delete": function( filePath ) {
var file = _.find( anvil.project.files, function( file ) {
return file.originalPath == filePath;
} );
if( file ) {
anvil.scheduler.parallel( [ anvil.config.output ], function( destination, done ) {
destination = path.resolve( destination );
var removeFrom = anvil.fs.buildPath( [ destination, file.relativePath, file.name ] );
anvil.log.debug( "Deleting output at " + removeFrom );
anvil.fs["delete"]( removeFrom, done );
}, function() {} );
}
},
getFilesForPattern: function( pattern ) {
return _.filter( anvil.project.files, function( file ) {
var relativePath = anvil.fs.buildPath( [ file.relativePath, file.name ] );
return !file.noCopy && anvil.fs.match( [ relativePath ], pattern, {} ).length > 0;
} );
},
getOutputList: function() {
var partials = _.map( this.output.partial, function( directories, pattern ) {
directories = _.isArray( directories ) ? directories : [ directories ];
return { pattern: pattern, directories: directories };
} );
return partials.concat( { pattern: "**/*", directories: this.output.full } );
},
normalizeConfig: function() {
var output = anvil.config.output || {};
this.output.partial = output.partial || {};
this.output.full = output.full || output;
if( _.isString( output ) ) {
this.output.full = [ output ];
} else if( _.isArray( output ) ) {
this.output.full = output;
} else {
this.output.full = _.isArray( this.output.full ) ? this.output.full : [ this.output.full ];
}
this.output.full = _.map( this.output.full, function( fullPath ) {
return fullPath.match( /[{]relative[}]/ ) ? fullPath : fullPath + "/{relative}";
} );
},
run: function( done ) {
var self = this;
self.copy( done );
}
} );
};