Skip to content

Instantly share code, notes, and snippets.

@ianwremmel
Last active August 7, 2017 18:01
Show Gist options
  • Save ianwremmel/ed0b553e63dbae21ec320c55683ebbfa to your computer and use it in GitHub Desktop.
Save ianwremmel/ed0b553e63dbae21ec320c55683ebbfa to your computer and use it in GitHub Desktop.
codemod: add header banner
/**
* 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`;
};
@ianwremmel
Copy link
Author

Note: causes destructured imports to all be on one line which, for reasons unknown, may break the istanbul coverage reporter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment