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

Copy link
Member Author

Choose a reason for hiding this comment

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

@kjugi It's not something I would want to create and maintain. Having this package frees us from that work. I don't mind if you do it, but I won't be doing it.

Copy link
Member

Choose a reason for hiding this comment

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

sure, I can visit this when I have some time. If I fail I want to use execa (or similar) to keep it open for similar ideas in the future

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, you can use it, although it might be something for the future. I'd like to add this protection before launching it, if you agree. If not, we can leave it for the future.

"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