Skip to content

Commit

Permalink
refactor(codemod): Convert codemods to commonjs (for compatibility wi…
Browse files Browse the repository at this point in the history
…th jscodeshift CLI)
  • Loading branch information
wheresrhys committed Sep 18, 2024
1 parent 90445a1 commit aad042b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/codemods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Codemods for upgrading fetch-mock",
"version": "0.0.0",
"main": "./src/index.js",
"type": "module",
"type": "commonjs",
"engines": {
"node": ">=18.11.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/codemods/src/__test__/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "module"}
8 changes: 5 additions & 3 deletions packages/codemods/src/codemods/methods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import j from 'jscodeshift';
export function getAllChainedMethodCalls(fetchMockVariableName, root) {
const j = require('jscodeshift');

function getAllChainedMethodCalls(fetchMockVariableName, root) {
return root
.find(j.CallExpression, {
callee: {
Expand All @@ -25,7 +26,8 @@ export function getAllChainedMethodCalls(fetchMockVariableName, root) {
});
}

export function simpleMethods(fetchMockVariableName, root) {
module.exports.getAllChainedMethodCalls = getAllChainedMethodCalls;
module.exports.simpleMethods = function(fetchMockVariableName, root) {
const fetchMockMethodCalls = getAllChainedMethodCalls(
fetchMockVariableName,
root,
Expand Down
6 changes: 3 additions & 3 deletions packages/codemods/src/codemods/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import j from 'jscodeshift';
import { getAllChainedMethodCalls } from './methods.js';
const j = require('jscodeshift');
const { getAllChainedMethodCalls } = require('./methods.js');
const simpleOptionNames = ['overwriteRoutes', 'warnOnFallback', 'sendAsJson'];

function appendError(message, path) {
Expand All @@ -9,7 +9,7 @@ function appendError(message, path) {
j(`throw new Error("${message}")`).find(j.ThrowStatement).get().value,
);
}
export function simpleOptions(fetchMockVariableName, root) {
module.exports.simpleOptions = function(fetchMockVariableName, root) {
const configSets = root
.find(j.CallExpression, {
callee: {
Expand Down
13 changes: 8 additions & 5 deletions packages/codemods/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import j from 'jscodeshift';
import { simpleOptions } from './codemods/options.js';
import { simpleMethods } from './codemods/methods.js';
const j = require( 'jscodeshift');
const { simpleOptions } = require( './codemods/options.js');
const { simpleMethods } = require( './codemods/methods.js');

function findFetchMockVariableName(root) {
let fetchMockVariableName;
Expand Down Expand Up @@ -29,7 +29,7 @@ function findFetchMockVariableName(root) {
return fetchMockVariableName;
}

export function codemod(source, variableName) {
function codemod(source, variableName) {
const root = j(source);
const fetchMockVariableName = variableName || findFetchMockVariableName(root);
simpleMethods(fetchMockVariableName, root);
Expand All @@ -40,7 +40,7 @@ export function codemod(source, variableName) {
return root.toSource();
}

export default function transformer(file, api) {
function transformer(file, api) {
let modifiedSource = codemod(file.source);
if (process.env.FM_VARIABLES) {
const extraVariables = process.env.FM_VARIABLES.split(',');
Expand All @@ -50,3 +50,6 @@ export default function transformer(file, api) {
}
return modifiedSource;
}

module.exports = transformer;
module.exports.codemod = codemod;

0 comments on commit aad042b

Please sign in to comment.