Skip to content

Commit 8257732

Browse files
committed
feat(deps)!: Change support of DEBUG=true to VERBOSE=true by using verbose
1 parent c63b43e commit 8257732

File tree

8 files changed

+58
-59
lines changed

8 files changed

+58
-59
lines changed

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env sh
22
. "$(dirname -- "$0")/_/husky.sh"
33

4-
DEBUG=true dart bin/lint_staged.dart
4+
VERBOSE=true dart bin/lint_staged.dart

lib/lint_staged.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:verbose/verbose.dart';
2+
13
import 'src/context.dart';
24
import 'src/exception.dart';
35
import 'src/logger.dart';
@@ -34,17 +36,18 @@ Future<bool> lintStaged({
3436
return true;
3537
// ignore: empty_catches
3638
} catch (e) {
39+
final verbose = Verbose('lint_staged');
3740
if (e is LintStagedException && e.ctx.errors.isNotEmpty) {
3841
if (e.ctx.errors.contains(kConfigNotFoundError)) {
39-
logger.debug(kNoConfigurationMsg);
42+
verbose(kNoConfigurationMsg);
4043
} else if (e.ctx.errors.contains(kApplyEmptyCommitError)) {
41-
logger.debug(kPreventedEmptyCommitMsg);
44+
verbose(kPreventedEmptyCommitMsg);
4245
} else if (e.ctx.errors.contains(kGitError) &&
4346
!e.ctx.errors.contains(kGetBackupStashError)) {
44-
logger.debug(kGitErrorMsg);
47+
verbose(kGitErrorMsg);
4548
if (e.ctx.shouldBackup) {
4649
// No sense to show this if the backup stash itself is missing.
47-
logger.debug(kRestoreStashExampleMsg);
50+
verbose(kRestoreStashExampleMsg);
4851
}
4952
}
5053

lib/src/git.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'dart:io';
22

3-
import 'logger.dart';
43
import 'package:path/path.dart';
4+
import 'package:verbose/verbose.dart';
55

6-
final logger = Logger('lint_staged:execGit');
6+
final verbose = Verbose('lint_staged:execGit');
77

88
///
99
/// Get git diff arguments
@@ -45,7 +45,7 @@ Future<String> execGit(
4545
String? workingDirectory,
4646
}) async {
4747
final gitArgs = [...kNoSubmoduleRecurse, ...args];
48-
logger.debug('git ${gitArgs.join(' ')}');
48+
verbose('git ${gitArgs.join(' ')}');
4949
final result =
5050
await Process.run('git', gitArgs, workingDirectory: workingDirectory);
5151
if (result.exitCode != 0) {

lib/src/git_workflow.dart

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import 'dart:math';
2-
import 'package:lint_staged/src/logger.dart';
3-
import 'package:lint_staged/src/symbols.dart';
42
import 'package:path/path.dart';
3+
import 'package:verbose/verbose.dart';
54

5+
import 'context.dart';
66
import 'file.dart';
77
import 'git.dart';
8-
import 'context.dart';
8+
import 'symbols.dart';
99

1010
/// In git status machine output, renames are presented as `to`NUL`from`
1111
/// When diffing, both need to be taken into account, but in some cases on the `to`.
1212
final _renameRegex = RegExp(r'\x00');
1313

14-
final logger = Logger('lint_staged:GitWorkflow');
14+
final verbose = Verbose('lint_staged:GitWorkflow');
1515

1616
///
1717
/// From list of files, split renames and flatten into two files `to`NUL`from`.
@@ -120,20 +120,20 @@ class GitWorkflow {
120120
/// Get a list of unstaged deleted files
121121
///
122122
Future<List<String>> getDeletedFiles() async {
123-
logger.debug('Getting deleted files...');
123+
verbose('Getting deleted files...');
124124
final lsFiles = await execGit(['ls-files', '--deleted'],
125125
workingDirectory: workingDirectory);
126126
final files =
127127
lsFiles.split('\n').where((line) => line.trim().isNotEmpty).toList();
128-
logger.debug('Found deleted files: $files');
128+
verbose('Found deleted files: $files');
129129
return files;
130130
}
131131

132132
///
133133
/// Save meta information about ongoing git merge
134134
///
135135
Future<void> backupMergeStatus() async {
136-
logger.debug('Backing up merge state...');
136+
verbose('Backing up merge state...');
137137
await Future.wait([
138138
readFile(mergeHeadFilename, workingDirectory: workingDirectory)
139139
.then((value) => mergeHeadContent = value),
@@ -142,14 +142,14 @@ class GitWorkflow {
142142
readFile(mergeMsgFilename, workingDirectory: workingDirectory)
143143
.then((value) => mergeModeContent = value)
144144
]);
145-
logger.debug('Done backing up merge state!');
145+
verbose('Done backing up merge state!');
146146
}
147147

148148
///
149149
/// Restore meta information about ongoing git merge
150150
///
151151
Future<void> restoreMergeStatus(LintStagedContext ctx) async {
152-
logger.debug('Restoring merge state...');
152+
verbose('Restoring merge state...');
153153
try {
154154
await Future.wait([
155155
if (mergeHeadContent != null)
@@ -162,10 +162,10 @@ class GitWorkflow {
162162
writeFile(mergeMsgFilename, mergeMsgContent!,
163163
workingDirectory: workingDirectory),
164164
]);
165-
logger.debug('Done restoring merge state!');
165+
verbose('Done restoring merge state!');
166166
} catch (e) {
167-
logger.debug('Failed restoring merge state with error:');
168-
logger.debug(e.toString());
167+
verbose('Failed restoring merge state with error:');
168+
verbose(e.toString());
169169
handleError(
170170
Exception('Merge state could not be restored due to an error!'),
171171
ctx,
@@ -179,7 +179,7 @@ class GitWorkflow {
179179
/// both the "from" and "to" filenames, where "from" is no longer on disk.
180180
///
181181
Future<List<String>> getPartiallyStagedFiles() async {
182-
logger.debug('Getting partially staged files...');
182+
verbose('Getting partially staged files...');
183183
final status =
184184
await execGit(['status', '-z'], workingDirectory: workingDirectory);
185185
if (status.isEmpty) {
@@ -215,7 +215,7 @@ class GitWorkflow {
215215

216216
/// Filter empty string
217217
.toList();
218-
logger.debug('Found partially staged files: $partiallyStaged');
218+
verbose('Found partially staged files: $partiallyStaged');
219219
return partiallyStaged;
220220
}
221221

@@ -224,7 +224,7 @@ class GitWorkflow {
224224
///
225225
Future<void> prepare(LintStagedContext ctx) async {
226226
try {
227-
logger.debug('Backing up original state...');
227+
verbose('Backing up original state...');
228228
partiallyStagedFiles = await getPartiallyStagedFiles();
229229
if (partiallyStagedFiles.isNotEmpty) {
230230
ctx.hasPartiallyStagedFiles = true;
@@ -263,7 +263,7 @@ class GitWorkflow {
263263
await execGit(['stash', 'store', '--quiet', '--message', kStash, hash],
264264
workingDirectory: workingDirectory);
265265

266-
logger.debug('Done backing up original state!');
266+
verbose('Done backing up original state!');
267267
} catch (e) {
268268
handleError(e, ctx);
269269
}
@@ -291,7 +291,7 @@ class GitWorkflow {
291291
/// In case of a merge-conflict retry with 3-way merge.
292292
///
293293
Future<void> applyModifications(LintStagedContext ctx) async {
294-
logger.debug('Adding task modifications to index...');
294+
verbose('Adding task modifications to index...');
295295

296296
/// `matchedFileChunks` includes staged files that lint_staged originally detected and matched against a task.
297297
/// Add only these files so any 3rd-party edits to other files won't be included in the commit.
@@ -301,7 +301,7 @@ class GitWorkflow {
301301
await execGit(['add', '--', ...files],
302302
workingDirectory: workingDirectory);
303303
}
304-
logger.debug('Done adding task modifications to index!');
304+
verbose('Done adding task modifications to index!');
305305

306306
final stagedFilesAfterAdd = await execGit(
307307
getDiffArgs(diff: diff, diffFilter: diffFilter),
@@ -318,23 +318,22 @@ class GitWorkflow {
318318
/// 3-way merge usually fixes this, and in case it doesn't we should just give up and throw.
319319
///
320320
Future<void> resotreUnstagedChanges(LintStagedContext ctx) async {
321-
logger.debug('Restoring unstaged changes...');
321+
verbose('Restoring unstaged changes...');
322322
final unstagedPatch = getHiddenFilepath(kPatchUnstaged);
323323
try {
324324
await execGit(['apply', ...kGitApplyArgs, unstagedPatch],
325325
workingDirectory: workingDirectory);
326326
} catch (applyError) {
327-
logger.debug('Error while restoring changes:');
328-
logger.debug(applyError.toString());
329-
logger.debug('Retrying with 3-way merge');
327+
verbose('Error while restoring changes:');
328+
verbose(applyError.toString());
329+
verbose('Retrying with 3-way merge');
330330
try {
331331
// Retry with a 3-way merge if normal apply fails
332332
await execGit(['apply', ...kGitApplyArgs, '--3way', unstagedPatch],
333333
workingDirectory: workingDirectory);
334334
} catch (threeWayApplyError) {
335-
logger
336-
.debug('Error while restoring unstaged changes using 3-way merge:');
337-
logger.debug(threeWayApplyError.toString());
335+
verbose('Error while restoring unstaged changes using 3-way merge:');
336+
verbose(threeWayApplyError.toString());
338337
handleError(
339338
Exception(
340339
'Unstaged changes could not be restored due to a merge conflict!'),
@@ -349,7 +348,7 @@ class GitWorkflow {
349348
///
350349
Future<void> restoreOriginState(LintStagedContext ctx) async {
351350
try {
352-
logger.debug('Restoring original state...');
351+
verbose('Restoring original state...');
353352
await execGit(['reset', '--hard', 'HEAD'],
354353
workingDirectory: workingDirectory);
355354
await execGit(
@@ -367,7 +366,7 @@ class GitWorkflow {
367366
await removeFile(getHiddenFilepath(kPatchUnstaged),
368367
workingDirectory: workingDirectory);
369368

370-
logger.debug('Done restoring original state!');
369+
verbose('Done restoring original state!');
371370
} catch (error) {
372371
handleError(error, ctx, kRestoreOriginalStateError);
373372
}
@@ -378,10 +377,10 @@ class GitWorkflow {
378377
///
379378
Future<void> cleanup(LintStagedContext ctx) async {
380379
try {
381-
logger.debug('Dropping backup stash...');
380+
verbose('Dropping backup stash...');
382381
await execGit(['stash', 'drop', '--quiet', await getBackupStash(ctx)],
383382
workingDirectory: workingDirectory);
384-
logger.debug('Done dropping backup stash!');
383+
verbose('Done dropping backup stash!');
385384
} catch (error) {
386385
handleError(error, ctx);
387386
}

lib/src/lint_runner.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import 'dart:collection';
22
import 'dart:io';
33

4+
import 'package:verbose/verbose.dart';
5+
46
import 'context.dart';
5-
import 'logger.dart';
67
import 'symbols.dart';
78

8-
final logger = Logger('lint_staged:LintRunner');
9+
final verbose = Verbose('lint_staged:LintRunner');
910

1011
///
1112
/// `dart fix` for single file is supportted in Dart SDK 2.18
@@ -54,13 +55,13 @@ class LintRunner {
5455
} else {
5556
cmds.add(path);
5657
}
57-
logger.debug(cmds.join(' '));
58+
verbose(cmds.join(' '));
5859
final result = await Process.run(cmds.removeAt(0), cmds,
5960
workingDirectory: workingDirectory);
6061
if (result.exitCode != 0) {
6162
ctx.errors.add(kTaskError);
62-
logger.debug(result.stdout);
63-
logger.debug(result.stderr);
63+
verbose(result.stdout);
64+
verbose(result.stderr);
6465
}
6566
}
6667
}));

lib/src/logger.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import 'package:ansi/ansi.dart';
44

55
class Logger {
66
final String name;
7-
final bool isDebug;
8-
Logger(this.name) : isDebug = io.Platform.environment['DEBUG'] == 'true';
7+
Logger(this.name);
98

109
void stderr(String message) {
1110
io.stderr.writeln(ansi.red('$name $message'));
@@ -14,10 +13,4 @@ class Logger {
1413
void stdout(String message) {
1514
io.stdout.writeln('$name $message');
1615
}
17-
18-
void debug(String message) {
19-
if (isDebug) {
20-
io.stdout.writeln('$name $message');
21-
}
22-
}
2316
}

lib/src/run.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import 'dart:io';
22

33
import 'package:glob/glob.dart';
4+
import 'package:lint_staged/src/logger.dart';
5+
import 'package:verbose/verbose.dart';
46

57
import 'chunk.dart';
68
import 'config.dart';
79
import 'exception.dart';
810
import 'git.dart';
911
import 'git_workflow.dart';
1012
import 'lint_runner.dart';
11-
import 'logger.dart';
1213
import 'message.dart';
1314
import 'context.dart';
1415
import 'symbols.dart';
1516

1617
final logger = Logger('lint_staged:run');
18+
final verbose = Verbose('lint_staged:run');
1719

1820
Future<LintStagedContext> runAll({
1921
bool allowEmpty = false,
@@ -42,7 +44,7 @@ Future<LintStagedContext> runAll({
4244
/// and when using the default list of staged files by default
4345
ctx.shouldBackup = hasInitialCommit && stash;
4446
if (!ctx.shouldBackup) {
45-
logger.debug(skippingBackupMsg(hasInitialCommit, diff));
47+
verbose(skippingBackupMsg(hasInitialCommit, diff));
4648
}
4749
final stagedFiles = await getStagedFiles(
4850
diff: diff,
@@ -58,7 +60,7 @@ Future<LintStagedContext> runAll({
5860
ctx.output.add(kNoStagedFilesMsg);
5961
return ctx;
6062
}
61-
logger.debug('Loaded list of staged files int git:\n$stagedFiles');
63+
verbose('Loaded list of staged files int git:\n$stagedFiles');
6264

6365
final foundConfigs = await loadConifg(workingDirectory: workingDirectory);
6466
if (foundConfigs == null) {
@@ -69,14 +71,14 @@ Future<LintStagedContext> runAll({
6971
ctx.errors.add(kConfigEmptyError);
7072
return ctx;
7173
}
72-
logger.debug('Found configs: $foundConfigs');
74+
verbose('Found configs: $foundConfigs');
7375
final matchedFiles = <String>{};
7476
final tasks = <Future Function()>[];
7577
for (var pattern in foundConfigs.keys) {
7678
final glob = Glob(pattern);
7779
final scopedFiles = <String>{};
7880
for (var file in stagedFiles) {
79-
logger.debug('$file matches $pattern is ${glob.matches(file)}');
81+
verbose('$file matches $pattern is ${glob.matches(file)}');
8082
if (glob.matches(file)) {
8183
scopedFiles.add(file);
8284
matchedFiles.add(file);
@@ -91,10 +93,10 @@ Future<LintStagedContext> runAll({
9193
workingDirectory: workingDirectory);
9294
tasks.add(runner.run);
9395
}
94-
logger.debug('matched files: $matchedFiles');
96+
verbose('matched files: $matchedFiles');
9597
final matchedFileChunks =
9698
chunkFiles(matchedFiles.toList(), maxArgLength: maxArgLength);
97-
logger.debug('matched file chunks: $matchedFileChunks');
99+
verbose('matched file chunks: $matchedFileChunks');
98100
final git = GitWorkflow(
99101
allowEmpty: allowEmpty,
100102
gitConfigDir: await getGitConfigDir(),

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ dependencies:
1313
args: ^2.3.1
1414
glob: ^2.0.1
1515
path: ^1.8.0
16+
verbose: ^0.1.0
1617
yaml: ^3.1.1
1718

1819
dev_dependencies:
1920
commitlint_cli: ^0.3.0
20-
husky: ^0.1.4
21+
husky: ^0.1.6
2122
lints: ^2.0.0
2223
test: ^1.16.0
2324

2425
lint_staged:
25-
'lib/**.dart': dart fix --apply && dart format --fix
26+
'**.dart': dart fix --apply && dart format --fix

0 commit comments

Comments
 (0)