Last active
August 7, 2017 18:01
-
-
Save ianwremmel/ed0b553e63dbae21ec320c55683ebbfa to your computer and use it in GitHub Desktop.
codemod: add header banner
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
/** | |
* Adds a copyright banner to all files, removing any copyright banner that may | |
* have already existed. | |
* | |
* Note: You'll need to run `eslint --fix` afterwards to remove the stray | |
* whitespace that this transform adds. | |
*/ | |
module.exports = function transform({source}, {jscodeshift}) { | |
const j = jscodeshift; | |
// apply trim to remove any leading whitespace that may already exist | |
let root = jscodeshift(source.trim()); | |
const banner = j.commentBlock(`!\n * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.\n `); | |
root | |
.find(j.Program) | |
.forEach((path) => { | |
const body = path.node.body; | |
const first = body[0]; | |
if (first.comments && (first.comments[0].value.startsWith(`!`) || first.comments[0].value.startsWith(`*!`))) { | |
first.comments.shift(); | |
} | |
}); | |
root = jscodeshift(root.toSource().trim()); | |
root | |
.find(j.Program) | |
.forEach((path) => { | |
const comments = path.node.comments = path.node.comments || []; | |
comments.push(banner); | |
const body = path.node.body; | |
body.unshift(` `); | |
}); | |
return `${root.toSource().trim()}\n`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: causes destructured imports to all be on one line which, for reasons unknown, may break the istanbul coverage reporter.