Add a --quiet flag to the CLI

Partially addresses #105
This commit is contained in:
Natalie Weizenbaum 2018-03-11 21:04:46 -07:00
parent 4c6494c288
commit b93fcdb5ed
4 changed files with 56 additions and 7 deletions

View File

@ -6,6 +6,8 @@
* Add a `--load-path` command-line option (alias `-I`) for passing additional
paths to search for Sass files to import.
* Add a `--quiet` command-line option (alias `-q`) for silencing warnings.
### Dart API
* Add a `Logger` class that allows users to control how messages are printed by

View File

@ -31,6 +31,7 @@ main(List<String> args) async {
allowed: ['expanded', 'compressed'],
defaultsTo: 'expanded')
..addFlag('color', abbr: 'c', help: 'Whether to emit terminal colors.')
..addFlag('quiet', abbr: 'q', help: "Don't print warnings.")
..addFlag('trace', help: 'Print full Dart stack traces for exceptions.')
..addFlag('help',
abbr: 'h', help: 'Print this usage information.', negatable: false)
@ -67,6 +68,8 @@ main(List<String> args) async {
var color =
options.wasParsed('color') ? options['color'] as bool : hasTerminal;
var logger =
options['quiet'] as bool ? Logger.quiet : new Logger.stderr(color: color);
var style = options['style'] == 'compressed'
? OutputStyle.compressed
: OutputStyle.expanded;
@ -76,17 +79,24 @@ main(List<String> args) async {
String css;
if (stdinFlag) {
css = await _compileStdin(
style: style, loadPaths: loadPaths, asynchronous: asynchronous);
logger: logger,
style: style,
loadPaths: loadPaths,
asynchronous: asynchronous);
} else {
var input = options.rest.first;
if (input == '-') {
css = await _compileStdin(
style: style, loadPaths: loadPaths, asynchronous: asynchronous);
logger: logger,
style: style,
loadPaths: loadPaths,
asynchronous: asynchronous);
} else if (asynchronous) {
css = await compileAsync(input,
color: color, style: style, loadPaths: loadPaths);
logger: logger, style: style, loadPaths: loadPaths);
} else {
css = compile(input, color: color, style: style, loadPaths: loadPaths);
css =
compile(input, logger: logger, style: style, loadPaths: loadPaths);
}
}
@ -150,7 +160,7 @@ Future<String> _loadVersion() async {
/// Compiles Sass from standard input and returns the result.
Future<String> _compileStdin(
{bool color: false,
{Logger logger,
OutputStyle style,
List<String> loadPaths,
bool asynchronous: false}) async {
@ -158,10 +168,10 @@ Future<String> _compileStdin(
var importer = new FilesystemImporter('.');
if (asynchronous) {
return await compileStringAsync(text,
color: color, style: style, importer: importer, loadPaths: loadPaths);
logger: logger, style: style, importer: importer, loadPaths: loadPaths);
} else {
return compileString(text,
color: color, style: style, importer: importer, loadPaths: loadPaths);
logger: logger, style: style, importer: importer, loadPaths: loadPaths);
}
}

View File

@ -13,6 +13,9 @@ import 'utils.dart';
///
/// This may be implemented by user code.
abstract class Logger {
/// A logger that silently ignores all messages.
static final Logger quiet = new _QuietLogger();
/// Creates a logger that prints warnings to standard error, with terminal
/// colors if [color] is `true` (default `false`).
const factory Logger.stderr({bool color}) = _StderrLogger;

View File

@ -168,6 +168,40 @@ void sharedTests(Future<TestProcess> runSass(Iterable<String> arguments)) {
await sass.shouldExit(0);
});
group("with --quiet", () {
test("doesn't emit @warn", () async {
await d.file("test.scss", "@warn heck").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
test("doesn't emit @debug", () async {
await d.file("test.scss", "@debug heck").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
test("doesn't emit parser warnings", () async {
await d.file("test.scss", "a {b: c && d}").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
test("doesn't emit runner warnings", () async {
await d.file("test.scss", "#{blue} {x: y}").create();
var sass = await runSass(["--quiet", "test.scss"]);
expect(sass.stderr, emitsDone);
await sass.shouldExit(0);
});
});
group("reports errors", () {
test("from invalid arguments", () async {
var sass = await runSass(["--asdf"]);