Skip to content

Commit

Permalink
feat: add git status check before transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Jan 9, 2025
1 parent 60f69d6 commit 45369db
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 6 deletions.
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",
"jscodeshift": "^17.1.1",
"picocolors": "^1.1.1",
"prompts": "^2.4.2"
Expand Down
22 changes: 22 additions & 0 deletions utils/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import isGitClean from 'is-git-clean'
import { yellow } from 'picocolors'

export function checkGitStatus(root: string) {
let clean = false
let errorMessage = 'Unable to determine if git directory is clean'

try {
clean = isGitClean.sync(root)
errorMessage = 'Git directory is not clean'
} 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)
}
}

0 comments on commit 45369db

Please sign in to comment.