xweb-templating
Version:
A cli tool for converting 'tags' to regular html, php or some other format
36 lines (35 loc) • 1.85 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());
});
};
import { error, info } from "../log.js";
import fetch from "node-fetch";
import path from "path";
import fs from "fs";
export function is_bad_response(executer, tag_name, response) {
if (!response.ok) {
error(executer, `Error while fetching tagset '${tag_name}'. Error code ${response.status}`);
return true;
}
return false;
}
export function save_tagsets(executer, tagset_list, config) {
let ready = 0;
tagset_list.forEach((tag_name) => __awaiter(this, void 0, void 0, function* () {
info(executer, `Fetching tagset ${ready + 1}/${tagset_list.length} '${tag_name}'`);
const FETCH_URL = `https://raw.githubusercontent.com/CodeBoy124/xweb-tagsets/main/${tag_name}.js`;
const RESPONSE = yield fetch(FETCH_URL);
if (is_bad_response(executer, tag_name, RESPONSE))
return;
const CONTENT = yield RESPONSE.text();
const TAGSET_FILE_PATH = path.join(process.cwd(), config.folder.tags, tag_name + ".js");
fs.writeFileSync(TAGSET_FILE_PATH, CONTENT);
info(executer, `Finished adding tagset ${ready + 1}/${tagset_list.length} '${tag_name}'`);
ready++;
}));
}