UNPKG

npub

Version:

publishing tool for your node projects hosted on github

94 lines (93 loc) 2.77 kB
// Generated by CoffeeScript 2.0.0-beta7 void function () { var debug, ensureLicense, fs, getExtension, getSourceFiles, glob, log, prepend, readFile, SOURCE_FILES; fs = require('fs'); glob = require('globber'); log = require('./log'); debug = require('debug')('license'); SOURCE_FILES = { coffee: { startComment: '###', endComment: '###' }, js: { startComment: '/*', endComment: '*/' } }; module.exports = function (directory, config) { var files, license; if (null == config) config = {}; license = readFile('' + directory + '/LICENSE'); debug('has license: ' + license); if (!(null != license)) return; files = getSourceFiles(directory, null != config.license ? config.license.exclude : void 0); debug('files: ' + files); return function (accum$) { var file; for (var i$ = 0, length$ = files.length; i$ < length$; ++i$) { file = files[i$]; accum$.push(ensureLicense(file, license)); } return accum$; }.call(this, []); }; readFile = function (filePath) { var buffer; if (fs.existsSync(filePath)) { buffer = fs.readFileSync(filePath); return buffer.toString(); } }; getExtension = function (path) { return path.split('.').pop(); }; getSourceFiles = function (directory, exclude) { var files, options; if (null == exclude) exclude = []; options = { exclude: ['node_modules'].concat(exclude), recursive: true, includeDirectories: false }; files = glob.sync(directory, options); files = files.map(function (file) { return { path: file, ext: getExtension(file) }; }); return files.filter(function (file) { return in$(file.ext, Object.keys(SOURCE_FILES)); }); }; ensureLicense = function (file, license) { var cache$, endComment, newline, startComment; file.content = readFile(file.path); newline = '\n'; cache$ = SOURCE_FILES[file.ext]; startComment = cache$.startComment; endComment = cache$.endComment; license = startComment + newline + license + endComment + newline + newline; if (file.content.indexOf(license) !== 0) { log('' + file.path + ': adding license'); return prepend(file, license); } }; prepend = function (file, license) { var newFile, tempFilePath; newFile = license + file.content; tempFilePath = '' + file.path + '_tmp'; fs.writeFileSync(tempFilePath, newFile); return fs.renameSync(tempFilePath, file.path); }; function in$(member, list) { for (var i = 0, length = list.length; i < length; ++i) if (i in list && list[i] === member) return true; return false; } }.call(this);