Last active
August 9, 2017 17:00
-
-
Save andrejewski/8d0b4927f73978e78b0105f84ad8abd4 to your computer and use it in GitHub Desktop.
Super mod: Update license year
This file contains hidden or 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
var fs = require('fs') | |
var path = require('path') | |
module.exports = { | |
getOptions: function () { | |
return [] | |
}, | |
run: function (directory) { | |
var potentialNames = ['LICENSE', 'LICENSE.md', 'license', 'license.md'] | |
var filepath | |
for (var i = 0; i < potentialNames.length; i++) { | |
var testFilepath = path.join(directory, potentialNames[i]) | |
if (fs.existsSync(testFilepath)) { | |
filepath = testFilepath | |
break | |
} | |
} | |
if (!filepath) { | |
console.error('"' + filepath + '" was not found, cannot update license year') | |
return Promise.resolve() | |
} | |
return new Promise(function (resolve, reject) { | |
fs.readFile(filepath, {encoding: 'utf8'}, function (error, text) { | |
error ? reject(error) : resolve(text) | |
}) | |
}).then(function (text) { | |
var year = (new Date).getFullYear() | |
var newCopyrightDate = '(c) ' + year | |
var newText = text.replace(/\(c\) 20\d\d/, newCopyrightDate) | |
if (newText === text) { | |
if (newText.indexOf(newCopyrightDate) !== -1) { | |
console.error('Copyright year is already this year, nothing to update.') | |
} else { | |
console.error('Copyright year was not found, unable to update.') | |
} | |
} | |
return newText | |
}).then(function (newText) { | |
return new Promise(function (resolve, reject) { | |
fs.writeFile(filepath, newText, function (error) { | |
error ? reject(error) : resolve() | |
}) | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment