runCodemod.js 730 B

123456789101112131415161718192021222324252627282930
  1. const adapt = require('vue-jscodeshift-adapter')
  2. let jscodeshift = require('jscodeshift')
  3. module.exports = function runCodemod (transformModule, fileInfo, options = {}) {
  4. const transform = typeof transformModule.default === 'function'
  5. ? transformModule.default
  6. : transformModule
  7. let parser = transformModule.parser || options.parser
  8. if (!parser) {
  9. if (fileInfo.path.endsWith(('.ts'))) {
  10. parser = 'ts'
  11. } else if (fileInfo.path.endsWith('.tsx')) {
  12. parser = 'tsx'
  13. }
  14. }
  15. if (parser) {
  16. jscodeshift = jscodeshift.withParser(parser)
  17. }
  18. const api = {
  19. jscodeshift,
  20. j: jscodeshift,
  21. stats: () => {},
  22. report: () => {}
  23. }
  24. return adapt(transform)(fileInfo, api, options)
  25. }