zippycli
Version:
An unofficial Zippyshare CLI
135 lines (107 loc) • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.dateHumanTimestamp = dateHumanTimestamp;
exports.divmod = divmod;
exports.env = env;
exports.envTrue = envTrue;
exports.fstat = fstat;
exports.parseDate = parseDate;
exports.pipelineP = void 0;
exports.readInputFile = readInputFile;
var _stream = require("stream");
var _util = require("util");
var _fsExtra = _interopRequireDefault(require("fs-extra"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const pipelineP = (0, _util.promisify)(_stream.pipeline);
/**
* Integer division with remained.
*
* @param v Value.
* @param d Divisor.
* @returns Integer divided and remained.
*/
exports.pipelineP = pipelineP;
function divmod(v, d) {
return [Math.floor(v / d), v % d];
}
/**
* Parse date or return null is not valid.
*
* @param str Date string.
* @returns Date object or null.
*/
function parseDate(str) {
const d = new Date(str);
const t = d.getTime(); // Test if the time is NaN (NaN !== NaN).
// eslint-disable-next-line no-self-compare
return t === t ? d : null;
}
/**
* Get environment variable value.
*
* @param name Environment name.
* @returns String value or null.
*/
function env(name) {
// eslint-disable-next-line no-process-env
const v = process.env[name];
return typeof v === 'undefined' ? null : v;
}
/**
* Check if the environment variable value is true.
*
* @param name Environment name.
* @returns String value is a true-like value.
*/
function envTrue(name) {
const v = env(name);
return v && /^(1|true|yes)$/i.test(v);
}
/**
* Stat path or return null if path does not exist.
*
* @param filepath File path.
* @returns Stat object or null.
*/
async function fstat(filepath) {
try {
return await _fsExtra.default.stat(filepath);
} catch (err) {
const code = err ? err.code : null;
if (code === 'ENOENT' || code === 'ENOTDIR') {
return null;
}
throw err;
}
}
/**
* Read input file to list all URL's line by line.
*
* @param filepath Input file.
* @returns URL list.
*/
async function readInputFile(filepath) {
const r = [];
const data = await _fsExtra.default.readFile(filepath, 'utf8');
const lines = data.split(/[\r\n]+/);
for (const line of lines) {
const s = line.trim();
if (/^https?:\/\//i.test(s)) {
r.push(s);
}
}
return r;
}
/**
* Format date for human readable timestamps.
*
* @param date Date object or null.
* @returns Date string.
*/
function dateHumanTimestamp(date = null) {
const d = date || new Date(); // Format: yyyy-mm-dd h:MM:ss
return [[d.getFullYear(), `0${d.getMonth() + 1}`.slice(-2), `0${d.getDate()}`.slice(-2)].join('-'), [d.getHours(), `0${d.getMinutes()}`.slice(-2), `0${d.getSeconds()}`.slice(-2)].join(':')].join(' ');
}
//# sourceMappingURL=util.js.map