Skip to content

Commit

Permalink
feat: add quiet opion to supress output
Browse files Browse the repository at this point in the history
  • Loading branch information
kopach committed Oct 9, 2022
1 parent 6d69fda commit 0579ac1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ 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
```

Expand Down
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +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({
doCommit: program.commit !== undefined,
force: program.force !== undefined,
quiet: program.quiet !== undefined,
});
20 changes: 11 additions & 9 deletions src/lockfix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@ import { relative } from 'path';
interface LockfixProps {
doCommit: boolean;
force: boolean;
quiet: boolean;
}

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

if (!force && !(await isGitRoot())) {
log('🤔 Not a Git root directory, exiting...');
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 @@ -67,7 +69,7 @@ export default async function lockfix({
]);
shell.rm([patchName]);

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

async function isGitRoot(): Promise<boolean> {
Expand Down Expand Up @@ -96,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 0579ac1

Please sign in to comment.