Skip to content

Instantly share code, notes, and snippets.

@andrejewski
Last active August 9, 2017 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrejewski/8d0b4927f73978e78b0105f84ad8abd4 to your computer and use it in GitHub Desktop.
Save andrejewski/8d0b4927f73978e78b0105f84ad8abd4 to your computer and use it in GitHub Desktop.
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