1 | ###
|
2 | Copyright (c) 2014, Sean Massa
|
3 | All rights reserved.
|
4 |
|
5 | Redistribution and use in source and binary forms, with or without
|
6 | modification, are permitted provided that the following conditions
|
7 | are met:
|
8 |
|
9 | Redistributions of source code must retain the above copyright notice,
|
10 | this list of conditions and the following disclaimer.
|
11 |
|
12 | Redistributions in binary form must reproduce the above copyright
|
13 | notice, this list of conditions and the following disclaimer in the
|
14 | documentation and/or other materials provided with the distribution.
|
15 |
|
16 | Neither the name of GROUPON nor the names of its contributors may be
|
17 | used to endorse or promote products derived from this software without
|
18 | specific prior written permission.
|
19 |
|
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
21 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
22 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
23 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
26 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31 | ###
|
32 |
|
33 | fs = require 'fs'
|
34 | glob = require 'globber'
|
35 | debug = require('debug') 'license'
|
36 |
|
37 | SOURCE_FILES = {
|
38 | 'coffee':
|
39 | startComment: '###'
|
40 | endComment: '###'
|
41 | 'js':
|
42 | startComment: '/*'
|
43 | endComment: '*/'
|
44 | }
|
45 |
|
46 | module.exports = (directory, log, config={}) ->
|
47 | license = readFile "#{directory}/LICENSE"
|
48 | debug "has license: #{license}"
|
49 | return unless license?
|
50 |
|
51 | files = getSourceFiles(directory, config.license?.exclude)
|
52 | debug "files: #{files}"
|
53 |
|
54 | for file in files
|
55 | ensureLicense file, license, log
|
56 |
|
57 | readFile = (filePath) ->
|
58 | if (fs.existsSync filePath)
|
59 | buffer = fs.readFileSync filePath
|
60 | buffer.toString()
|
61 |
|
62 | getExtension = (path) ->
|
63 | path.split('.').pop()
|
64 |
|
65 | getSourceFiles = (directory, exclude=[]) ->
|
66 | options =
|
67 | exclude: ['node_modules'].concat(exclude)
|
68 | recursive: true
|
69 | includeDirectories: false
|
70 |
|
71 | files = glob.sync directory, options
|
72 | files = files.map (file) ->
|
73 | {
|
74 | path: file
|
75 | ext: getExtension(file)
|
76 | }
|
77 |
|
78 | files.filter (file) ->
|
79 | file.ext in (Object.keys SOURCE_FILES)
|
80 |
|
81 | ensureLicense = (file, license, log) ->
|
82 | file.content = readFile file.path
|
83 |
|
84 | newline = '\n'
|
85 | {startComment, endComment} = SOURCE_FILES[file.ext]
|
86 | license = startComment + newline + license + endComment + newline + newline
|
87 |
|
88 | if file.content.indexOf(license) != 0
|
89 | log "#{file.path}: adding license"
|
90 | prepend file, license
|
91 |
|
92 | prepend = (file, license) ->
|
93 | newFile = license + file.content
|
94 |
|
95 | tempFilePath = "#{file.path}_tmp"
|
96 | fs.writeFileSync tempFilePath, newFile
|
97 | fs.renameSync tempFilePath, file.path
|
98 |
|