|
| 1 | +import j from 'jscodeshift'; |
| 2 | + |
| 3 | +module.exports = (file, api: j.API) => { |
| 4 | + const j = api.jscodeshift; |
| 5 | + const root = j(file.source); |
| 6 | + |
| 7 | + replaceImports(root, j); |
| 8 | + |
| 9 | + return root.toSource({ quote: 'single', lineTerminator: '\n' }); |
| 10 | +}; |
| 11 | + |
| 12 | +const replaceImports = (root, j) => { |
| 13 | + // Check if there is an import from react-admin |
| 14 | + const reactAdminImport = root.find(j.ImportDeclaration, { |
| 15 | + source: { |
| 16 | + value: 'react-admin', |
| 17 | + }, |
| 18 | + }); |
| 19 | + if (!reactAdminImport.length) { |
| 20 | + return root.toSource(); |
| 21 | + } |
| 22 | + |
| 23 | + // Check if there is an import of DataGrid from react-admin |
| 24 | + const datagridImport = reactAdminImport.filter(path => { |
| 25 | + return path.node.specifiers.some( |
| 26 | + specifier => |
| 27 | + j.ImportSpecifier.check(specifier) && |
| 28 | + specifier.imported.name === 'Datagrid' |
| 29 | + ); |
| 30 | + }); |
| 31 | + console.log('toto - 4', datagridImport); |
| 32 | + if (!datagridImport.length) { |
| 33 | + console.log('toto - 5 - OUT'); |
| 34 | + return root.toSource(); |
| 35 | + } |
| 36 | + |
| 37 | + // Replace import of DataGrid with DataTable |
| 38 | + reactAdminImport.replaceWith(({ node }) => |
| 39 | + j.importDeclaration( |
| 40 | + node.specifiers.map(specifier => { |
| 41 | + if ( |
| 42 | + j.ImportSpecifier.check(specifier) && |
| 43 | + specifier.imported.name === 'Datagrid' |
| 44 | + ) { |
| 45 | + return j.importSpecifier(j.identifier('DataTable')); |
| 46 | + } |
| 47 | + return specifier; |
| 48 | + }), |
| 49 | + node.source |
| 50 | + ) |
| 51 | + ); |
| 52 | +}; |
0 commit comments