Skip to content

Commit 45369db

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

File tree

4 files changed

+230
-6
lines changed

4 files changed

+230
-6
lines changed

commands/transform.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ 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'
8+
9+
const ignorePaths =
10+
process.env.NODE_ENV === 'test'
11+
? '**/node_modules/**'
12+
: [
13+
'**/node_modules/**',
14+
'**/dist/**',
15+
'**/build/**',
16+
'**/*.test.*',
17+
'**/*.spec.*',
18+
'**/__tests__/**',
19+
'**/__mocks__/**',
20+
]
721

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

79+
if (!options?.dry) {
80+
checkGitStatus(sourceSelected)
81+
}
82+
6583
const transformerPath = join(transformerDirectory, `${codemodSelected}.js`)
6684

6785
const args: Options = {
6886
...options,
6987
verbose: options?.verbose ? 2 : 0,
7088
babel: false,
71-
ignorePattern: '**/node_modules/**',
89+
ignorePattern: ignorePaths,
7290
extensions: 'cts,mts,ts,js,mjs,cjs',
7391
}
7492

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)