Created
May 24, 2017 12:50
-
-
Save denisborovikov/0c38799b83d75e2aef529a3560eb65d7 to your computer and use it in GitHub Desktop.
Convert recompose imports
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
module.exports = function transformer(file, api, options) { | |
const j = api.jscodeshift; | |
const root = j(file.source); | |
const printOptions = options.printOptions || { quote: 'single' }; | |
const EXCLUDE_IMPORTS = [ | |
'baconObservableConfig', | |
'flydObservableConfig', | |
'kefirObservableConfig', | |
'mostObservableConfig', | |
'rxjs4ObservableConfig', | |
'rxjsObservableConfig', | |
'xstreamObservableConfig', | |
]; | |
const modules = []; | |
const getImportModules = modules => | |
j.importDeclaration( | |
modules.sort().map(m => j.importSpecifier(j.identifier(m))), | |
j.literal('recompose') | |
); | |
return root | |
.find(j.ImportDeclaration) | |
.filter(node => node.value.source.value.indexOf('recompose') !== -1) | |
.filter(node => | |
EXCLUDE_IMPORTS.every(el => el !== node.value.specifiers[0].local.name) | |
) | |
.forEach(node => node.value.specifiers.map(m => modules.push(m.local.name))) | |
.replaceWith((_, i) => (i === 0 ? getImportModules(modules) : undefined)) | |
.toSource(printOptions); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment