Skip to content
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

feat: add git status check before transformation #15

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion commands/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { run as jscodeshift } from 'jscodeshift/src/Runner'
import { bold } from 'picocolors'
import prompts from 'prompts'
import { TRANSFORM_OPTIONS } from '../config'
import { checkGitStatus } from '../utils/git'

const ignorePaths =
process.env.NODE_ENV === 'test'
? '**/node_modules/**'
: [
'**/node_modules/**',
'**/dist/**',
'**/build/**',
'**/*.test.*',
'**/*.spec.*',
'**/__tests__/**',
'**/__mocks__/**',
]

export function onCancel() {
console.info('> Cancelled process. Program will stop now without any actions. \n')
Expand Down Expand Up @@ -62,13 +76,17 @@ export async function transform(codemodName?: string, source?: string, options?:
process.exit(1)
}

if (!options?.dry) {
checkGitStatus(sourceSelected)
}

const transformerPath = join(transformerDirectory, `${codemodSelected}.js`)

const args: Options = {
...options,
verbose: options?.verbose ? 2 : 0,
babel: false,
ignorePattern: '**/node_modules/**',
ignorePattern: ignorePaths,
extensions: 'cts,mts,ts,js,mjs,cjs',
}

Expand Down
193 changes: 188 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"commander": "^12.1.0",
"fast-glob": "^3.3.2",
"is-git-clean": "^1.1.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to use child_process exec from nodejs instead of external lib?
https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but if this package already does its job well, why not use it?

Copy link
Member

@kjugi kjugi Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be that hard to achieve what you want with this proposal. It's probably a different story when something is complex or defeats the purpose of the library (for example commander).

If I had to choose between 10 different scripts vs 10 different packages for small or rather simple things I would pick the first option. It's better as you control this stuff and what it does.

If the given proposal with child_rocess can't be done easily we can revisit this topic

"jscodeshift": "^17.1.1",
"picocolors": "^1.1.1",
"prompts": "^2.4.2"
Expand Down
20 changes: 20 additions & 0 deletions utils/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import isGitClean from 'is-git-clean'
import { yellow } from 'picocolors'

export function checkGitStatus(root: string) {
let clean = false

try {
clean = isGitClean.sync(root)
} catch (err) {
if (err?.stderr?.includes('Not a git repository')) {
clean = true
}
}

if (!clean) {
console.log('Thank you for using @expressjs/codemod!\n')
console.log(yellow('But before we continue, please stash or commit your git changes.'))
process.exit(1)
}
}
Loading