|
2 | 2 | // MIT-style license that can be found in the LICENSE file or at
|
3 | 3 | // https://opensource.org/licenses/MIT.
|
4 | 4 |
|
5 |
| -export 'package:sass/src/executable.dart' |
6 |
| - if (dart.library.js) 'package:sass/src/node.dart'; |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:isolate'; |
| 7 | + |
| 8 | +import 'package:path/path.dart' as p; |
| 9 | +import 'package:stack_trace/stack_trace.dart'; |
| 10 | +import 'package:term_glyph/term_glyph.dart' as term_glyph; |
| 11 | + |
| 12 | +import 'package:sass/src/exception.dart'; |
| 13 | +import 'package:sass/src/executable/compile_stylesheet.dart'; |
| 14 | +import 'package:sass/src/executable/options.dart'; |
| 15 | +import 'package:sass/src/executable/repl.dart'; |
| 16 | +import 'package:sass/src/executable/watch.dart'; |
| 17 | +import 'package:sass/src/import_cache.dart'; |
| 18 | +import 'package:sass/src/io.dart'; |
| 19 | +import 'package:sass/src/stylesheet_graph.dart'; |
| 20 | + |
| 21 | +Future<void> main(List<String> args) async { |
| 22 | + var printedError = false; |
| 23 | + |
| 24 | + // Prints [error] to stderr, along with a preceding newline if anything else |
| 25 | + // has been printed to stderr. |
| 26 | + // |
| 27 | + // If [trace] is passed, its terse representation is printed after the error. |
| 28 | + void printError(String error, StackTrace stackTrace) { |
| 29 | + if (printedError) stderr.writeln(); |
| 30 | + printedError = true; |
| 31 | + stderr.writeln(error); |
| 32 | + |
| 33 | + if (stackTrace != null) { |
| 34 | + stderr.writeln(); |
| 35 | + stderr.writeln(Trace.from(stackTrace).terse.toString().trimRight()); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + ExecutableOptions options; |
| 40 | + try { |
| 41 | + options = ExecutableOptions.parse(args); |
| 42 | + term_glyph.ascii = !options.unicode; |
| 43 | + |
| 44 | + if (options.version) { |
| 45 | + print(await _loadVersion()); |
| 46 | + exitCode = 0; |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (options.interactive) { |
| 51 | + await repl(options); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + var graph = StylesheetGraph( |
| 56 | + ImportCache([], loadPaths: options.loadPaths, logger: options.logger)); |
| 57 | + if (options.watch) { |
| 58 | + await watch(options, graph); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + for (var source in options.sourcesToDestinations.keys) { |
| 63 | + var destination = options.sourcesToDestinations[source]; |
| 64 | + try { |
| 65 | + await compileStylesheet(options, graph, source, destination, |
| 66 | + ifModified: options.update); |
| 67 | + } on SassException catch (error, stackTrace) { |
| 68 | + // This is an immediately-invoked function expression to work around |
| 69 | + // dart-lang/sdk#33400. |
| 70 | + () { |
| 71 | + try { |
| 72 | + if (destination != null && !options.emitErrorCss) { |
| 73 | + deleteFile(destination); |
| 74 | + } |
| 75 | + } on FileSystemException { |
| 76 | + // If the file doesn't exist, that's fine. |
| 77 | + } |
| 78 | + }(); |
| 79 | + |
| 80 | + printError(error.toString(color: options.color), |
| 81 | + options.trace ? stackTrace : null); |
| 82 | + |
| 83 | + // Exit code 65 indicates invalid data per |
| 84 | + // http://www.freebsd.org/cgi/man.cgi?query=sysexits. |
| 85 | + // |
| 86 | + // We let exitCode 66 take precedence for deterministic behavior. |
| 87 | + if (exitCode != 66) exitCode = 65; |
| 88 | + if (options.stopOnError) return; |
| 89 | + } on FileSystemException catch (error, stackTrace) { |
| 90 | + printError("Error reading ${p.relative(error.path)}: ${error.message}.", |
| 91 | + options.trace ? stackTrace : null); |
| 92 | + |
| 93 | + // Error 66 indicates no input. |
| 94 | + exitCode = 66; |
| 95 | + if (options.stopOnError) return; |
| 96 | + } |
| 97 | + } |
| 98 | + } on UsageException catch (error) { |
| 99 | + print("${error.message}\n"); |
| 100 | + print("Usage: sass <input.scss> [output.css]\n" |
| 101 | + " sass <input.scss>:<output.css> <input/>:<output/> <dir/>\n"); |
| 102 | + print(ExecutableOptions.usage); |
| 103 | + exitCode = 64; |
| 104 | + } catch (error, stackTrace) { |
| 105 | + var buffer = StringBuffer(); |
| 106 | + if (options != null && options.color) buffer.write('\u001b[31m\u001b[1m'); |
| 107 | + buffer.write('Unexpected exception:'); |
| 108 | + if (options != null && options.color) buffer.write('\u001b[0m'); |
| 109 | + buffer.writeln(); |
| 110 | + buffer.writeln(error); |
| 111 | + |
| 112 | + printError(buffer.toString(), stackTrace); |
| 113 | + exitCode = 255; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/// Loads and returns the current version of Sass. |
| 118 | +Future<String> _loadVersion() async { |
| 119 | + var version = const String.fromEnvironment('version'); |
| 120 | + if (const bool.fromEnvironment('node')) { |
| 121 | + version += " compiled with dart2js " |
| 122 | + "${const String.fromEnvironment('dart-version')}"; |
| 123 | + } |
| 124 | + if (version != null) return version; |
| 125 | + |
| 126 | + var libDir = |
| 127 | + p.fromUri(await Isolate.resolvePackageUri(Uri.parse('package:sass/'))); |
| 128 | + var pubspec = readFile(p.join(libDir, '..', 'pubspec.yaml')); |
| 129 | + return pubspec |
| 130 | + .split("\n") |
| 131 | + .firstWhere((line) => line.startsWith('version: ')) |
| 132 | + .split(" ") |
| 133 | + .last; |
| 134 | +} |
0 commit comments