boggo 0.0.1 → 0.1.0
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.
- package/README.md +81 -5
- package/bin/boggo.js +10 -10
- package/package.json +7 -3
- package/src/api.js +129 -0
- package/src/cli.js +260 -0
- package/src/command-texts.js +204 -0
- package/src/commands.js +716 -0
- package/src/config.js +35 -0
- package/src/lang.js +115 -0
- package/src/link.js +50 -0
- package/src/messages.js +511 -0
- package/src/pack.js +269 -0
- package/src/paint.js +35 -0
package/src/pack.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Kansion paketointi julkaisua varten (boggo push): tar.gz ilman riippuvuuksia. Mukaan eivät tule .git, node_modules, .boggo,
|
|
2
|
+
// .env-tiedostot (paitsi esimerkit), rakennusten välimuistit, .gitignoren ja .boggoignoren tiedostot eikä yli 50 Mt:n tiedostot.
|
|
3
|
+
// Raja 100 Mt pakattuna (Boggo Cloudin raja, megatavu = 1 048 576 tavua). Portaali tarkistaa paketin samoilla säännöillä.
|
|
4
|
+
import { accessSync, constants, lstatSync, readdirSync, readFileSync, readlinkSync } from "node:fs";
|
|
5
|
+
import { join, posix } from "node:path";
|
|
6
|
+
import { createGzip } from "node:zlib";
|
|
7
|
+
|
|
8
|
+
export const MB = 1_048_576;
|
|
9
|
+
export const UPLOAD_MAX_BYTES = 100 * MB;
|
|
10
|
+
const FILE_MAX_BYTES = 50 * MB;
|
|
11
|
+
|
|
12
|
+
// Aina pois: versionhallinta, riippuvuudet, Boggon oma kansio ja kehyksien välimuistit (rakennetaan Boggo Cloudissa uudelleen).
|
|
13
|
+
const ALWAYS_DIRS = new Set([".git", ".hg", ".svn", "node_modules", ".boggo", ".next", ".nuxt", ".output", ".svelte-kit", ".turbo", ".vercel", ".cache"]);
|
|
14
|
+
const ALWAYS_FILES = new Set([".DS_Store", "Thumbs.db"]);
|
|
15
|
+
// .env-tiedostot jäävät pois (salaisuudet), esimerkit kelpaavat.
|
|
16
|
+
const ENV_EXAMPLES = new Set([".env.example", ".env.sample", ".env.template", ".env.local.example", ".env.dist"]);
|
|
17
|
+
const isEnvFile = (name) => (name === ".env" || name.startsWith(".env.")) && !ENV_EXAMPLES.has(name);
|
|
18
|
+
|
|
19
|
+
// Tunnistuksen tiedostot, jotka lähetetään portaalille (boggo init): samat kuin portaalin säännöissä.
|
|
20
|
+
const DETECT_FILES = new Set(["package.json", "Dockerfile", "requirements.txt", "pyproject.toml"]);
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------------------------------------
|
|
23
|
+
// .gitignore-säännöt (tavalliset muodot: kommentit, !, kansio/, /juuri, *, **, ?, [abc])
|
|
24
|
+
|
|
25
|
+
function patternRegex(body) {
|
|
26
|
+
let re = "";
|
|
27
|
+
for (let i = 0; i < body.length; i++) {
|
|
28
|
+
const c = body[i];
|
|
29
|
+
if (c === "*") {
|
|
30
|
+
if (body[i + 1] === "*") {
|
|
31
|
+
const slashAfter = body[i + 2] === "/";
|
|
32
|
+
re += slashAfter ? "(?:.*/)?" : ".*";
|
|
33
|
+
i += slashAfter ? 2 : 1;
|
|
34
|
+
} else re += "[^/]*";
|
|
35
|
+
} else if (c === "?") re += "[^/]";
|
|
36
|
+
else if (c === "[") {
|
|
37
|
+
const end = body.indexOf("]", i + 1);
|
|
38
|
+
if (end < 0) re += "\\[";
|
|
39
|
+
else {
|
|
40
|
+
re += `[${body.slice(i + 1, end).replace(/^!/, "^").replace(/\\/g, "\\\\")}]`;
|
|
41
|
+
i = end;
|
|
42
|
+
}
|
|
43
|
+
} else if (c === "\\" && i + 1 < body.length) re += `\\${body[++i]}`;
|
|
44
|
+
else re += /[.+^${}()|\\/]/.test(c) ? `\\${c}` : c;
|
|
45
|
+
}
|
|
46
|
+
return re;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Yhden ignore-tiedoston säännöt kansiolle base ('' = juuri).
|
|
50
|
+
export function parseIgnore(text, base = "") {
|
|
51
|
+
const rules = [];
|
|
52
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
53
|
+
let line = raw.replace(/(?<!\\)\s+$/, "");
|
|
54
|
+
if (!line || line.startsWith("#")) continue;
|
|
55
|
+
const negate = line.startsWith("!");
|
|
56
|
+
if (negate) line = line.slice(1);
|
|
57
|
+
if (line.startsWith("\\#") || line.startsWith("\\!")) line = line.slice(1);
|
|
58
|
+
const dirOnly = line.endsWith("/");
|
|
59
|
+
if (dirOnly) line = line.slice(0, -1);
|
|
60
|
+
const anchored = line.includes("/");
|
|
61
|
+
if (line.startsWith("/")) line = line.slice(1);
|
|
62
|
+
if (!line) continue;
|
|
63
|
+
const body = patternRegex(line);
|
|
64
|
+
const re = new RegExp(anchored ? `^${body}$` : `(?:^|/)${body}$`);
|
|
65
|
+
rules.push({ base, re, negate, dirOnly });
|
|
66
|
+
}
|
|
67
|
+
return rules;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Viimeinen osuva sääntö ratkaisee (kuten git). path on suhteellinen paketin juuresta.
|
|
71
|
+
export function ignored(rules, path, isDir) {
|
|
72
|
+
let result = false;
|
|
73
|
+
for (const r of rules) {
|
|
74
|
+
if (r.dirOnly && !isDir) continue;
|
|
75
|
+
let rel = path;
|
|
76
|
+
if (r.base) {
|
|
77
|
+
if (!path.startsWith(`${r.base}/`)) continue;
|
|
78
|
+
rel = path.slice(r.base.length + 1);
|
|
79
|
+
}
|
|
80
|
+
if (r.re.test(rel)) result = !r.negate;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------------------------------------
|
|
86
|
+
// Kansion läpikäynti
|
|
87
|
+
|
|
88
|
+
// Tiedostot ja kansiot paketoitavaksi. Palauttaa entries [{ path, type: "file"|"dir"|"link", size, mode, mtime, link }],
|
|
89
|
+
// skipped (ohitetut isot tiedostot ja ulos osoittavat linkit), unreadable (tiedostot ja kansiot, joita ei voi lukea, esim.
|
|
90
|
+
// root-käyttäjän Docker-kansio; { path, code }) ja detect (tunnistuksen tiedostot sisältöineen). Lukukelvotonta ei ohiteta
|
|
91
|
+
// hiljaa, koska paketista puuttuisi silloin tiedostoja: boggo push kertoo ne, ja ne voi jättää pois .boggoignorella.
|
|
92
|
+
export function collect(root) {
|
|
93
|
+
const entries = [];
|
|
94
|
+
const skipped = [];
|
|
95
|
+
const unreadable = [];
|
|
96
|
+
const detect = {};
|
|
97
|
+
const readIgnore = (dir, rel) => {
|
|
98
|
+
const out = [];
|
|
99
|
+
for (const name of [".gitignore", ".boggoignore"]) {
|
|
100
|
+
try {
|
|
101
|
+
out.push(...parseIgnore(readFileSync(join(dir, name), "utf8"), rel));
|
|
102
|
+
} catch {
|
|
103
|
+
/* ei tiedostoa */
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
};
|
|
108
|
+
const walk = (dir, rel, rules) => {
|
|
109
|
+
const own = [...rules, ...readIgnore(dir, rel)];
|
|
110
|
+
let names;
|
|
111
|
+
try {
|
|
112
|
+
names = readdirSync(dir).sort();
|
|
113
|
+
} catch (error) {
|
|
114
|
+
unreadable.push({ path: rel || ".", code: error.code ?? "EIO" });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
for (const name of names) {
|
|
118
|
+
const path = rel ? `${rel}/${name}` : name;
|
|
119
|
+
const full = join(dir, name);
|
|
120
|
+
let st;
|
|
121
|
+
try {
|
|
122
|
+
st = lstatSync(full);
|
|
123
|
+
} catch {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const isDir = st.isDirectory();
|
|
127
|
+
if (isDir && ALWAYS_DIRS.has(name)) continue;
|
|
128
|
+
if (!isDir && (ALWAYS_FILES.has(name) || isEnvFile(name))) continue;
|
|
129
|
+
if (ignored(own, path, isDir)) continue;
|
|
130
|
+
if (st.isSymbolicLink()) {
|
|
131
|
+
let target;
|
|
132
|
+
try {
|
|
133
|
+
target = readlinkSync(full);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
unreadable.push({ path, code: error.code ?? "EIO" });
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const resolved = posix.normalize(posix.join(posix.dirname(path), target));
|
|
139
|
+
if (target.startsWith("/") || resolved === ".." || resolved.startsWith("../")) {
|
|
140
|
+
skipped.push({ path, reason: "link" });
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
entries.push({ path, type: "link", size: 0, mode: 0o777, mtime: st.mtimeMs, link: target });
|
|
144
|
+
} else if (isDir) {
|
|
145
|
+
entries.push({ path, type: "dir", size: 0, mode: 0o755, mtime: st.mtimeMs });
|
|
146
|
+
walk(full, path, own);
|
|
147
|
+
} else if (st.isFile()) {
|
|
148
|
+
if (st.size > FILE_MAX_BYTES) {
|
|
149
|
+
skipped.push({ path, reason: "size", size: st.size });
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
accessSync(full, constants.R_OK);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
unreadable.push({ path, code: error.code ?? "EIO" });
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
entries.push({ path, type: "file", size: st.size, mode: st.mode & 0o111 ? 0o755 : 0o644, mtime: st.mtimeMs, full });
|
|
159
|
+
if (DETECT_FILES.has(name) && path.split("/").length <= 4 && st.size <= 64 * 1024) {
|
|
160
|
+
try {
|
|
161
|
+
detect[path] = readFileSync(full, "utf8");
|
|
162
|
+
} catch {
|
|
163
|
+
/* tunnistus ilman sisältöä; paketointi kertoo virheen */
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Muut (laitetiedostot, FIFOt, socketit) ohitetaan hiljaa.
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
walk(root, "", []);
|
|
171
|
+
return { entries, skipped, unreadable, detect };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ---------------------------------------------------------------------------------------------------------
|
|
175
|
+
// tar (ustar, pitkät nimet PAX-otsakkeella)
|
|
176
|
+
|
|
177
|
+
const enc = new TextEncoder();
|
|
178
|
+
|
|
179
|
+
function header(name, { size = 0, type = "0", mode = 0o644, mtime = 0, link = "" }) {
|
|
180
|
+
const h = Buffer.alloc(512);
|
|
181
|
+
const put = (s, at, len) => enc.encode(s).subarray(0, len).forEach((b, i) => (h[at + i] = b));
|
|
182
|
+
const oct = (n, len) => n.toString(8).padStart(len - 1, "0") + "\0";
|
|
183
|
+
put(name, 0, 100);
|
|
184
|
+
put(oct(mode, 8), 100, 8);
|
|
185
|
+
put(oct(0, 8), 108, 8);
|
|
186
|
+
put(oct(0, 8), 116, 8);
|
|
187
|
+
put(oct(size, 12), 124, 12);
|
|
188
|
+
put(oct(Math.max(0, Math.floor(mtime / 1000)), 12), 136, 12);
|
|
189
|
+
put(" ", 148, 8);
|
|
190
|
+
put(type, 156, 1);
|
|
191
|
+
put(link, 157, 100);
|
|
192
|
+
put("ustar\0", 257, 6);
|
|
193
|
+
put("00", 263, 2);
|
|
194
|
+
let sum = 0;
|
|
195
|
+
for (const b of h) sum += b;
|
|
196
|
+
put(`${sum.toString(8).padStart(6, "0")}\0 `, 148, 8);
|
|
197
|
+
return h;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const pad = (n) => Buffer.alloc((512 - (n % 512)) % 512);
|
|
201
|
+
|
|
202
|
+
function paxRecord(key, value) {
|
|
203
|
+
const record = ` ${key}=${value}\n`;
|
|
204
|
+
let len = Buffer.byteLength(record) + 1;
|
|
205
|
+
while (String(len).length + Buffer.byteLength(record) !== len) len++;
|
|
206
|
+
return `${len}${record}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Merkinnän otsakkeet: pitkä nimi tai linkki PAX-tietueella, muuten pelkkä ustar.
|
|
210
|
+
function entryHeaders(e) {
|
|
211
|
+
const name = e.type === "dir" ? `${e.path}/` : e.path;
|
|
212
|
+
const type = e.type === "dir" ? "5" : e.type === "link" ? "2" : "0";
|
|
213
|
+
const long = Buffer.byteLength(name) > 100 || (e.link && Buffer.byteLength(e.link) > 100);
|
|
214
|
+
const out = [];
|
|
215
|
+
if (long) {
|
|
216
|
+
const pax = Buffer.from(paxRecord("path", name) + (e.link ? paxRecord("linkpath", e.link) : ""));
|
|
217
|
+
out.push(header("PaxHeader", { size: pax.length, type: "x" }), pax, pad(pax.length));
|
|
218
|
+
}
|
|
219
|
+
out.push(header(long ? name.slice(0, 99) : name, { size: e.type === "file" ? e.size : 0, type, mode: e.mode, mtime: e.mtime, link: long ? "" : (e.link ?? "") }));
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Paketoi merkinnät tar.gz-puskuriksi. Heittää virheen { code: "too_big" }, jos pakattu ylittää rajan, ja
|
|
224
|
+
// { code: "unreadable", path, reason }, jos tiedostoa ei voi enää lukea (poistettu tai oikeudet muuttuneet läpikäynnin jälkeen).
|
|
225
|
+
export async function packEntries(entries, { max = UPLOAD_MAX_BYTES } = {}) {
|
|
226
|
+
const gzip = createGzip({ level: 6 });
|
|
227
|
+
const chunks = [];
|
|
228
|
+
let size = 0;
|
|
229
|
+
let tooBig = false;
|
|
230
|
+
gzip.on("data", (c) => {
|
|
231
|
+
size += c.length;
|
|
232
|
+
if (size > max) tooBig = true;
|
|
233
|
+
else chunks.push(c);
|
|
234
|
+
});
|
|
235
|
+
const done = new Promise((resolve, reject) => {
|
|
236
|
+
gzip.on("end", resolve);
|
|
237
|
+
gzip.on("error", reject);
|
|
238
|
+
});
|
|
239
|
+
const write = (buf) => (gzip.write(buf) ? Promise.resolve() : new Promise((r) => gzip.once("drain", r)));
|
|
240
|
+
for (const e of entries) {
|
|
241
|
+
if (tooBig) break;
|
|
242
|
+
for (const h of entryHeaders(e)) await write(h);
|
|
243
|
+
if (e.type === "file") {
|
|
244
|
+
let data;
|
|
245
|
+
try {
|
|
246
|
+
data = readFileSync(e.full);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
gzip.destroy();
|
|
249
|
+
throw Object.assign(new Error("unreadable"), { code: "unreadable", path: e.path, reason: error.code ?? "EIO" });
|
|
250
|
+
}
|
|
251
|
+
// Tiedosto muuttui läpikäynnin jälkeen: otsakkeen koko ratkaisee (katkaistaan tai täytetään).
|
|
252
|
+
const body = data.length === e.size ? data : Buffer.concat([data.subarray(0, e.size), Buffer.alloc(Math.max(0, e.size - data.length))]);
|
|
253
|
+
await write(body);
|
|
254
|
+
await write(pad(e.size));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
gzip.end(Buffer.alloc(1024));
|
|
258
|
+
await done;
|
|
259
|
+
if (tooBig) throw Object.assign(new Error("too_big"), { code: "too_big" });
|
|
260
|
+
return Buffer.concat(chunks);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Tavut tekstinä, esim. 12,4 Mt / 12.4 MB.
|
|
264
|
+
export function sizeText(n, lang = "fi") {
|
|
265
|
+
const unit = lang === "fi" ? ["kt", "Mt"] : ["kB", "MB"];
|
|
266
|
+
if (n < MB) return `${Math.max(1, Math.round(n / 1024))} ${unit[0]}`;
|
|
267
|
+
const v = (n / MB).toFixed(1);
|
|
268
|
+
return `${lang === "fi" ? v.replace(".", ",") : v} ${unit[1]}`;
|
|
269
|
+
}
|
package/src/paint.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Värit ja korostukset päätteeseen. Vain kun virta on pääte, ei NO_COLORilla (https://no-color.org) eikä TERM=dumb-päätteessä:
|
|
2
|
+
// putkeen, tiedostoon ja CI:n lokiin tulee pelkkä teksti.
|
|
3
|
+
|
|
4
|
+
export function painter(stream, env) {
|
|
5
|
+
const on = Boolean(stream?.isTTY) && !(env.NO_COLOR !== undefined && env.NO_COLOR !== "") && env.TERM !== "dumb";
|
|
6
|
+
const wrap = (open, close) => (s) => (on ? `\u001b[${open}m${s}\u001b[${close}m` : String(s));
|
|
7
|
+
return {
|
|
8
|
+
on,
|
|
9
|
+
bold: wrap(1, 22),
|
|
10
|
+
dim: wrap(2, 22),
|
|
11
|
+
underline: wrap(4, 24),
|
|
12
|
+
red: wrap(31, 39),
|
|
13
|
+
green: wrap(32, 39),
|
|
14
|
+
yellow: wrap(33, 39),
|
|
15
|
+
cyan: wrap(36, 39),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Päätteen ohjauskoodit pois ulkoa tulevasta tekstistä (rakennusloki, portaalin viestit, tiedostonimet): ne voisivat tyhjentää
|
|
20
|
+
// ruudun, siirtää kohdistinta, vaihtaa ikkunan otsikon tai kirjoittaa leikepöydälle. Vain värikoodit (SGR, ESC[…m) sallitaan.
|
|
21
|
+
const CONTROL_RE = /\u001b(?!\[[0-9;]{1,8}m)|[\u0000-\u0008\u000b-\u001a\u001c-\u001f\u007f-\u009f]/g;
|
|
22
|
+
|
|
23
|
+
export function safe(text) {
|
|
24
|
+
return String(text).replace(CONTROL_RE, "");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Tekstin sisällä olevat komennot (boggo push --prod) ja osoitteet syaanilla, jotta ohjeen seuraava askel erottuu.
|
|
28
|
+
// Osoite päättyy värikoodiin, joten valmiiksi korostettu (esim. alleviivattu) osoite saa värin ilman, että koodit sotkeutuvat.
|
|
29
|
+
const COMMAND_RE = /\bboggo [a-z]+(?: --?[a-z]+(?:[ =]<[^>]+>)?)*/g;
|
|
30
|
+
const URL_RE = /https?:\/\/[^\s)\u001b]+?(?=[.,:;]?(?:\s|\)|\u001b|$))/g;
|
|
31
|
+
|
|
32
|
+
export function highlight(paint, text) {
|
|
33
|
+
if (!paint.on) return text;
|
|
34
|
+
return String(text).replace(new RegExp(`${URL_RE.source}|${COMMAND_RE.source}`, "g"), (m) => paint.cyan(m));
|
|
35
|
+
}
|