@capawesome/cli
Version:
The Capawesome Cloud Command Line Interface (CLI) to manage Live Updates and more.
83 lines (82 loc) • 3.69 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFile = exports.isDirectory = exports.fileExistsAtPath = exports.getFilesInDirectoryAndSubdirectories = void 0;
const fs_1 = __importDefault(require("fs"));
const mime_1 = __importDefault(require("mime"));
const path_1 = __importDefault(require("path"));
const getFilesInDirectoryAndSubdirectories = (path) => __awaiter(void 0, void 0, void 0, function* () {
const files = [];
const walk = (directory) => __awaiter(void 0, void 0, void 0, function* () {
const dirEntries = yield fs_1.default.promises.readdir(directory, { withFileTypes: true }).catch(() => []);
for (const dirEntry of dirEntries) {
const fullPath = path_1.default.join(directory, dirEntry.name);
if (dirEntry.isDirectory()) {
yield walk(fullPath);
}
else {
let pathToReplace = path_1.default.normalize(path);
// Remove the leading './' from the path
if (pathToReplace.startsWith('./')) {
pathToReplace = pathToReplace.replace('./', '');
}
let href = fullPath.replace(pathToReplace, '');
// Replace the backslashes with forward slashes (Windows only)
href = href.replace(/\\/g, '/');
// Remove the leading '/' from the href
if (href.startsWith('/')) {
href = href.replace('/', '');
}
files.push({
href,
mimeType: mime_1.default.getType(dirEntry.name) || 'application/octet-stream',
name: dirEntry.name,
path: fullPath,
});
}
}
});
yield walk(path);
return files;
});
exports.getFilesInDirectoryAndSubdirectories = getFilesInDirectoryAndSubdirectories;
const fileExistsAtPath = (path) => __awaiter(void 0, void 0, void 0, function* () {
return new Promise((resolve) => {
fs_1.default.access(path, fs_1.default.constants.F_OK, (err) => {
resolve(!err);
});
});
});
exports.fileExistsAtPath = fileExistsAtPath;
const isDirectory = (path) => __awaiter(void 0, void 0, void 0, function* () {
return new Promise((resolve) => {
fs_1.default.lstat(path, (err, stats) => {
resolve(stats.isDirectory());
});
});
});
exports.isDirectory = isDirectory;
const writeFile = (path, data) => __awaiter(void 0, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
fs_1.default.writeFile(path, data, (err) => {
if (err) {
reject(err);
}
else {
resolve(undefined);
}
});
});
});
exports.writeFile = writeFile;