// Copyright (c) 2015-2016 Yuya Ochiai
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
const fs = require('fs');
function asyncSleep(timeout) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeout);
});
}
function dirExistsAsync(path) {
return new Promise((resolve, reject) => {
fs.stat(path, (error, stats) => {
if (error) {
if (error.code === 'ENOENT') {
resolve(false);
} else {
reject(error);
return;
resolve(stats.isDirectory());
function mkDirAsync(path) {
dirExistsAsync(path).then((exists) => {
if (!exists) {
fs.mkdir(path, {recursive: true}, (error) => {
}).catch((err) => {
reject(err);
function rmDirAsync(path) {
if (exists) {
fs.rm(path, {recursive: true, force: true}, (error) => {
function unlinkAsync(path) {
fs.unlink(path, (error) => {
function writeFileAsync(path, data) {
fs.writeFile(path, data, (err) => {
if (err) {
module.exports = {
asyncSleep,
dirExistsAsync,
mkDirAsync,
rmDirAsync,
unlinkAsync,
writeFileAsync,
};