npub
Version:
publishing tool for your node projects hosted on github
96 lines (85 loc) • 2.51 kB
JavaScript
// Generated by CoffeeScript 1.9.1
var SOURCE_FILES, debug, ensureLicense, fs, getExtension, getSourceFiles, glob, prepend, readFile,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
fs = require('fs');
glob = require('globber');
debug = require('debug')('license');
SOURCE_FILES = {
'coffee': {
startComment: '###',
endComment: '###'
},
'js': {
startComment: '/*',
endComment: '*/'
}
};
module.exports = function(directory, log, config) {
var file, files, i, len, license, ref, results;
if (config == null) {
config = {};
}
license = readFile(directory + "/LICENSE");
debug("has license: " + license);
if (license == null) {
return;
}
files = getSourceFiles(directory, (ref = config.license) != null ? ref.exclude : void 0);
debug("files: " + files);
results = [];
for (i = 0, len = files.length; i < len; i++) {
file = files[i];
results.push(ensureLicense(file, license, log));
}
return results;
};
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 (exclude == null) {
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) {
var ref;
return ref = file.ext, indexOf.call(Object.keys(SOURCE_FILES), ref) >= 0;
});
};
ensureLicense = function(file, license, log) {
var endComment, newline, ref, startComment;
file.content = readFile(file.path);
newline = '\n';
ref = SOURCE_FILES[file.ext], startComment = ref.startComment, endComment = ref.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);
};