@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
161 lines • 6.28 kB
JavaScript
;
/**
* 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.pathExists = exports.isFileSync = exports.isDirectorySync = exports.isDirectory = exports.findAzureRestApiSpecsRepositoryPathSync = exports.getChildDirectoriesSync = exports.getChildDirectories = void 0;
var tslib_1 = require("tslib");
var fssync = tslib_1.__importStar(require("fs"));
var fs_1 = require("fs");
var path = tslib_1.__importStar(require("path"));
function getChildDirectories(parent) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var allChildren, childDirectories, allChildren_1, allChildren_1_1, child, e_1_1;
var e_1, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, pathExists(parent)];
case 1:
if (!(_b.sent())) {
throw new Error("\"" + parent + "\" directory does not exist");
}
return [4 /*yield*/, fs_1.promises.readdir(parent)];
case 2:
allChildren = _b.sent();
childDirectories = [];
_b.label = 3;
case 3:
_b.trys.push([3, 8, 9, 10]);
allChildren_1 = tslib_1.__values(allChildren), allChildren_1_1 = allChildren_1.next();
_b.label = 4;
case 4:
if (!!allChildren_1_1.done) return [3 /*break*/, 7];
child = allChildren_1_1.value;
return [4 /*yield*/, isDirectory(path.resolve(parent, child))];
case 5:
if (_b.sent()) {
childDirectories.push(child);
}
_b.label = 6;
case 6:
allChildren_1_1 = allChildren_1.next();
return [3 /*break*/, 4];
case 7: return [3 /*break*/, 10];
case 8:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 10];
case 9:
try {
if (allChildren_1_1 && !allChildren_1_1.done && (_a = allChildren_1.return)) _a.call(allChildren_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 10: return [2 /*return*/, childDirectories];
}
});
});
}
exports.getChildDirectories = getChildDirectories;
function getChildDirectoriesSync(parent) {
var e_2, _a;
if (!fssync.existsSync(parent)) {
throw new Error("\"" + parent + "\" directory does not exist");
}
var allChildren = fssync.readdirSync(parent);
var childDirectories = [];
try {
for (var allChildren_2 = tslib_1.__values(allChildren), allChildren_2_1 = allChildren_2.next(); !allChildren_2_1.done; allChildren_2_1 = allChildren_2.next()) {
var child = allChildren_2_1.value;
if (isDirectorySync(path.resolve(parent, child))) {
childDirectories.push(child);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (allChildren_2_1 && !allChildren_2_1.done && (_a = allChildren_2.return)) _a.call(allChildren_2);
}
finally { if (e_2) throw e_2.error; }
}
return childDirectories;
}
exports.getChildDirectoriesSync = getChildDirectoriesSync;
function findAzureRestApiSpecsRepositoryPathSync() {
var repositoryName = "azure-rest-api-specs";
var currentDirectory = __dirname;
var pathData = path.parse(currentDirectory);
var rootDirectory = pathData.root;
do {
currentDirectory = path.resolve(currentDirectory, "..");
if (containsDirectorySync(repositoryName, currentDirectory)) {
return path.resolve(currentDirectory, repositoryName);
}
} while (currentDirectory !== rootDirectory);
return undefined;
}
exports.findAzureRestApiSpecsRepositoryPathSync = findAzureRestApiSpecsRepositoryPathSync;
function containsDirectorySync(directoryName, parentPath) {
return fssync.existsSync(path.resolve(parentPath, directoryName));
}
function isDirectory(directoryPath) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var stats, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, fs_1.promises.lstat(directoryPath)];
case 1:
stats = _b.sent();
return [2 /*return*/, stats.isDirectory()];
case 2:
_a = _b.sent();
return [2 /*return*/, false];
case 3: return [2 /*return*/];
}
});
});
}
exports.isDirectory = isDirectory;
function isDirectorySync(directoryPath) {
try {
var stats = fssync.lstatSync(directoryPath);
return stats.isDirectory();
}
catch (_a) {
return false;
}
}
exports.isDirectorySync = isDirectorySync;
/**
* Get whether or not a file exists at the provided path.
* @param filePath The path to check.
* @returns Whether or not a file exists at the provided path.
*/
function isFileSync(filePath) {
try {
var stats = fssync.lstatSync(filePath);
return stats.isFile();
}
catch (_a) {
return false;
}
}
exports.isFileSync = isFileSync;
function pathExists(path) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
fssync.exists(path, function (exists) {
resolve(exists);
});
})];
});
});
}
exports.pathExists = pathExists;
//# sourceMappingURL=filesystem.js.map