Create a directory for CSS output if necessary (#291)

Closes #288
This commit is contained in:
Natalie Weizenbaum 2018-04-12 16:40:53 -07:00 committed by GitHub
parent 5cce76b6d4
commit a31251fdc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 21 deletions

View File

@ -1,6 +1,7 @@
## 1.1.2
## 1.2.0
* Internal changes only.
* The command-line executable will now create the directory for the resulting
CSS if that directory doesn't exist.
## 1.1.1

View File

@ -111,6 +111,7 @@ main(List<String> args) async {
}
if (destination != null) {
ensureDir(p.dirname(destination));
writeFile(destination, css + "\n");
} else if (css.isNotEmpty) {
print(css);

View File

@ -61,5 +61,9 @@ bool fileExists(String path) => null;
/// Returns whether a dir at [path] exists.
bool dirExists(String path) => null;
/// Ensures that a directory exists at [path], creating it and its ancestors if
/// necessary.
void ensureDir(String path) => null;
/// Gets and sets the exit code that the process will use when it exits.
int exitCode;

View File

@ -17,6 +17,7 @@ class _FS {
external readFileSync(String path, [String encoding]);
external void writeFileSync(String path, String data);
external bool existsSync(String path);
external void mkdirSync(String path);
}
@JS()
@ -93,25 +94,11 @@ String readFile(String path) {
}
/// Wraps `fs.readFileSync` to throw a [FileSystemException].
_readFile(String path, [String encoding]) {
try {
return _fs.readFileSync(path, encoding);
} catch (error) {
var systemError = error as _SystemError;
throw new FileSystemException._(
_cleanErrorMessage(systemError), systemError.path);
}
}
_readFile(String path, [String encoding]) =>
_systemErrorToFileSystemException(() => _fs.readFileSync(path, encoding));
void writeFile(String path, String contents) {
try {
return _fs.writeFileSync(path, contents);
} catch (error) {
var systemError = error as _SystemError;
throw new FileSystemException._(
_cleanErrorMessage(systemError), systemError.path);
}
}
void writeFile(String path, String contents) =>
_systemErrorToFileSystemException(() => _fs.writeFileSync(path, contents));
Future<String> readStdin() async {
var completer = new Completer<String>();
@ -152,6 +139,32 @@ bool fileExists(String path) => _fs.existsSync(path);
bool dirExists(String path) => _fs.existsSync(path);
void ensureDir(String path) {
return _systemErrorToFileSystemException(() {
try {
_fs.mkdirSync(path);
} catch (error) {
var systemError = error as _SystemError;
if (systemError.code == 'EEXIST') return;
if (systemError.code != 'ENOENT') rethrow;
ensureDir(p.dirname(path));
_fs.mkdirSync(path);
}
});
}
/// Runs callback and converts any [_SystemError]s it throws into
/// [FileSystemException]s.
T _systemErrorToFileSystemException<T>(T callback()) {
try {
return callback();
} catch (error) {
var systemError = error as _SystemError;
throw new FileSystemException._(
_cleanErrorMessage(systemError), systemError.path);
}
}
@JS("process.stderr")
external _Stderr get _stderr;

View File

@ -53,3 +53,6 @@ Future<String> readStdin() async {
bool fileExists(String path) => new io.File(path).existsSync();
bool dirExists(String path) => new io.Directory(path).existsSync();
void ensureDir(String path) =>
new io.Directory(path).createSync(recursive: true);

View File

@ -1,5 +1,5 @@
name: sass
version: 1.1.2
version: 1.2.0-dev
description: A Sass implementation in Dart.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/sass/dart-sass

View File

@ -51,6 +51,17 @@ void sharedTests(Future<TestProcess> runSass(Iterable<String> arguments)) {
await d.file("out.css", equalsIgnoringWhitespace("a { b: 3; }")).validate();
});
test("creates directories if necessary", () async {
await d.file("test.scss", "a {b: 1 + 2}").create();
var sass = await runSass(["test.scss", "some/new/dir/out.css"]);
expect(sass.stdout, emitsDone);
await sass.shouldExit(0);
await d
.file("some/new/dir/out.css", equalsIgnoringWhitespace("a { b: 3; }"))
.validate();
});
test("compiles from stdin with the magic path -", () async {
var sass = await runSass(["-"]);
sass.stdin.writeln("a {b: 1 + 2}");