-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [v5 breaking change] change sort and aggregation fields API (#3…
- Loading branch information
Showing
16 changed files
with
1,766 additions
and
263 deletions.
There are no files selected for viewing
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
62 changes: 32 additions & 30 deletions
62
packages/gatsby-codemods/src/bin/__tests__/gatsby-codemods-test.js
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
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.` | ||
) | ||
}) | ||
}) |
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
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) | ||
} |
Oops, something went wrong.