// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import * as fs from "fs";
import * as Q from "q";
export class FileSystemUtil {
public ensureDirectory(dir: string): Q.Promise<void> {
return Q.nfcall(fs.stat, dir).then((stat: fs.Stats): void => {
if (stat.isDirectory()) {
return;
} else {
throw new Error(`Expected ${dir} to be a directory`);
}
}, (err: Error & {code?: string}): Q.Promise<any> => {
if (err && err.code === "ENOENT") {
return Q.nfcall(fs.mkdir, dir);
} else {
throw err;
}
});
}
public ensureFileWithContents(file: string, contents: string): Q.Promise<void> {
return Q.nfcall(fs.stat, file).then((stat: fs.Stats): void => {
if (!stat.isFile()) {
throw new Error(`Expected ${file} to be a file`);
}
// The file already exists, assume the contents are good and do not touch it.
}, (err: Error & {code?: string}): Q.Promise<any> => {
if (err && err.code === "ENOENT") {
return Q.nfcall(fs.writeFile, file, contents);
} else {
throw err;
}
});
}
}microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/utils/fileSystemUtil.ts
39lines · modepreview