-
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: temp run.js
script to quickly run local codemods
#65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
kiprasmel
wants to merge
4
commits into
hypermod-io:main
from
pipedrive:feat/temp-run-utility-to-quickly-run-local-codemods
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* a temporary utility to run local codemods quickly | ||
* before we improve stuff upstream. | ||
*/ | ||
|
||
const helpMsg = ` | ||
run.js parser=flow codemodsToRun fileOrDirectoryToModify extensions="js,jsx,ts,tsx" | ||
|
||
codemodsToRun - paths to codemods which have a codeshift.config.js file. | ||
separated by spaces. | ||
can also just provide keys from the "shorthands.json" file | ||
|
||
examples: | ||
|
||
./run.js flow ./community/@pipedrive__convention-ui-react/src/5.0.0/transform.ts ~/projects/some-project-which-uses-cui4/src/ | ||
./run.js flow cui5 ~/projects/some-project-which-uses-cui4/src/ # cui5 is from shorthands.json | ||
|
||
`; | ||
|
||
const path = require('path'); | ||
const cp = require('child_process'); | ||
|
||
const peekNextArg = () => process.argv[0]; | ||
const eatNextArg = () => process.argv.shift(); | ||
|
||
const shouldPrintHelp = () => ( | ||
(should = | ||
!process.argv.length || | ||
['-h', '--help', '-help', 'help'].includes(peekNextArg())), | ||
should && console.log(helpMsg), | ||
should | ||
); | ||
|
||
const parseArgv = () => ( | ||
process.argv.splice(0, 2), | ||
shouldPrintHelp() && process.exit(1), | ||
{ | ||
parser: eatNextArg() || 'flow', | ||
transformsToRun: parseArrayFromCsv(eatNextArg() || ''), | ||
fileOrDirectoryToModify: eatNextArg() || '', | ||
extensions: eatNextArg() || 'js,jsx,ts,tsx', | ||
} | ||
); | ||
|
||
/** | ||
* will pipe stdio as if we were running the command ourselves! | ||
* | ||
* see https://stackoverflow.com/a/47338488/9285308 | ||
* | ||
* usage: | ||
* | ||
* ```js | ||
* const command = "ls -la"; | ||
* require("child_process").execSync(command, { ...pipeStdioOpts() }); | ||
* ``` | ||
* | ||
*/ | ||
const pipeStdioOpts = (cwd = process.cwd()) => ({ cwd, stdio: 'inherit' }); | ||
|
||
run(); | ||
|
||
function run() { | ||
let { | ||
parser, // | ||
transformsToRun, | ||
fileOrDirectoryToModify, | ||
extensions, | ||
} = parseArgv(); | ||
|
||
const shorthands = require(path.join(__dirname, './shorthands.json')); | ||
console.log({ shorthands }); | ||
|
||
transformsToRun = transformsToRun | ||
.map(t => { | ||
if (t in shorthands) { | ||
return resolveTransformsFromShorthand(shorthands[t]); | ||
} else { | ||
const dir = path.dirname(t); | ||
const cmd = `yarn --cwd ${dir} build`; | ||
console.log('transform to run, build cmd', { cmd }); | ||
cp.execSync(cmd, { ...pipeStdioOpts() }); | ||
return t; | ||
} | ||
}) | ||
.flat() | ||
.join(','); | ||
|
||
const cliPath = path.join(__dirname, './packages/cli/bin/codeshift-cli.js'); | ||
|
||
const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRun} ${fileOrDirectoryToModify}`; | ||
console.log({ cmdToExec }); | ||
Comment on lines
+90
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as you can see i'm just forwarding stuff to the actual cli 😅 |
||
|
||
cp.execSync(cmdToExec, { ...pipeStdioOpts() }); | ||
} | ||
|
||
function parseArrayFromCsv(csv = '') { | ||
return csv | ||
.split(',') | ||
.filter(c => !!c) | ||
.map(c => c.trim()) | ||
.filter(c => !!c); | ||
} | ||
|
||
function resolveTransformsFromShorthand([pathToCodemodPkg, transformVersion]) { | ||
cp.execSync(`yarn --cwd ${pathToCodemodPkg} build`, { ...pipeStdioOpts() }); | ||
console.log('built'); | ||
|
||
const pathToCodemodConfig = path.join( | ||
pathToCodemodPkg, | ||
'dist', | ||
'codeshift.config.js', | ||
); | ||
console.log({ pathToCodemodConfig }); | ||
|
||
const codemodCfg = require(path.join(__dirname, pathToCodemodConfig)); | ||
|
||
const { transforms } = codemodCfg; | ||
|
||
const transformsApplicable = Object.entries(transforms) | ||
.map(([version, relPathToTransform]) => { | ||
if (version === transformVersion) { | ||
return relPathToTransform; | ||
// return path.join(pathToCodemodPkg, 'dist', relPathToTransform); // TODO must ensure it's compiled / run with ts-node / require from 'dist' | ||
} | ||
}) | ||
.filter(x => !!x); | ||
|
||
return transformsApplicable; | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"cui5": ["./community/@pipedrive__convention-ui-react", "5.0.0"] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't need this if we implemented it properly in the actual codeshift-cli because then we could just use ts-node for the source files as well, right?
thought, maybe another use case would be someone also having codemods in .ts in their own repo like thought about in #58, so perhaps we'd need to point ts-node dynamically, and not only to own our source files?