UNPKG

3.01 kBtext/coffeescriptView Raw
1###
2Copyright (c) 2014, Sean Massa
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
9Redistributions of source code must retain the above copyright notice,
10this list of conditions and the following disclaimer.
11
12Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
16Neither the name of GROUPON nor the names of its contributors may be
17used to endorse or promote products derived from this software without
18specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31###
32
33fs = require 'fs'
34glob = require 'globber'
35debug = require('debug') 'license'
36
37SOURCE_FILES = {
38 'coffee':
39 startComment: '###'
40 endComment: '###'
41 'js':
42 startComment: '/*'
43 endComment: '*/'
44}
45
46module.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
57readFile = (filePath) ->
58 if (fs.existsSync filePath)
59 buffer = fs.readFileSync filePath
60 buffer.toString()
61
62getExtension = (path) ->
63 path.split('.').pop()
64
65getSourceFiles = (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
81ensureLicense = (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
92prepend = (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