2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const _resolve = require("path").resolve;
|
|
|
|
|
|
|
|
function repeat(chr, length) {
|
|
|
|
let result = chr;
|
|
|
|
while (result.length < length) { result += chr; }
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function zpad(value) {
|
|
|
|
value = String(value);
|
|
|
|
while (value.length < 2) { value = "0" + value; }
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-05-24 18:16:46 -04:00
|
|
|
function getDate(date) {
|
|
|
|
return [
|
|
|
|
date.getFullYear(),
|
|
|
|
zpad(date.getMonth() + 1),
|
|
|
|
zpad(date.getDate())
|
|
|
|
].join("-");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDateTime(date) {
|
|
|
|
return getDate(date) + " " + [
|
2020-01-11 04:17:08 -05:00
|
|
|
zpad(date.getHours()) ,
|
2019-05-24 18:16:46 -04:00
|
|
|
zpad(date.getMinutes() + 1)
|
|
|
|
].join(":");
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
function today() {
|
2019-05-24 18:16:46 -04:00
|
|
|
return getDate(new Date());
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadJson(filename) {
|
|
|
|
return JSON.parse(fs.readFileSync(filename).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: atomic write
|
|
|
|
function saveJson(filename, json) {
|
|
|
|
fs.writeFileSync(filename, JSON.stringify(json, null, 2) + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolve(...args) {
|
|
|
|
args = args.slice();
|
|
|
|
args.unshift("..");
|
|
|
|
args.unshift(__dirname);
|
|
|
|
return _resolve.apply(null, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
resolve: resolve,
|
|
|
|
|
|
|
|
loadJson: loadJson,
|
|
|
|
saveJson: saveJson,
|
|
|
|
|
|
|
|
repeat: repeat,
|
2019-05-24 18:16:46 -04:00
|
|
|
|
|
|
|
today: today,
|
|
|
|
getDate: getDate,
|
|
|
|
getDateTime: getDateTime
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|