Last active
October 16, 2019 22:55
-
-
Save FermiDirak/f1cba0264930cd16c6a73ba24b4c7bcf to your computer and use it in GitHub Desktop.
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
/** | |
* TEAM: frontend_infra | |
* | |
* @flow | |
*/ | |
/* eslint-disable no-console */ | |
/** | |
* This script will force relative imports to be fully | |
* specified imports. For instance, ./ComponentName might | |
* get turned into components/base_candidate/ComponentName. | |
*/ | |
import {type FileInfo, type Api, type Options} from "./codemodTypes"; | |
function isAbsolute(path: string) { | |
return path[0] !== "." | |
} | |
export default function( | |
file: FileInfo, | |
{jscodeshift: j}: Api, | |
_options: Options | |
) { | |
const importDecs = []; | |
const tree = j(file.source); | |
tree | |
.find(j.ImportDeclaration) | |
.forEach(importDeclaration => { | |
importDecs.push(importDeclaration); | |
}); | |
let last = 0; | |
console.log(importDecs[0].value.loc.start.line) | |
console.log(importDecs[0].value.loc.end.line) | |
for (let i = 0; i < importDecs.length; ++i) { | |
const importDec = importDecs[i]; | |
const source = importDec.value.source.value; | |
if (isAbsolute(source)) { | |
const tempSpecifier = importDecs[last].value.specifiers; | |
const tempSource = importDecs[last].value.source; | |
importDecs[last].value.specifiers = importDecs[i].value.specifiers; | |
importDecs[last].value.source = importDecs[i].value.source; | |
importDecs[i].value.specifiers = tempSpecifier; | |
importDecs[i].value.source = tempSource; | |
last += 1; | |
} | |
} | |
return tree.toSource(); | |
} | |
export const parser = "flow"; | |
---------------------------------------- | |
/** | |
* TEAM: frontend_infra | |
* | |
* @flow | |
*/ | |
/* eslint-disable no-console */ | |
/** | |
* This script will force relative imports to be fully | |
* specified imports. For instance, ./ComponentName might | |
* get turned into components/base_candidate/ComponentName. | |
*/ | |
import {type FileInfo, type Api, type Options} from "./codemodTypes"; | |
const RELATIVE_IMPORT = /^\.\.?\//; | |
const path = require("path"); | |
const ignore = [/flow-typed/, /\/node_modules\//, /fixture.js/]; | |
const NODE_MODULES = [ | |
"@storybook/addon-actions", | |
"@storybook/addon-links", | |
"@storybook/react", | |
"aphrodite", | |
"babel-eslint", | |
"babel-jest", | |
"babel-loader", | |
"babel-plugin-add-react-displayname", | |
"babel-plugin-dynamic-import-node", | |
"babel-plugin-idx", | |
"babel-plugin-prismjs", | |
"babel-plugin-relay", | |
"babel-plugin-remove-test-ids", | |
"classlist-polyfill", | |
"classnames", | |
"codecov", | |
"enzyme", | |
"enzyme-adapter-react-16", | |
"enzyme-to-json", | |
"eslint", | |
"eslint-config-airbnb", | |
"eslint-config-prettier", | |
"eslint-import-resolver-webpack", | |
"eslint-plugin-flowtype", | |
"eslint-plugin-import", | |
"eslint-plugin-jest", | |
"eslint-plugin-jsx-a11y", | |
"eslint-plugin-lodash", | |
"eslint-plugin-react", | |
"eslint-plugin-react-hooks", | |
"fetch-mock", | |
"flow-bin", | |
"flow-remove-types", | |
"flux", | |
"formula-one", | |
"found", | |
"graphql", | |
"i18next", | |
"idx", | |
"immutability-helper", | |
"inline-style-prefixer", | |
"jest", | |
"jsverify", | |
"keymaster", | |
"keymirror", | |
"lodash", | |
"math-expression-evaluator", | |
"moment", | |
"moment-timezone", | |
"murmurhash-js", | |
"parse-decimal-number", | |
"positions", | |
"prettier", | |
"prismjs", | |
"prop-types", | |
"raven-js", | |
"rc-tooltip", | |
"react", | |
"react-addons-shallow-compare", | |
"react-datepicker-hackerone", | |
"react-dom", | |
"react-dotdotdot", | |
"react-dropzone", | |
"react-element-to-jsx-string", | |
"react-highlight", | |
"react-hot-loader", | |
"react-infinite", | |
"react-live", | |
"react-loadable", | |
"react-markdown", | |
"react-modal", | |
"react-onclickoutside", | |
"react-pdf", | |
"react-popper", | |
"react-relay", | |
"react-select", | |
"react-simple-code-editor", | |
"react-test-renderer", | |
"react-transition-group", | |
"react-virtualized-auto-sizer", | |
"react-window", | |
"react-window-infinite-loader", | |
"reflective-bind", | |
"scrollparent", | |
"terser-webpack-plugin", | |
"url-polyfill", | |
"uuid", | |
"webpack", | |
"webpack-bundle-analyzer", | |
"webpack-manifest-plugin", | |
"webpack-plugin-hash-output", | |
"webpack-shell-plugin", | |
"xss", | |
"@babel/cli", | |
"@babel/core", | |
"@babel/node", | |
"@babel/plugin-proposal-class-properties", | |
"@babel/plugin-proposal-decorators", | |
"@babel/plugin-proposal-export-namespace-from", | |
"@babel/plugin-proposal-function-sent", | |
"@babel/plugin-proposal-json-strings", | |
"@babel/plugin-proposal-nullish-coalescing-operator", | |
"@babel/plugin-proposal-numeric-separator", | |
"@babel/plugin-proposal-optional-chaining", | |
"@babel/plugin-proposal-throw-expressions", | |
"@babel/plugin-syntax-dynamic-import", | |
"@babel/plugin-syntax-import-meta", | |
"@babel/plugin-transform-runtime", | |
"@babel/preset-env", | |
"@babel/preset-flow", | |
"@babel/preset-react", | |
"@babel/runtime", | |
"@percy/storybook", | |
"@storybook/addon-actions", | |
"@storybook/addon-knobs", | |
"@storybook/addon-links", | |
"@storybook/addons", | |
"@storybook/cli", | |
"babel-plugin-lodash", | |
"chunkd", | |
"ci-parallel-vars", | |
"css-loader", | |
"eslint-plugin-autofix", | |
"flow-node", | |
"husky", | |
"jest-environment-jsdom", | |
"jest-environment-jsdom-global", | |
"relay-compiler", | |
"style-loader", | |
"thread-loader", | |
"webpack-cli", | |
"webpack-dev-server", | |
"yarnhook", | |
// builtins | |
"events", | |
]; | |
function isNodeModule(path: string) { | |
return NODE_MODULES.some(m => { | |
if (m === path) return true; | |
if (path.startsWith(`${m}/`)) return true; | |
return false; | |
}); | |
} | |
function getRelativePath(filePath: string, importPath: string) { | |
const relativePath = path.relative(path.dirname(filePath), importPath); | |
if (!relativePath.startsWith(".")) { | |
return `./${relativePath}`; | |
} | |
return relativePath; | |
} | |
export default function( | |
fileInfo: FileInfo, | |
{jscodeshift: j}: Api, | |
_options: Options | |
) { | |
const filePath = fileInfo.path; | |
if (ignore.some(i => i.test(filePath))) { | |
console.log(`Ignoring ${filePath}`); | |
return null; | |
} | |
const matches = filePath.match(/^..\/latitude\/src\/(.+)$/); | |
if (matches == null) { | |
console.log(`Not sure how to update imports for file "${filePath}"`); | |
return null; | |
} | |
const filePath2 = matches[1]; | |
const root = j(fileInfo.source); | |
root | |
.find(j.ImportDeclaration) | |
.find(j.Literal) | |
.filter(importDec => { | |
const {node} = importDec; | |
const importPath = node.value; | |
if (RELATIVE_IMPORT.test(importPath)) { | |
// console.log(`Skipping already relative path ${importPath}`); | |
return false; | |
} | |
if (isNodeModule(importPath)) { | |
// console.log(`Skipping node module import "${importPath}"`); | |
return false; | |
} | |
return true; | |
}) | |
.replaceWith(importDec => { | |
const {node} = importDec; | |
const importPath = node.value; | |
const relativePath = getRelativePath(filePath2, importPath); | |
console.log( | |
`Replacing "${filePath2}" import "${importPath}" with "${relativePath}"` | |
); | |
node.value = relativePath; | |
return node; | |
}); | |
return root.toSource(); | |
} | |
export const parser = "flow"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment