playwright-fluent
Version:
Fluent API around playwright
79 lines (78 loc) • 3.47 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.shouldUpdateFile = exports.lastUpdateOf = exports.ensureDirectoryExists = exports.deleteFile = exports.getFilesOlderThanInDirectory = exports.getFilesInDirectory = exports.fileDoesNotExist = exports.fileExists = exports.userDownloadsDirectory = exports.userHomeDirectory = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const dates_1 = require("../dates");
exports.userHomeDirectory = process.env.HOME || process.env.USERPROFILE || process.cwd();
exports.userDownloadsDirectory = (0, path_1.join)(exports.userHomeDirectory, 'Downloads');
const isFile = (path) => (0, fs_1.statSync)(path).isFile();
const isDirectory = (path) => (0, fs_1.statSync)(path).isDirectory();
const lastModifiedDate = (path) => (0, fs_1.statSync)(path).mtime;
const lastChangedDate = (path) => (0, fs_1.statSync)(path).ctime;
const ignoreNodeModule = (path) => path.indexOf('node_modules') < 0;
const ignoreDotDir = (path) => path.startsWith('.') === false;
const fileExists = (filePath) => {
if ((0, fs_1.existsSync)(filePath) && isFile(filePath)) {
return true;
}
if ((0, fs_1.existsSync)(filePath) && isDirectory(filePath)) {
throw new Error(`File '${filePath}' is a directory but should be a file.`);
}
return false;
};
exports.fileExists = fileExists;
const fileDoesNotExist = (filePath) => {
return !(0, exports.fileExists)(filePath);
};
exports.fileDoesNotExist = fileDoesNotExist;
const getFilesInDirectory = (path, fileFilter) => (0, fs_1.readdirSync)(path)
.map((name) => (0, path_1.join)(path.toString(), name))
.filter(isFile)
.filter(ignoreNodeModule)
.filter(ignoreDotDir)
.filter(fileFilter || (() => true));
exports.getFilesInDirectory = getFilesInDirectory;
const getFilesOlderThanInDirectory = (path, durationInSeconds, fileFilter) => (0, exports.getFilesInDirectory)(path, fileFilter).filter((filepath) => {
const modifiedDate = lastModifiedDate(filepath);
const elapsedTimeInSeconds = (new Date().getTime() - modifiedDate.getTime()) * 1000;
return elapsedTimeInSeconds > durationInSeconds;
});
exports.getFilesOlderThanInDirectory = getFilesOlderThanInDirectory;
const deleteFile = (path) => {
if ((0, exports.fileExists)(path)) {
(0, fs_1.unlinkSync)(path);
}
};
exports.deleteFile = deleteFile;
const ensureDirectoryExists = (directoryPath) => {
if ((0, fs_1.existsSync)(directoryPath)) {
return;
}
(0, fs_1.mkdirSync)(directoryPath);
};
exports.ensureDirectoryExists = ensureDirectoryExists;
const lastUpdateOf = (filePath) => {
if ((0, exports.fileDoesNotExist)(filePath)) {
throw new Error(`File '${filePath}' does not exist.`);
}
const modified = lastModifiedDate(filePath);
const changed = lastChangedDate(filePath);
const result = modified.getTime() > changed.getTime() ? modified : changed;
return result;
};
exports.lastUpdateOf = lastUpdateOf;
function shouldUpdateFile(filePath, policy) {
if (policy === 'never') {
return false;
}
if (policy === 'always') {
return true;
}
const lastUpdate = (0, exports.lastUpdateOf)(filePath);
const now = new Date();
const elapsedTimeInDays = (now.getTime() - lastUpdate.getTime()) / (1000 * 60 * 60 * 24);
const days = (0, dates_1.toDays)(policy);
return elapsedTimeInDays > days;
}
exports.shouldUpdateFile = shouldUpdateFile;
;