@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
896 lines • 39.7 kB
JavaScript
"use strict";
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteFolder = exports.deleteFiles = exports.deleteFile = exports.deleteEntry = exports.writeFileContents = exports.readFileContents = exports.getChildFilePaths = exports.getChildFolderPaths = exports.getChildEntryPaths = exports.findFolderInPath = exports.findFileInPathSync = exports.findFileInPath = exports.findEntryInPath = exports.copyFolder = exports.copyFile = exports.copyEntry = exports.createTemporaryFolder = exports.createFolder = exports._createFolder = exports.folderExists = exports.fileExistsSync = exports.fileExists = exports.symbolicLinkExists = exports.entryExists = void 0;
var tslib_1 = require("tslib");
var fs = tslib_1.__importStar(require("fs"));
var arrays_1 = require("./arrays");
var path_1 = require("./path");
function _entryExists(entryPath, condition) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
fs.lstat(entryPath, function (error, stats) {
if (error) {
if (error.code === "ENOENT" || error.code === "ENOTDIR") {
resolve(false);
}
else {
reject(error);
}
}
else {
resolve(!condition || condition(stats));
}
});
})];
});
});
}
function _entryExistsSync(entryPath, condition) {
var result = false;
try {
var stat = fs.lstatSync(entryPath);
result = !!(!condition || condition(stat));
}
catch (error) {
}
return result;
}
/**
* Get whether or not a file entry (file or folder) exists at the provided entryPath.
* @param entryPath The path to the file entry to check.
* @returns Whether or not a file entry (file or folder) exists at the provided entryPath.
*/
function entryExists(entryPath) {
return _entryExists(entryPath);
}
exports.entryExists = entryExists;
/**
* Check whether or not a symbolic link exists at the provided path.
* @param symbolicLinkPath The path to check.
* @returns Whether or not a symbolic link exists at the provided path.
*/
function symbolicLinkExists(symbolicLinkPath) {
return _entryExists(symbolicLinkPath, function (stats) { return stats.isSymbolicLink(); });
}
exports.symbolicLinkExists = symbolicLinkExists;
/**
* Check whether or not a file exists at the provided filePath.
* @param filePath The path to check.
* @returns Whether or not a file exists at the provided filePath.
*/
function fileExists(filePath) {
return _entryExists(filePath, function (stats) { return stats.isFile(); });
}
exports.fileExists = fileExists;
/**
* Check whether or not a file exists at the provided filePath.
* @param filePath The path to check.
* @returns Whether or not a file exists at the provided filePath.
*/
function fileExistsSync(filePath) {
return _entryExistsSync(filePath, function (stats) { return stats.isFile(); });
}
exports.fileExistsSync = fileExistsSync;
/**
* Check whether or not a folder exists at the provided folderPath.
* @param folderPath The path to check.
* @returns Whether or not a folder exists at the provided folderPath.
*/
function folderExists(folderPath) {
return _entryExists(folderPath, function (stats) { return stats.isDirectory(); });
}
exports.folderExists = folderExists;
function _createFolder(folderPath) {
return new Promise(function (resolve, reject) {
fs.mkdir(folderPath, function (error) {
if (error) {
if (error.code === "EEXIST") {
resolve(false);
}
else {
reject(error);
}
}
else {
resolve(true);
}
});
});
}
exports._createFolder = _createFolder;
/**
* Create a folder at the provided folderPath. If the folder is successfully created, then true will
* be returned. If the folder already exists, then false will be returned.
* @param folderPath The path to create a folder at.
*/
function createFolder(folderPath) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, createFolderError_1, createFolderError2_1, createParentFolderError_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 11]);
return [4 /*yield*/, _createFolder(folderPath)];
case 1:
result = _a.sent();
return [3 /*break*/, 11];
case 2:
createFolderError_1 = _a.sent();
if (!(createFolderError_1.code !== "ENOENT")) return [3 /*break*/, 3];
result = Promise.reject(createFolderError_1);
return [3 /*break*/, 10];
case 3:
_a.trys.push([3, 9, , 10]);
return [4 /*yield*/, createFolder(path_1.getParentFolderPath(folderPath))];
case 4:
_a.sent();
_a.label = 5;
case 5:
_a.trys.push([5, 7, , 8]);
return [4 /*yield*/, _createFolder(folderPath)];
case 6:
result = _a.sent();
return [3 /*break*/, 8];
case 7:
createFolderError2_1 = _a.sent();
result = Promise.reject(createFolderError2_1);
return [3 /*break*/, 8];
case 8: return [3 /*break*/, 10];
case 9:
createParentFolderError_1 = _a.sent();
result = Promise.reject(createFolderError_1);
return [3 /*break*/, 10];
case 10: return [3 /*break*/, 11];
case 11: return [2 /*return*/, result];
}
});
});
}
exports.createFolder = createFolder;
/**
* Create a temporary folder and return the absolute path to the folder. If a parentFolderPath is
* provided, then the temporary folder will be created as a child of the parentFolderPath. If
* parentFolderPath is not provided, then the current working folder will be used as the
* parentFolderPath.
* @param The folder that the temporary folder will be created as a child of. Defaults to the
* current working folder.
*/
function createTemporaryFolder(parentFolderPath) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, i, tempFolderPath;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!parentFolderPath) {
parentFolderPath = process.cwd();
}
else if (!path_1.isRooted(parentFolderPath)) {
parentFolderPath = path_1.joinPath(process.cwd(), parentFolderPath);
}
result = "";
i = 1;
_a.label = 1;
case 1:
if (!!result) return [3 /*break*/, 3];
tempFolderPath = path_1.joinPath(parentFolderPath, i.toString());
return [4 /*yield*/, createFolder(tempFolderPath)];
case 2:
if (_a.sent()) {
result = tempFolderPath;
return [3 /*break*/, 3];
}
else {
++i;
}
return [3 /*break*/, 1];
case 3: return [2 /*return*/, result];
}
});
});
}
exports.createTemporaryFolder = createTemporaryFolder;
/**
* Copy the entry at the source entry path to the destination entry path.
* @param sourceEntryPath The path to the entry to copy from.
* @param destinationEntryPath The path to entry to copy to.
*/
function copyEntry(sourceEntryPath, destinationEntryPath) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, error;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fileExists(sourceEntryPath)];
case 1:
if (!_a.sent()) return [3 /*break*/, 2];
result = copyFile(sourceEntryPath, destinationEntryPath);
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, folderExists(sourceEntryPath)];
case 3:
if (_a.sent()) {
result = copyFolder(sourceEntryPath, destinationEntryPath);
}
else {
error = new Error("ENOENT: no such file or directory: " + sourceEntryPath);
error.code = "ENOENT";
error.path = sourceEntryPath;
result = Promise.reject(error);
}
_a.label = 4;
case 4: return [2 /*return*/, result];
}
});
});
}
exports.copyEntry = copyEntry;
/**
* Copy the file at the source file path to the destination file path.
* @param sourceFilePath The path to the file to copy from.
* @param destinationFilePath The path to file to copy to.
* @param createDestinationFolder Whether or not the destination parent folder will be created if it
* doesn't exist.
*/
function copyFile(sourceFilePath, destinationFilePath, createDestinationFolder) {
if (createDestinationFolder === void 0) { createDestinationFolder = true; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _this = this;
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
fs.copyFile(sourceFilePath, destinationFilePath, function (error) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _a, destinationFolderPath, error2_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!!error) return [3 /*break*/, 1];
resolve();
return [3 /*break*/, 10];
case 1:
_a = error.code !== "ENOENT";
if (_a) return [3 /*break*/, 3];
return [4 /*yield*/, fileExists(sourceFilePath)];
case 2:
_a = !(_b.sent());
_b.label = 3;
case 3:
if (!(_a || !createDestinationFolder)) return [3 /*break*/, 4];
reject(error);
return [3 /*break*/, 10];
case 4:
destinationFolderPath = path_1.getParentFolderPath(destinationFilePath);
return [4 /*yield*/, folderExists(destinationFolderPath)];
case 5:
if (!_b.sent()) return [3 /*break*/, 6];
reject(error);
return [3 /*break*/, 10];
case 6:
_b.trys.push([6, 9, , 10]);
return [4 /*yield*/, createFolder(destinationFolderPath)];
case 7:
_b.sent();
return [4 /*yield*/, copyFile(sourceFilePath, destinationFilePath, false)];
case 8:
_b.sent();
resolve();
return [3 /*break*/, 10];
case 9:
error2_1 = _b.sent();
reject(error);
return [3 /*break*/, 10];
case 10: return [2 /*return*/];
}
});
}); });
})];
});
});
}
exports.copyFile = copyFile;
/**
* Copy the folder at the source folder path to the destination folder path.
* @param sourceFolderPath The path to the folder to copy from.
* @param destinationFolderPath The path to the folder to copy to. This folder and its parent
* folders will be created if they don't already exist.
*/
function copyFolder(sourceFolderPath, destinationFolderPath) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, childEntryPaths, error, childEntryPaths_1, childEntryPaths_1_1, childEntryPath, childEntryName, e_1_1;
var e_1, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, getChildEntryPaths(sourceFolderPath)];
case 1:
childEntryPaths = _b.sent();
if (!!childEntryPaths) return [3 /*break*/, 2];
error = new Error("ENOENT: no such file or directory: " + sourceFolderPath);
error.code = "ENOENT";
error.path = sourceFolderPath;
result = Promise.reject(error);
return [3 /*break*/, 10];
case 2:
_b.trys.push([2, 7, 8, 9]);
childEntryPaths_1 = tslib_1.__values(childEntryPaths), childEntryPaths_1_1 = childEntryPaths_1.next();
_b.label = 3;
case 3:
if (!!childEntryPaths_1_1.done) return [3 /*break*/, 6];
childEntryPath = childEntryPaths_1_1.value;
childEntryName = path_1.getPathName(childEntryPath);
return [4 /*yield*/, copyEntry(childEntryPath, path_1.joinPath(destinationFolderPath, childEntryName))];
case 4:
_b.sent();
_b.label = 5;
case 5:
childEntryPaths_1_1 = childEntryPaths_1.next();
return [3 /*break*/, 3];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (childEntryPaths_1_1 && !childEntryPaths_1_1.done && (_a = childEntryPaths_1.return)) _a.call(childEntryPaths_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9:
result = Promise.resolve();
_b.label = 10;
case 10: return [2 /*return*/, result];
}
});
});
}
exports.copyFolder = copyFolder;
/**
* Get whether or not the provided string completely matches the provided regularExpression.
*/
function matches(regularExpression, possibleMatch) {
var matchResult = possibleMatch.match(regularExpression);
return !!(matchResult && matchResult[0].length === possibleMatch.length);
}
function findEntryInPath(entryName, startFolderPath, condition) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, folderPath, folderEntryPath, folderEntryPaths, folderEntryPaths_1, folderEntryPaths_1_1, folderEntryPath, folderEntryName, e_2_1, parentFolderPath;
var e_2, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
folderPath = startFolderPath || process.cwd();
_b.label = 1;
case 1:
if (!true) return [3 /*break*/, 15];
if (!(typeof entryName === "string")) return [3 /*break*/, 3];
folderEntryPath = path_1.joinPath(folderPath, entryName);
return [4 /*yield*/, Promise.resolve(condition(folderEntryPath))];
case 2:
if (_b.sent()) {
result = folderEntryPath;
return [3 /*break*/, 15];
}
return [3 /*break*/, 14];
case 3: return [4 /*yield*/, getChildEntryPaths(folderPath)];
case 4:
folderEntryPaths = _b.sent();
if (!arrays_1.any(folderEntryPaths)) return [3 /*break*/, 14];
_b.label = 5;
case 5:
_b.trys.push([5, 12, 13, 14]);
folderEntryPaths_1 = (e_2 = void 0, tslib_1.__values(folderEntryPaths)), folderEntryPaths_1_1 = folderEntryPaths_1.next();
_b.label = 6;
case 6:
if (!!folderEntryPaths_1_1.done) return [3 /*break*/, 11];
folderEntryPath = folderEntryPaths_1_1.value;
if (!matches(entryName, folderEntryPath)) return [3 /*break*/, 8];
return [4 /*yield*/, Promise.resolve(condition(folderEntryPath))];
case 7:
if (_b.sent()) {
result = folderEntryPath;
return [3 /*break*/, 15];
}
return [3 /*break*/, 10];
case 8:
folderEntryName = path_1.getPathName(folderEntryPath);
if (!matches(entryName, folderEntryName)) return [3 /*break*/, 10];
return [4 /*yield*/, Promise.resolve(condition(folderEntryPath))];
case 9:
if (_b.sent()) {
result = folderEntryPath;
return [3 /*break*/, 15];
}
_b.label = 10;
case 10:
folderEntryPaths_1_1 = folderEntryPaths_1.next();
return [3 /*break*/, 6];
case 11: return [3 /*break*/, 14];
case 12:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 14];
case 13:
try {
if (folderEntryPaths_1_1 && !folderEntryPaths_1_1.done && (_a = folderEntryPaths_1.return)) _a.call(folderEntryPaths_1);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 14:
parentFolderPath = path_1.getParentFolderPath(folderPath);
if (!parentFolderPath || folderPath === parentFolderPath) {
return [3 /*break*/, 15];
}
else {
folderPath = parentFolderPath;
}
return [3 /*break*/, 1];
case 15: return [2 /*return*/, result];
}
});
});
}
exports.findEntryInPath = findEntryInPath;
function findEntryInPathSync(entryName, startFolderPath, condition) {
var result;
var folderPath = startFolderPath || process.cwd();
while (folderPath) {
var possibleResult = path_1.joinPath(folderPath, entryName);
if (condition(possibleResult)) {
result = possibleResult;
break;
}
else {
var parentFolderPath = path_1.getParentFolderPath(folderPath);
if (!parentFolderPath || folderPath === parentFolderPath) {
break;
}
else {
folderPath = parentFolderPath;
}
}
}
return result;
}
/**
* Find the closest file with the provided name by searching the immediate child folders of the
* folder at the provided startFolderPath. If no file is found with the provided fileName, then the
* search will move up to the parent folder of the startFolderPath. This will continue until either
* the file is found, or the folder being searched does not have a parent folder (if it is a root
* folder).
* @param fileName The name of the file to look for.
* @param startFolderPath The path to the folder where the search will begin.
* @returns The path to the closest file with the provided fileName, or undefined if no file could
* be found.
*/
function findFileInPath(fileName, startFolderPath) {
return findEntryInPath(fileName, startFolderPath, fileExists);
}
exports.findFileInPath = findFileInPath;
/**
* Find the closest file with the provided name by searching the immediate child folders of the
* folder at the provided startFolderPath. If no file is found with the provided fileName, then the
* search will move up to the parent folder of the startFolderPath. This will continue until either
* the file is found, or the folder being searched does not have a parent folder (if it is a root
* folder).
* @param fileName The name of the file to look for.
* @param startFolderPath The path to the folder where the search will begin.
* @returns The path to the closest file with the provided fileName, or undefined if no file could
* be found.
*/
function findFileInPathSync(fileName, startFolderPath) {
return findEntryInPathSync(fileName, startFolderPath, fileExistsSync);
}
exports.findFileInPathSync = findFileInPathSync;
/**
* Find the closest folder with the provided name by searching the immediate child folders of the
* folder at the provided startFolderPath. If no folder is found with the provided folderName, then
* the search will move up to the parent folder of the startFolderPath. This will continue until
* either the folder is found, or the folder being searched does not have a parent folder (it is a
* root folder).
* @param folderName The name of the folder to look for.
* @param startFolderPath The path to the folder where the search will begin.
* @returns The path to the closest folder with the provided folderName, or undefined if no folder
* could be found.
*/
function findFolderInPath(folderName, startFolderPath) {
return findEntryInPath(folderName, startFolderPath, folderExists);
}
exports.findFolderInPath = findFolderInPath;
/**
* Get the child entries of the folder at the provided folderPath. If the provided folder doesn't
* exist, then undefined will be returned.
* @param folderPath The path to the folder.
* @returns The paths to the child entries of the folder at the provided folder path, or undefined
* if the folder at the provided folder path doesn't exist.
*/
function getChildEntryPaths(folderPath, options) {
var _this = this;
if (options === void 0) { options = {}; }
return new Promise(function (resolve, reject) {
fs.readdir(folderPath, function (error, entryNames) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var result, entryNames_1, entryNames_1_1, entryName, entryPath, addFile, _a, _b, _c, addFolder, _d, _e, _f, _g, _h, error_1, e_3_1;
var e_3, _j;
return tslib_1.__generator(this, function (_k) {
switch (_k.label) {
case 0:
if (!error) return [3 /*break*/, 1];
if (error.code === "ENOENT" || error.code === "ENOTDIR") {
resolve(undefined);
}
else {
reject(error);
}
return [3 /*break*/, 29];
case 1:
result = options.result || [];
_k.label = 2;
case 2:
_k.trys.push([2, 26, 27, 28]);
entryNames_1 = tslib_1.__values(entryNames), entryNames_1_1 = entryNames_1.next();
_k.label = 3;
case 3:
if (!!entryNames_1_1.done) return [3 /*break*/, 25];
entryName = entryNames_1_1.value;
entryPath = path_1.joinPath(folderPath, entryName);
_k.label = 4;
case 4:
_k.trys.push([4, 23, , 24]);
return [4 /*yield*/, fileExists(entryPath)];
case 5:
if (!_k.sent()) return [3 /*break*/, 11];
_b = !options.condition;
if (_b) return [3 /*break*/, 7];
return [4 /*yield*/, Promise.resolve(options.condition(entryPath))];
case 6:
_b = (_k.sent());
_k.label = 7;
case 7:
_a = (_b);
if (!_a) return [3 /*break*/, 10];
_c = !options.fileCondition;
if (_c) return [3 /*break*/, 9];
return [4 /*yield*/, Promise.resolve(options.fileCondition(entryPath))];
case 8:
_c = (_k.sent());
_k.label = 9;
case 9:
_a = (_c);
_k.label = 10;
case 10:
addFile = _a;
if (addFile) {
result.push(entryPath);
}
return [3 /*break*/, 22];
case 11: return [4 /*yield*/, folderExists(entryPath)];
case 12:
if (!_k.sent()) return [3 /*break*/, 22];
_e = !options.condition;
if (_e) return [3 /*break*/, 14];
return [4 /*yield*/, Promise.resolve(options.condition(entryPath))];
case 13:
_e = (_k.sent());
_k.label = 14;
case 14:
_d = (_e);
if (!_d) return [3 /*break*/, 17];
_f = !options.folderCondition;
if (_f) return [3 /*break*/, 16];
return [4 /*yield*/, Promise.resolve(options.folderCondition(entryPath))];
case 15:
_f = (_k.sent());
_k.label = 16;
case 16:
_d = (_f);
_k.label = 17;
case 17:
addFolder = _d;
if (addFolder) {
result.push(entryPath);
}
_g = options.recursive;
if (!_g) return [3 /*break*/, 20];
_h = typeof options.recursive !== "function";
if (_h) return [3 /*break*/, 19];
return [4 /*yield*/, Promise.resolve(options.recursive(entryPath))];
case 18:
_h = (_k.sent());
_k.label = 19;
case 19:
_g = (_h);
_k.label = 20;
case 20:
if (!_g) return [3 /*break*/, 22];
options.result = result;
return [4 /*yield*/, getChildEntryPaths(entryPath, options)];
case 21:
_k.sent();
_k.label = 22;
case 22: return [3 /*break*/, 24];
case 23:
error_1 = _k.sent();
return [3 /*break*/, 24];
case 24:
entryNames_1_1 = entryNames_1.next();
return [3 /*break*/, 3];
case 25: return [3 /*break*/, 28];
case 26:
e_3_1 = _k.sent();
e_3 = { error: e_3_1 };
return [3 /*break*/, 28];
case 27:
try {
if (entryNames_1_1 && !entryNames_1_1.done && (_j = entryNames_1.return)) _j.call(entryNames_1);
}
finally { if (e_3) throw e_3.error; }
return [7 /*endfinally*/];
case 28:
resolve(result);
_k.label = 29;
case 29: return [2 /*return*/];
}
});
}); });
});
}
exports.getChildEntryPaths = getChildEntryPaths;
/**
* Get the child folders of the folder at the provided folderPath. If the provided folder doesn't
* exist, then undefined will be returned.
* @param folderPath The path to the folder.
* @returns The paths to the child folders of the folder at the provided folder path, or undefined
* if the folder at the provided folder path doesn't exist.
*/
function getChildFolderPaths(folderPath, options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, getChildEntryPaths(folderPath, tslib_1.__assign(tslib_1.__assign({}, options), { fileCondition: function () { return false; } }))];
});
});
}
exports.getChildFolderPaths = getChildFolderPaths;
/**
* Get the child folders of the folder at the provided folderPath. If the provided folder doesn't
* exist, then undefined will be returned.
* @param folderPath The path to the folder.
* @returns The paths to the child folders of the folder at the provided folder path, or undefined
* if the folder at the provided folder path doesn't exist.
*/
function getChildFilePaths(folderPath, options) {
if (options === void 0) { options = {}; }
return getChildEntryPaths(folderPath, tslib_1.__assign(tslib_1.__assign({}, options), { folderCondition: function () { return false; } }));
}
exports.getChildFilePaths = getChildFilePaths;
/**
* Read the contents of the provided file.
* @param filePath The path to the file to read.
*/
function readFileContents(filePath) {
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: "utf8" }, function (error, content) {
if (error) {
if (error.code === "ENOENT") {
resolve(undefined);
}
else {
reject(error);
}
}
else {
resolve(content);
}
});
});
}
exports.readFileContents = readFileContents;
/**
* Write the provided contents to the file at the provided filePath.
* @param filePath The path to the file to write.
* @param contents The contents to write to the file.
*/
function writeFileContents(filePath, contents) {
return new Promise(function (resolve, reject) {
fs.writeFile(filePath, contents, function (error) {
if (error) {
reject(error);
}
else {
resolve();
}
});
});
}
exports.writeFileContents = writeFileContents;
function deleteEntry(path) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, folderExists(path)];
case 1:
if (!(_b.sent())) return [3 /*break*/, 3];
return [4 /*yield*/, deleteFolder(path)];
case 2:
_a = _b.sent();
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, deleteFile(path)];
case 4:
_a = _b.sent();
_b.label = 5;
case 5: return [2 /*return*/, _a];
}
});
});
}
exports.deleteEntry = deleteEntry;
/**
* Delete the file at the provided file path.
* @param {string} filePath The path to the file to delete.
*/
function deleteFile(filePath) {
return new Promise(function (resolve, reject) {
fs.unlink(filePath, function (error) {
if (error) {
if (error.code === "ENOENT") {
resolve(false);
}
else {
reject(error);
}
}
else {
resolve(true);
}
});
});
}
exports.deleteFile = deleteFile;
/**
* Delete each of the provided file paths.
* @param filePaths The file paths that should be deleted.
*/
function deleteFiles() {
var filePaths = [];
for (var _i = 0; _i < arguments.length; _i++) {
filePaths[_i] = arguments[_i];
}
return tslib_1.__awaiter(this, void 0, void 0, function () {
var filePaths_1, filePaths_1_1, filePath, e_4_1;
var e_4, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(filePaths && filePaths.length > 0)) return [3 /*break*/, 8];
_b.label = 1;
case 1:
_b.trys.push([1, 6, 7, 8]);
filePaths_1 = tslib_1.__values(filePaths), filePaths_1_1 = filePaths_1.next();
_b.label = 2;
case 2:
if (!!filePaths_1_1.done) return [3 /*break*/, 5];
filePath = filePaths_1_1.value;
return [4 /*yield*/, deleteFile(filePath)];
case 3:
_b.sent();
_b.label = 4;
case 4:
filePaths_1_1 = filePaths_1.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_4_1 = _b.sent();
e_4 = { error: e_4_1 };
return [3 /*break*/, 8];
case 7:
try {
if (filePaths_1_1 && !filePaths_1_1.done && (_a = filePaths_1.return)) _a.call(filePaths_1);
}
finally { if (e_4) throw e_4.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/];
}
});
});
}
exports.deleteFiles = deleteFiles;
function _deleteFolder(folderPath) {
return new Promise(function (resolve, reject) {
fs.rmdir(folderPath, function (error) {
if (error) {
if (error.code === "ENOENT") {
resolve(false);
}
else {
reject(error);
}
}
else {
resolve(true);
}
});
});
}
/**
* Delete the folder at the provided folder path.
* @param {string} folderPath The path to the folder to delete.
*/
function deleteFolder(folderPath) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result, deleteFolderError_1, childEntryPaths, childEntryPaths_2, childEntryPaths_2_1, childEntryPath, e_5_1, deleteFolderError2_1, deleteChildEntryError_1;
var e_5, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 19]);
return [4 /*yield*/, _deleteFolder(folderPath)];
case 1:
result = _b.sent();
return [3 /*break*/, 19];
case 2:
deleteFolderError_1 = _b.sent();
if (!(deleteFolderError_1.code === "ENOENT")) return [3 /*break*/, 3];
result = false;
return [3 /*break*/, 18];
case 3:
if (!(deleteFolderError_1.code !== "ENOTEMPTY")) return [3 /*break*/, 4];
result = deleteFolderError_1;
return [3 /*break*/, 18];
case 4:
_b.trys.push([4, 17, , 18]);
return [4 /*yield*/, getChildEntryPaths(folderPath)];
case 5:
childEntryPaths = (_b.sent());
_b.label = 6;
case 6:
_b.trys.push([6, 11, 12, 13]);
childEntryPaths_2 = tslib_1.__values(childEntryPaths), childEntryPaths_2_1 = childEntryPaths_2.next();
_b.label = 7;
case 7:
if (!!childEntryPaths_2_1.done) return [3 /*break*/, 10];
childEntryPath = childEntryPaths_2_1.value;
return [4 /*yield*/, deleteEntry(childEntryPath)];
case 8:
_b.sent();
_b.label = 9;
case 9:
childEntryPaths_2_1 = childEntryPaths_2.next();
return [3 /*break*/, 7];
case 10: return [3 /*break*/, 13];
case 11:
e_5_1 = _b.sent();
e_5 = { error: e_5_1 };
return [3 /*break*/, 13];
case 12:
try {
if (childEntryPaths_2_1 && !childEntryPaths_2_1.done && (_a = childEntryPaths_2.return)) _a.call(childEntryPaths_2);
}
finally { if (e_5) throw e_5.error; }
return [7 /*endfinally*/];
case 13:
_b.trys.push([13, 15, , 16]);
return [4 /*yield*/, _deleteFolder(folderPath)];
case 14:
result = _b.sent();
return [3 /*break*/, 16];
case 15:
deleteFolderError2_1 = _b.sent();
result = deleteFolderError2_1;
return [3 /*break*/, 16];
case 16: return [3 /*break*/, 18];
case 17:
deleteChildEntryError_1 = _b.sent();
result = deleteChildEntryError_1;
return [3 /*break*/, 18];
case 18: return [3 /*break*/, 19];
case 19: return [2 /*return*/, typeof result === "boolean"
? Promise.resolve(result)
: Promise.reject(result)];
}
});
});
}
exports.deleteFolder = deleteFolder;
//# sourceMappingURL=fileSystem2.js.map