jopi-toolkit 3.0.10 → 3.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,167 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
- import * as fs from 'node:fs/promises';
38
- import * as path from 'node:path';
39
- export * from "./common.ts";
40
- import * as jk_timer from "jopi-toolkit/jk_timer";
41
- /**
42
- * Download a file or a directory from a GitHub repository.
43
- */
44
- export function githubDownload(params) {
45
- return __awaiter(this, void 0, void 0, function () {
46
- function downloadFile(localPath, contentUrl) {
47
- return __awaiter(this, void 0, void 0, function () {
48
- var localDir, headers, response, buffer;
49
- return __generator(this, function (_a) {
50
- switch (_a.label) {
51
- case 0:
52
- localDir = path.dirname(localPath);
53
- headers = {};
54
- return [4 /*yield*/, fs.mkdir(localDir, { recursive: true })];
55
- case 1:
56
- _a.sent();
57
- return [4 /*yield*/, fetch(contentUrl, { headers: headers, })];
58
- case 2:
59
- response = _a.sent();
60
- if (!response.ok) {
61
- throw new Error("Github download error: ".concat(response.status, " ").concat(response.statusText));
62
- }
63
- return [4 /*yield*/, response.arrayBuffer()];
64
- case 3:
65
- buffer = _a.sent();
66
- return [4 /*yield*/, fs.writeFile(localPath, Buffer.from(buffer))];
67
- case 4:
68
- _a.sent();
69
- if (params.log)
70
- console.log("Downloaded", contentUrl);
71
- return [2 /*return*/];
72
- }
73
- });
74
- });
75
- }
76
- function fetchAndProcessContent(currentPath, downloadRoot) {
77
- return __awaiter(this, void 0, void 0, function () {
78
- var apiUrl, headers, response, data, isFile, contents, _i, contents_1, item, itemPath;
79
- return __generator(this, function (_a) {
80
- switch (_a.label) {
81
- case 0:
82
- apiUrl = "https://api.github.com/repos/".concat(repoOwner, "/").concat(repoName, "/contents/").concat(currentPath, "?ref=").concat(branchName);
83
- headers = {};
84
- return [4 /*yield*/, fetch(apiUrl, { headers: headers })];
85
- case 1:
86
- response = _a.sent();
87
- if (!response.ok) {
88
- throw new Error("Github download error: ".concat(response.status, " ").concat(response.statusText));
89
- }
90
- return [4 /*yield*/, response.json()];
91
- case 2:
92
- data = _a.sent();
93
- isFile = !Array.isArray(data) && (data.type === 'file');
94
- if (!(isFile && isFirst)) return [3 /*break*/, 4];
95
- return [4 /*yield*/, downloadFile(downloadRoot, data.download_url)];
96
- case 3:
97
- _a.sent();
98
- return [2 /*return*/];
99
- case 4:
100
- contents = Array.isArray(data) ? data : [data];
101
- isFirst = false;
102
- _i = 0, contents_1 = contents;
103
- _a.label = 5;
104
- case 5:
105
- if (!(_i < contents_1.length)) return [3 /*break*/, 11];
106
- item = contents_1[_i];
107
- // Reduce, otherwise we get a "403 rate limit exceed" error.
108
- return [4 /*yield*/, jk_timer.tick(gRateLimit)];
109
- case 6:
110
- // Reduce, otherwise we get a "403 rate limit exceed" error.
111
- _a.sent();
112
- if (!(item.type === 'file' && item.download_url)) return [3 /*break*/, 8];
113
- itemPath = item.path;
114
- itemPath = itemPath.substring(pathInsideRepo.length + 1);
115
- return [4 /*yield*/, downloadFile(path.join(downloadRoot, itemPath), item.download_url)];
116
- case 7:
117
- _a.sent();
118
- return [3 /*break*/, 10];
119
- case 8:
120
- if (!(item.type === 'dir')) return [3 /*break*/, 10];
121
- return [4 /*yield*/, fetchAndProcessContent(item.path, downloadRoot)];
122
- case 9:
123
- _a.sent();
124
- _a.label = 10;
125
- case 10:
126
- _i++;
127
- return [3 /*break*/, 5];
128
- case 11: return [2 /*return*/];
129
- }
130
- });
131
- });
132
- }
133
- var isFirst, url, parts, repoOwner, repoName, tree, branchName, pathInsideRepo;
134
- return __generator(this, function (_a) {
135
- switch (_a.label) {
136
- case 0:
137
- isFirst = true;
138
- url = params.url;
139
- if (!url.startsWith("https://github.com/"))
140
- throw new Error("Invalid URL - Must be of type https://github.com/ownerName/repoName/tree/branchName/path/to/folder");
141
- url = url.substring("https://github.com/".length);
142
- parts = url.split("/").reverse();
143
- repoOwner = parts.pop();
144
- repoName = parts.pop();
145
- tree = parts.pop();
146
- if (tree !== "tree")
147
- throw new Error("Invalid URL - Must be of type https://github.com/ownerName/repoName/tree/branchName/path/to/folder");
148
- branchName = parts.pop();
149
- pathInsideRepo = parts.reverse().join("/");
150
- return [4 /*yield*/, fetchAndProcessContent(pathInsideRepo, params.downloadPath)];
151
- case 1:
152
- _a.sent();
153
- return [2 /*return*/];
154
- }
155
- });
156
- });
157
- }
158
- function getRateLimit() {
159
- var sLimit = process.env.GITHUB_API_LIMIT_SECONDS;
160
- if (!sLimit)
161
- return 50;
162
- var res = parseInt(sLimit);
163
- if (isNaN(res))
164
- return 50;
165
- return res;
166
- }
167
- var gRateLimit = getRateLimit();
@@ -1,56 +0,0 @@
1
- var gCurrentLanguage = 'en';
2
- var gTranslations = {};
3
- export function setLanguage(languageName) {
4
- gCurrentLanguage = languageName;
5
- }
6
- export function translate(key, p) {
7
- return translateTo(gCurrentLanguage, key, p);
8
- }
9
- export function translateTo(language, key, p) {
10
- // Does it exist?
11
- if (!gTranslations[language]) {
12
- gTranslations[language] = {};
13
- }
14
- var translation;
15
- // Must use plural?
16
- //
17
- var translationKey = key;
18
- //
19
- if ((p.count !== undefined) && (p.count > 1)) {
20
- var pluralKey = "".concat(key, "_plural");
21
- translation = gTranslations[language][pluralKey];
22
- }
23
- // Get the translation.
24
- if (!translation) {
25
- translation = gTranslations[language][translationKey];
26
- }
27
- // Not found?
28
- if (!translation) {
29
- if (p.default) {
30
- gTranslations[language][translationKey] = p.default;
31
- return p.default;
32
- }
33
- translation = "[trNotFound:".concat(key, "]");
34
- }
35
- // Replace the parameters in the translation.
36
- if (p.params && translation.includes("$")) {
37
- for (var _i = 0, _a = Object.entries(p.params); _i < _a.length; _i++) {
38
- var _b = _a[_i], paramKey = _b[0], paramValue = _b[1];
39
- var placeholder = "$" + paramKey;
40
- translation = translation.replace(placeholder, String(paramValue));
41
- }
42
- }
43
- return translation;
44
- }
45
- export function addTranslation(language, key, value) {
46
- if (!gTranslations[language]) {
47
- gTranslations[language] = {};
48
- }
49
- gTranslations[language][key] = value;
50
- }
51
- export function addTranslations(language, newTranslations) {
52
- if (!gTranslations[language]) {
53
- gTranslations[language] = {};
54
- }
55
- Object.assign(gTranslations[language], newTranslations);
56
- }
@@ -1,5 +0,0 @@
1
- export var isNodeJS = typeof (Bun) === "undefined";
2
- export var isBunJS = typeof (Bun) !== "undefined";
3
- export var isServerSide = true;
4
- export var isBrowser = false;
5
- export var serverType = isNodeJS ? "node" : "bun";