UNPKG

2.77 kBJavaScriptView Raw
1// Generated by CoffeeScript 2.0.0-beta7
2void function () {
3 var debug, ensureLicense, fs, getExtension, getSourceFiles, glob, log, prepend, readFile, SOURCE_FILES;
4 fs = require('fs');
5 glob = require('globber');
6 log = require('./log');
7 debug = require('debug')('license');
8 SOURCE_FILES = {
9 coffee: {
10 startComment: '###',
11 endComment: '###'
12 },
13 js: {
14 startComment: '/*',
15 endComment: '*/'
16 }
17 };
18 module.exports = function (directory, config) {
19 var files, license;
20 if (null == config)
21 config = {};
22 license = readFile('' + directory + '/LICENSE');
23 debug('has license: ' + license);
24 if (!(null != license))
25 return;
26 files = getSourceFiles(directory, null != config.license ? config.license.exclude : void 0);
27 debug('files: ' + files);
28 return function (accum$) {
29 var file;
30 for (var i$ = 0, length$ = files.length; i$ < length$; ++i$) {
31 file = files[i$];
32 accum$.push(ensureLicense(file, license));
33 }
34 return accum$;
35 }.call(this, []);
36 };
37 readFile = function (filePath) {
38 var buffer;
39 if (fs.existsSync(filePath)) {
40 buffer = fs.readFileSync(filePath);
41 return buffer.toString();
42 }
43 };
44 getExtension = function (path) {
45 return path.split('.').pop();
46 };
47 getSourceFiles = function (directory, exclude) {
48 var files, options;
49 if (null == exclude)
50 exclude = [];
51 options = {
52 exclude: ['node_modules'].concat(exclude),
53 recursive: true,
54 includeDirectories: false
55 };
56 files = glob.sync(directory, options);
57 files = files.map(function (file) {
58 return {
59 path: file,
60 ext: getExtension(file)
61 };
62 });
63 return files.filter(function (file) {
64 return in$(file.ext, Object.keys(SOURCE_FILES));
65 });
66 };
67 ensureLicense = function (file, license) {
68 var cache$, endComment, newline, startComment;
69 file.content = readFile(file.path);
70 newline = '\n';
71 cache$ = SOURCE_FILES[file.ext];
72 startComment = cache$.startComment;
73 endComment = cache$.endComment;
74 license = startComment + newline + license + endComment + newline + newline;
75 if (file.content.indexOf(license) !== 0) {
76 log('' + file.path + ': adding license');
77 return prepend(file, license);
78 }
79 };
80 prepend = function (file, license) {
81 var newFile, tempFilePath;
82 newFile = license + file.content;
83 tempFilePath = '' + file.path + '_tmp';
84 fs.writeFileSync(tempFilePath, newFile);
85 return fs.renameSync(tempFilePath, file.path);
86 };
87 function in$(member, list) {
88 for (var i = 0, length = list.length; i < length; ++i)
89 if (i in list && list[i] === member)
90 return true;
91 return false;
92 }
93}.call(this);