1
+ /**
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2017 Mickael Jeanroy
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+
25
+ 'use strict' ;
26
+
27
+ var fs = require ( 'fs' ) ;
28
+ var path = require ( 'path' ) ;
29
+ var _ = require ( 'lodash' ) ;
30
+ var moment = require ( 'moment' ) ;
31
+ var commenting = require ( 'commenting' ) ;
32
+ var through = require ( 'through2' ) ;
33
+ var gutil = require ( 'gulp-util' ) ;
34
+ var read = require ( './read' ) ;
35
+
36
+ module . exports = function gulpHeaderComment ( ) {
37
+ var options = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
38
+
39
+ var separator = _ . isObject ( options ) && _ . isString ( options . separator ) ? options . separator : '\n' ;
40
+ var pkgPath = path . join ( process . cwd ( ) , 'package.json' ) ;
41
+ var pkg = fs . existsSync ( pkgPath ) ? require ( pkgPath ) : { } ;
42
+
43
+ return through . obj ( function ( file , encoding , cb ) {
44
+ if ( file . isNull ( ) || file . isDirectory ( ) ) {
45
+ return cb ( null , file ) ;
46
+ }
47
+
48
+ read ( options ) . then ( function ( content ) {
49
+ var templateFn = _ . template ( content ) ;
50
+ var template = templateFn ( { _ : _ , moment : moment , pkg : pkg } ) ;
51
+ var extension = path . extname ( file . path ) ;
52
+ var header = commenting ( template . trim ( ) , { extension : extension } ) + separator ;
53
+
54
+ if ( file . isBuffer ( ) ) {
55
+ // Just prepend content.
56
+ file . contents = new Buffer ( header + file . contents ) ;
57
+ } else if ( file . isStream ( ) ) {
58
+ // Pipe to a new stream.
59
+ var stream = through ( ) ;
60
+ stream . write ( new Buffer ( header ) ) ;
61
+ file . contents = file . contents . pipe ( stream ) ;
62
+ }
63
+
64
+ cb ( null , file ) ;
65
+ } ) . catch ( function ( err ) {
66
+ // Log error.
67
+ gutil . log ( gutil . colors . red ( 'gulp-header-comment: ' + err ) ) ;
68
+
69
+ // Wrap error.
70
+ cb ( new gutil . PluginError ( 'gulp-header-comment' , err ) ) ;
71
+ } ) ;
72
+ } ) ;
73
+ } ;
0 commit comments