Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Super mod: Update license year
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