Skip to content

Commit

Permalink
Merge pull request #248 from kopach/force
Browse files Browse the repository at this point in the history
  • Loading branch information
kopach committed Oct 9, 2022
2 parents ca85f5f + 0579ac1 commit 41431d6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ Usage: lockfix [options]

Options:
-V, --version output the version number
-c, --commit make commit as a backup of current working directory state
-c, --commit make backup commit with revert instruction before applying changes
-f, --force bypass Git root directory check
-q, --quiet suppress output
-h, --help display help for command
```


## 📄 License [🔝](#-table-of-contents)

This software licensed under the [MIT](https://github.com/kopach/lockfix/blob/master/LICENSE)
10 changes: 8 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ program
.version(pjson.version)
.option(
'-c, --commit',
'make commit as a backup of current working directory state'
'make backup commit with revert instruction before applying changes'
)
.option('-f, --force', 'bypass Git root directory check')
.option('-q, --quiet', 'suppress output')
.parse(process.argv);

lockfix(program.commit !== undefined);
lockfix({
doCommit: program.commit !== undefined,
force: program.force !== undefined,
quiet: program.quiet !== undefined,
});
33 changes: 21 additions & 12 deletions src/lockfix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,38 @@ import { generate } from 'shortid';
import { log } from './logger';
import { relative } from 'path';

// tslint:disable-next-line: no-flag-args
export default async function lockfix(doCommit: boolean): Promise<void> {
log('🎬 Starting...');
interface LockfixProps {
doCommit: boolean;
force: boolean;
quiet: boolean;
}

export default async function lockfix({
doCommit,
force,
quiet,
}: LockfixProps): Promise<void> {
log('🎬 Starting...', { quiet });

if (!(await isGitRoot())) {
log('🤔 Not a Git root directory, exiting...');
if (!force && !(await isGitRoot())) {
log('🤔 Not a Git root directory, exiting...', { quiet });

return;
}

if (!(await isAnyUncommitedChnage())) {
log('🤔 Nothing to do for me, exiting...');
log('🤔 Nothing to do for me, exiting...', { quiet });

return;
}

log('🔁 Applying changes');
log('🔁 Applying changes', { quiet });

if (doCommit) {
await execa('git', ['add', '.']);
await execa('git', ['commit', '--no-verify', '-m', '--lockfix--']);

await printRevertInstructions();
await printRevertInstructions(quiet);
}

const commitDiff: string = (
Expand Down Expand Up @@ -60,7 +69,7 @@ export default async function lockfix(doCommit: boolean): Promise<void> {
]);
shell.rm([patchName]);

log('✅ Done');
log('✅ Done', { quiet });
}

async function isGitRoot(): Promise<boolean> {
Expand Down Expand Up @@ -89,13 +98,13 @@ function isChnagendLockFile(str: string): boolean {
);
}

async function printRevertInstructions(): Promise<void> {
async function printRevertInstructions(quiet: boolean): Promise<void> {
const commitHash: string = (await execa('git', ['rev-parse', 'HEAD'])).stdout;

log(`🔙 ${prepareRevertInstruction(commitHash)}`);
log(`🔙 ${prepareRevertInstruction(commitHash)}`, { quiet });
}

function prepareRevertInstruction(commitHash: string): string {
return `In case of neeed – use command below to revert changes done by LockFix
return `In case of need – use command below to revert changes done by LockFix
${underline.bold(`git reset --hard ${commitHash} && git reset HEAD~1`)}`;
}
10 changes: 8 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function log(...message: string[]): void {
console.log('LockFix:', ...message);
interface LogProps {
quiet?: boolean;
}

export function log(message: string, { quiet = false }: LogProps): void {
if (quiet) return;

console.log('LockFix:', message);
}

0 comments on commit 41431d6

Please sign in to comment.