Skip to content

Commit

Permalink
feat: [v5 breaking change] change sort and aggregation fields API (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Sep 30, 2022
1 parent 2aa8325 commit 275b356
Show file tree
Hide file tree
Showing 16 changed files with 1,766 additions and 263 deletions.
7 changes: 4 additions & 3 deletions packages/gatsby-codemods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "A collection of codemod scripts for use with JSCodeshift that help migrate to newer versions of Gatsby.",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir . --ignore \"**/__tests__\"",
"watch": "babel -w src --out-dir . --ignore \"**/__tests__\"",
"build": "babel src --out-dir . --ignore \"**/__tests__\" --extensions \".ts,.tsx,.js\"",
"watch": "babel -w src --out-dir . --ignore \"**/__tests__\" --extensions \".ts,.tsx,.js\"",
"prepare": "cross-env NODE_ENV=production npm run build"
},
"keywords": [
Expand Down Expand Up @@ -36,11 +36,12 @@
},
"devDependencies": {
"@babel/cli": "^7.15.4",
"@types/jscodeshift": "^0.11.5",
"babel-preset-gatsby-package": "^2.25.0-next.0",
"cross-env": "^7.0.3"
},
"engines": {
"node": ">=14.15.0"
},
"bin": "./bin/gatsby-codemods.js"
}
}
62 changes: 32 additions & 30 deletions packages/gatsby-codemods/src/bin/__tests__/gatsby-codemods-test.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
let execaReturnValue

jest.setMock('execa', {
sync: () => execaReturnValue
});
jest.setMock("execa", {
sync: () => execaReturnValue,
})

import process from 'process'
import path from 'path'
import fs from 'fs'
import process from "process"
import path from "path"
import fs from "fs"

const {
runTransform,
run,
transformerDirectory,
jscodeshiftExecutable
} = require('../cli');

describe('transform', () => {
jscodeshiftExecutable,
} = require("../cli")

it('finds transformer directory', () => {
describe("transform", () => {
it("finds transformer directory", () => {
fs.lstatSync(transformerDirectory)
})

it('finds jscodeshift executable', () => {
it("finds jscodeshift executable", () => {
fs.lstatSync(jscodeshiftExecutable)
})

it('runs jscodeshift for the given transformer', () => {
execaReturnValue = { error: null };
console.log = jest.fn();
it("runs jscodeshift for the given transformer", () => {
execaReturnValue = { error: null }
console.log = jest.fn()
runTransform(`gatsby-plugin-image`, `src`)

expect(console.log).toBeCalledWith(
`Executing command: jscodeshift --ignore-pattern=**/node_modules/** --ignore-pattern=**/.cache/** --ignore-pattern=**/public/** --extensions=jsx,js,ts,tsx --transform ${path.join(transformerDirectory, 'gatsby-plugin-image.js')} src`
`Executing command: jscodeshift --ignore-pattern=**/node_modules/** --ignore-pattern=**/.cache/** --ignore-pattern=**/public/** --extensions=jsx,js,ts,tsx --transform ${path.join(
transformerDirectory,
"gatsby-plugin-image.js"
)} src`
)
})

it('warns when on missing transform', () => {
execaReturnValue = { error: null };
console.log = jest.fn();
it("warns when on missing transform", () => {
execaReturnValue = { error: null }
console.log = jest.fn()
process.argv = [`node`, `dir`]
run()

expect(console.log).toBeCalledWith(
`Be sure to pass in the name of the codemod you're attempting to run.`
)
})

it('warns when on missing target', () => {
execaReturnValue = { error: null };
console.log = jest.fn();
it("warns when on missing target", () => {
execaReturnValue = { error: null }
console.log = jest.fn()
process.argv = [`node`, `dir`, `gatsby-plugin-image`]
run()

expect(console.log).toBeCalledWith(
`You have not provided a target directory to run the codemod against, will default to root.`
)
})

it('warns when invalid transform', () => {
execaReturnValue = { error: null };
console.log = jest.fn();
it("warns when invalid transform", () => {
execaReturnValue = { error: null }
console.log = jest.fn()
process.argv = [`node`, `dir`, `does-not-exist`]
run()

expect(console.log).toBeCalledWith(
`You have passed in invalid codemod name: does-not-exist. Please pass in one of the following: gatsby-plugin-image, global-graphql-calls, import-link, navigate-calls, rename-bound-action-creators.`
`You have passed in invalid codemod name: does-not-exist. Please pass in one of the following: gatsby-plugin-image, global-graphql-calls, import-link, navigate-calls, rename-bound-action-creators, sort-and-aggr-graphql.`
)
})
})
57 changes: 36 additions & 21 deletions packages/gatsby-codemods/src/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
import path from 'path'
import execa from 'execa'
import path from "path"
import execa from "execa"

const codemods = [`gatsby-plugin-image`, `global-graphql-calls`, `import-link`, `navigate-calls`, `rename-bound-action-creators`]
const codemods = [
`gatsby-plugin-image`,
`global-graphql-calls`,
`import-link`,
`navigate-calls`,
`rename-bound-action-creators`,
`sort-and-aggr-graphql`,
]

export const transformerDirectory = path.join(__dirname, '../', 'transforms')
export const jscodeshiftExecutable = require.resolve('.bin/jscodeshift')
export const transformerDirectory = path.join(__dirname, `../`, `transforms`)
export const jscodeshiftExecutable = require.resolve(`.bin/jscodeshift`)

export function runTransform(transform, targetDir) {
const transformerPath = path.join(transformerDirectory, `${transform}.js`)

let args = []

args.push('--ignore-pattern=**/node_modules/**')
args.push('--ignore-pattern=**/.cache/**')
args.push('--ignore-pattern=**/public/**')
args.push(`--ignore-pattern=**/node_modules/**`)
args.push(`--ignore-pattern=**/.cache/**`)
args.push(`--ignore-pattern=**/public/**`)

args.push('--extensions=jsx,js,ts,tsx')

args = args.concat(['--transform', transformerPath, targetDir])
args.push(`--extensions=jsx,js,ts,tsx`)

console.log(`Executing command: jscodeshift ${args.join(' ')}`);
args = args.concat([`--transform`, transformerPath, targetDir])

const result = execa.sync(jscodeshiftExecutable, args, {
stdio: 'inherit',
stripEof: false
console.log(`Executing command: jscodeshift ${args.join(` `)}`)

const pathToNodeBin = process.argv[0]
const result = execa.sync(pathToNodeBin, args, {
stdio: `inherit`,
stripEof: false,
})

if (result.error) {
throw result.error
}
}

export function run() {
let [transform, targetDir] = process.argv.slice(2)

if (!transform) {
console.log(`Be sure to pass in the name of the codemod you're attempting to run.`)
console.log(
`Be sure to pass in the name of the codemod you're attempting to run.`
)
return
}

if (!codemods.includes(transform)) {
console.log(`You have passed in invalid codemod name: ${transform}. Please pass in one of the following: ${codemods.join(", ")}.`)
console.log(
`You have passed in invalid codemod name: ${transform}. Please pass in one of the following: ${codemods.join(
", "
)}.`
)
return
}

if(!targetDir) {
console.log(`You have not provided a target directory to run the codemod against, will default to root.`)
if (!targetDir) {
console.log(
`You have not provided a target directory to run the codemod against, will default to root.`
)
targetDir = `./`

}
runTransform(transform, targetDir)
}
Loading

0 comments on commit 275b356

Please sign in to comment.