Skip to content

Commit 1a55f42

Browse files
committed
feat: add git status check before transformation
1 parent 60f69d6 commit 1a55f42

File tree

4 files changed

+225
-6
lines changed

4 files changed

+225
-6
lines changed

commands/transform.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { run as jscodeshift } from 'jscodeshift/src/Runner'
44
import { bold } from 'picocolors'
55
import prompts from 'prompts'
66
import { TRANSFORM_OPTIONS } from '../config'
7+
import { checkGitStatus } from '../utils/git'
78

89
export function onCancel() {
910
console.info('> Cancelled process. Program will stop now without any actions. \n')
@@ -62,13 +63,25 @@ export async function transform(codemodName?: string, source?: string, options?:
6263
process.exit(1)
6364
}
6465

66+
if (!options?.dry) {
67+
checkGitStatus(sourceSelected)
68+
}
69+
6570
const transformerPath = join(transformerDirectory, `${codemodSelected}.js`)
6671

6772
const args: Options = {
6873
...options,
6974
verbose: options?.verbose ? 2 : 0,
7075
babel: false,
71-
ignorePattern: '**/node_modules/**',
76+
ignorePattern: [
77+
'**/node_modules/**',
78+
'**/dist/**',
79+
'**/build/**',
80+
'**/*.test.*',
81+
'**/*.spec.*',
82+
'**/__tests__/**',
83+
'**/__mocks__/**',
84+
],
7285
extensions: 'cts,mts,ts,js,mjs,cjs',
7386
}
7487

package-lock.json

Lines changed: 188 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"commander": "^12.1.0",
2121
"fast-glob": "^3.3.2",
22+
"is-git-clean": "^1.1.0",
2223
"jscodeshift": "^17.1.1",
2324
"picocolors": "^1.1.1",
2425
"prompts": "^2.4.2"

utils/git.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import isGitClean from 'is-git-clean'
2+
import { yellow } from 'picocolors'
3+
4+
export function checkGitStatus(root: string) {
5+
let clean = false
6+
let errorMessage = 'Unable to determine if git directory is clean'
7+
8+
try {
9+
clean = isGitClean.sync(root)
10+
errorMessage = 'Git directory is not clean'
11+
} catch (err) {
12+
if (err?.stderr?.includes('Not a git repository')) {
13+
clean = true
14+
}
15+
}
16+
17+
if (!clean) {
18+
console.log('Thank you for using @expressjs/codemod!\n')
19+
console.log(yellow('But before we continue, please stash or commit your git changes.'))
20+
process.exit(1)
21+
}
22+
}

0 commit comments

Comments
 (0)