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