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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Dejno committed Oct 21, 2019
2 parents d53542c + cb74866 commit ea37647
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions utils/imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function ensureImport(j, root, name, source, local) {
const declaration = root.find(j.ImportDeclaration, {
source: { value: source },
});
const specifer = j.importSpecifier(j.identifier(name), local ? j.identifier(local) : null);
if (declaration.size()) {
if (!declaration.find(j.ImportSpecifier, { imported: { name } }).size()) {
declaration.get('specifiers').push(specifer);
}
} else {
const newImport = j.importDeclaration([specifer], j.literal(source));
root
.get('program')
.get('body')
.insertAt(0, newImport);
}
}

module.exports = { ensureImport };

0 comments on commit ea37647

Please sign in to comment.