Created
November 3, 2013 18:06
-
-
Save kyo-ago/7292974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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