Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #158 from mobify/update-copyright-tool
Browse files Browse the repository at this point in the history
Patch to --update flag in copyright tool
  • Loading branch information
cdok authored May 8, 2017
2 parents 8f3c08c + a024673 commit 15da548
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## To be released
- Fixes whitespace / newline management in copyright tool
- Fix to additional space added when --update flag passed into copyright tool

## v2.8.0 (May 5, 2017)
- Adds new copyright-header management tool

Expand Down
31 changes: 23 additions & 8 deletions copyright/copyright.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Copyright (c) 2017 Mobify Research & Development Inc. All rights reserved. */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
Expand Down Expand Up @@ -63,6 +62,19 @@ const buildSupportedExtensions = () => {
})
}

/**
* Removes extra \n characters from the top of any files
* to ensure more consistent spacing between copyright headers
* @param {String} content original file to edit
* @return {String} new file with no leading \n
*/
const removeLeadingNewlines = (content) => {
if (content[0] !== '') {
return content
}
content.shift()
return removeLeadingNewlines(content)
}

if (args.length === 0 || args.indexOf('--help') >= 0) {

Expand Down Expand Up @@ -105,13 +117,15 @@ args
const content = fs.readFileSync(file)
const hasCopyrightHeader = content.includes('Copyright (c)')
const ext = file.match(/\.[0-9a-z]+$/i)[0]

let newData = ''

if (hasCopyrightHeader && updateMode) {
newData = content.toString().replace(/(\(c\)\s)(\d{4})/, `$1 ${currentYear}`)
fs.writeFileSync(file, newData)
console.log(`${green}Copyright header succesfully updated to ${currentYear} in ${magenta}${file}`)
let previousHeaderYear = content.toString().match(/(?:\(c\))(?:\s)(\d{4})/)[1]
if (previousHeaderYear !== currentYear.toString()) {
newData = content.toString().replace(`(c) ${previousHeaderYear}`, `(c) ${currentYear}`)
fs.writeFileSync(file, newData)
console.log(`${green}Copyright header succesfully updated from ${previousHeaderYear} to ${currentYear} in ${magenta}${file}`)
}
}

if (!hasCopyrightHeader) {
Expand All @@ -124,10 +138,11 @@ args
// accomodate for shebang and insert before header
if (contentStr[0].indexOf('#!') >= 0) {
const shebang = contentStr.shift()
contentStr = contentStr.join('\n')
newData = shebang + '\n\n' + getHeaderText(ext) + '\n' + contentStr // eslint-disable-line prefer-template
contentStr = removeLeadingNewlines(contentStr).join('\n')
newData = shebang + '\n' + getHeaderText(ext) + '\n' + contentStr // eslint-disable-line prefer-template
} else {
newData = getHeaderText(ext) + `\n${content}` // eslint-disable-line prefer-template
contentStr = removeLeadingNewlines(contentStr).join('\n')
newData = getHeaderText(ext) + `\n${contentStr}` // eslint-disable-line prefer-template
}

fs.writeFileSync(file, newData)
Expand Down

0 comments on commit 15da548

Please sign in to comment.