@haxtheweb/create
Version:
CLI for all things HAX the web
135 lines (129 loc) • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SITE_FILE_NAME = void 0;
exports.camelToDash = camelToDash;
exports.capitalizeFirstLetter = capitalizeFirstLetter;
exports.dashToCamel = dashToCamel;
exports.exec = void 0;
exports.generateUUID = generateUUID;
exports.getTimeDifference = getTimeDifference;
exports.interactiveExec = interactiveExec;
exports.readAllFiles = readAllFiles;
exports.readConfigFile = readConfigFile;
exports.spawn = void 0;
exports.writeConfigFile = writeConfigFile;
var fs = _interopRequireWildcard(require("node:fs"));
var os = _interopRequireWildcard(require("node:os"));
var path = _interopRequireWildcard(require("node:path"));
var child_process = _interopRequireWildcard(require("child_process"));
var util = _interopRequireWildcard(require("node:util"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const exec = exports.exec = util.promisify(child_process.exec);
const spawn = exports.spawn = child_process.spawn;
function getTimeDifference(timestamp1, timestamp2) {
const time1 = new Date(timestamp1).getTime();
const time2 = new Date(timestamp2).getTime();
if (isNaN(time1) || isNaN(time2)) {
return "Invalid date format";
}
const difference = Math.abs(time2 - time1);
const seconds = Math.floor(difference / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
return {
days,
hours: hours % 24,
minutes: minutes % 60,
seconds: seconds % 60
};
}
// write user config file
function writeConfigFile(filename, data) {
let tempDir = os.homedir();
if (process.env.VERCEL_ENV) {
tempDir = "/tmp/";
}
const filePath = path.join(tempDir, '.haxtheweb', filename);
try {
fs.writeFileSync(filePath, data);
return filePath;
} catch (error) {
return null;
}
}
// read user config file
function readConfigFile(filename) {
let tempDir = os.homedir();
if (process.env.VERCEL_ENV) {
tempDir = "/tmp/";
}
const filePath = path.join(tempDir, '.haxtheweb', filename);
try {
let file = fs.readFileSync(filePath, 'utf8');
return file;
} catch (error) {
return null;
}
}
async function interactiveExec(command, args = [], options = {}) {
return new Promise((resolve, reject) => {
process.env.NODE_NO_WARNINGS = 1;
const child = spawn(command, args, {
stdio: 'inherit',
...options
});
child.on('exit', code => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Command failed with code ${code}`));
}
});
child.on('error', err => {
reject(err);
});
});
}
const SITE_FILE_NAME = exports.SITE_FILE_NAME = "site.json";
/**
* Helper to convert dash to camel; important when reading attributes.
*/
function dashToCamel(str) {
return capitalizeFirstLetter(str.replace(/-([a-z0-9])/g, function (g) {
return g[1].toUpperCase();
}));
}
//capitalize only the first letter of the string.
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// generate unique-enough id
function generateUUID() {
return "ss-s-s-s-sss".replace(/s/g, _uuidPart);
}
/**
* Helper to convert camel case to dash; important when setting attributes.
*/
function camelToDash(str) {
return str.replace(/\W+/g, "-").replace(/([a-z\d])([A-Z])/g, "$1-$2").toLowerCase();
}
function _uuidPart() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
// read in all files recursively for rewriting
function* readAllFiles(dir) {
const files = fs.readdirSync(dir, {
withFileTypes: true
});
for (const file of files) {
if (file.isDirectory()) {
yield* readAllFiles(path.join(dir, file.name));
} else {
yield path.join(dir, file.name);
}
}
}