Skip to content

Instantly share code, notes, and snippets.

@kyo-ago
Created November 3, 2013 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyo-ago/7292974 to your computer and use it in GitHub Desktop.
Save kyo-ago/7292974 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = function (grunt) {
var _ = grunt.util._;
var esprima = require('esprima');
var licenseRegExp = /BSD|MIT|License/i;
grunt.registerTask('save-license', 'Save the license', function () {
this.files.forEach(function (file) {
var valid = file.src.filter(grunt.file.exists.bind(grunt.file));
_.difference(file.src, valid).forEach(function (filepath) {
grunt.log.warn('Source file "' + filepath + '" not found.');
});
var licenses = valid
.map(flookupLocenses)
.filter(function (hit) {
return hit.license;
})
;
grunt.file.write(f.dest, JSON.stringify(licenses, null, '\t'));
})
});
function lookupLocenses (src) {
var code = grunt.file.read(src);
var comments = esprima.parse(code, { 'comment' : true }).comments;
if (!comments.length) {
return;
}
var license = _.chain(comments)
.map(function (cmm) {
return cmm.value;
}).filter(function (cmm) {
return cmm;
}).find(function (cmm) {
return cmm.value.match(licenseRegExp);
}).value()
;
return {
'file' : src
'license' : license
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment