@jannael/glinter 1.0.3 → 1.0.5

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.
Files changed (2) hide show
  1. package/dist/index.js +414 -69
  2. package/package.json +7 -3
package/dist/index.js CHANGED
@@ -90,6 +90,19 @@ var require_src = __commonJS((exports, module) => {
90
90
  module.exports = { cursor, scroll, erase, beep };
91
91
  });
92
92
 
93
+ // src/utils/colors.ts
94
+ var GREEN = "\x1B[32m";
95
+ var YELLOW = "\x1B[33m";
96
+ var RED = "\x1B[31m";
97
+ var MAGENTA = "\x1B[35m";
98
+ var BLUE = "\x1B[34m";
99
+ var BLACK = "\x1B[30m";
100
+ var BG_YELLOW = "\x1B[43m";
101
+ var RESET = "\x1B[0m";
102
+
103
+ // src/utils/check.ts
104
+ var CHECK = `${GREEN}\u2714${RESET}`;
105
+
93
106
  // node_modules/@clack/core/dist/index.mjs
94
107
  import { styleText as y } from "util";
95
108
  import { stdout as S, stdin as $ } from "process";
@@ -1008,6 +1021,33 @@ var it = class extends p {
1008
1021
  });
1009
1022
  }
1010
1023
  };
1024
+ class nt extends p {
1025
+ options;
1026
+ cursor = 0;
1027
+ get _selectedValue() {
1028
+ return this.options[this.cursor];
1029
+ }
1030
+ changeValue() {
1031
+ this.value = this._selectedValue.value;
1032
+ }
1033
+ constructor(t) {
1034
+ super(t, false), this.options = t.options;
1035
+ const e = this.options.findIndex(({ value: i }) => i === t.initialValue), s = e === -1 ? 0 : e;
1036
+ this.cursor = this.options[s].disabled ? d(s, 1, this.options) : s, this.changeValue(), this.on("cursor", (i) => {
1037
+ switch (i) {
1038
+ case "left":
1039
+ case "up":
1040
+ this.cursor = d(this.cursor, -1, this.options);
1041
+ break;
1042
+ case "down":
1043
+ case "right":
1044
+ this.cursor = d(this.cursor, 1, this.options);
1045
+ break;
1046
+ }
1047
+ this.changeValue();
1048
+ });
1049
+ }
1050
+ }
1011
1051
 
1012
1052
  // node_modules/@clack/prompts/dist/index.mjs
1013
1053
  import { styleText as t, stripVTControlCharacters as ne } from "util";
@@ -1167,100 +1207,405 @@ ${r ? t("cyan", E2) : ""}
1167
1207
  } }).prompt();
1168
1208
  };
1169
1209
  var Ve = { light: w2("\u2500", "-"), heavy: w2("\u2501", "="), block: w2("\u2588", "#") };
1210
+ var re = (e, i) => e.includes(`
1211
+ `) ? e.split(`
1212
+ `).map((s) => i(s)).join(`
1213
+ `) : i(e);
1214
+ var _t = (e) => {
1215
+ const i = (s, r) => {
1216
+ const u2 = s.label ?? String(s.value);
1217
+ switch (r) {
1218
+ case "disabled":
1219
+ return `${t("gray", H2)} ${re(u2, (n) => t("gray", n))}${s.hint ? ` ${t("dim", `(${s.hint ?? "disabled"})`)}` : ""}`;
1220
+ case "selected":
1221
+ return `${re(u2, (n) => t("dim", n))}`;
1222
+ case "active":
1223
+ return `${t("green", z2)} ${u2}${s.hint ? ` ${t("dim", `(${s.hint})`)}` : ""}`;
1224
+ case "cancelled":
1225
+ return `${re(u2, (n) => t(["strikethrough", "dim"], n))}`;
1226
+ default:
1227
+ return `${t("dim", H2)} ${re(u2, (n) => t("dim", n))}`;
1228
+ }
1229
+ };
1230
+ return new nt({ options: e.options, signal: e.signal, input: e.input, output: e.output, initialValue: e.initialValue, render() {
1231
+ const s = e.withGuide ?? u.withGuide, r = `${V2(this.state)} `, u2 = `${ye(this.state)} `, n = R(e.output, e.message, u2, r), o = `${s ? `${t("gray", d2)}
1232
+ ` : ""}${n}
1233
+ `;
1234
+ switch (this.state) {
1235
+ case "submit": {
1236
+ const c2 = s ? `${t("gray", d2)} ` : "", a = R(e.output, i(this.options[this.cursor], "selected"), c2);
1237
+ return `${o}${a}`;
1238
+ }
1239
+ case "cancel": {
1240
+ const c2 = s ? `${t("gray", d2)} ` : "", a = R(e.output, i(this.options[this.cursor], "cancelled"), c2);
1241
+ return `${o}${a}${s ? `
1242
+ ${t("gray", d2)}` : ""}`;
1243
+ }
1244
+ default: {
1245
+ const c2 = s ? `${t("cyan", d2)} ` : "", a = s ? t("cyan", E2) : "", l = o.split(`
1246
+ `).length, $2 = s ? 2 : 1;
1247
+ return `${o}${c2}${Y2({ output: e.output, cursor: this.cursor, options: this.options, maxItems: e.maxItems, columnPadding: c2.length, rowPadding: l + $2, style: (y2, p2) => i(y2, y2.disabled ? "disabled" : p2 ? "active" : "inactive") }).join(`
1248
+ ${c2}`)}
1249
+ ${a}
1250
+ `;
1251
+ }
1252
+ }
1253
+ } }).prompt();
1254
+ };
1170
1255
  var je = `${t("gray", d2)} `;
1171
1256
 
1172
- // src/add-command.ts
1173
- var {$: $2 } = globalThis.Bun;
1174
- async function main() {
1175
- const output = await $2`git status --porcelain -z`.quiet().text();
1176
- if (!output.trim()) {
1177
- console.log("\x1B[32m\u2714\x1B[0m No changes detected.");
1178
- return;
1257
+ // src/utils/multiselect.ts
1258
+ async function MultiSelect({
1259
+ message,
1260
+ options
1261
+ }) {
1262
+ const selected = await yt({
1263
+ message,
1264
+ options,
1265
+ required: true
1266
+ });
1267
+ if (q(selected)) {
1268
+ pt("Operation cancelled.");
1269
+ process.exit(0);
1179
1270
  }
1180
- const warnings = new Set;
1181
- const entries = output.split("\x00").filter(Boolean);
1182
- const changes = [];
1183
- for (let i = 0;i < entries.length; i++) {
1184
- const entry = entries[i];
1185
- if (!entry)
1186
- continue;
1187
- const status = entry.slice(0, 2);
1188
- const file = entry.slice(3);
1189
- const isStaged = status[0] !== " " && status[0] !== "?";
1190
- if (isStaged)
1191
- continue;
1192
- let displayPath = file;
1193
- const value = file;
1194
- if (status.startsWith("R") || status.startsWith("C")) {
1195
- const source = entries[++i];
1196
- displayPath = `${source} -> ${file}`;
1271
+ return selected;
1272
+ }
1273
+
1274
+ // src/modules/add/app/add-command.ts
1275
+ class AddCommand {
1276
+ getChangesUseCase;
1277
+ stageChangesUseCase;
1278
+ constructor(getChangesUseCase, stageChangesUseCase) {
1279
+ this.getChangesUseCase = getChangesUseCase;
1280
+ this.stageChangesUseCase = stageChangesUseCase;
1281
+ }
1282
+ async execute() {
1283
+ const { changes, warnings } = await this.getChangesUseCase.execute();
1284
+ if (changes.length === 0) {
1285
+ console.log(`${CHECK} All changes are either staged or sensitive (like .env).`);
1286
+ return;
1287
+ }
1288
+ const options = [
1289
+ ...changes.map((c2) => ({ value: c2.value, label: c2.label }))
1290
+ ];
1291
+ const selectedChanges = await MultiSelect({
1292
+ message: `Select the changes you want to commit. ${BLUE}[space] to select and${RESET} ${GREEN}[enter] to confirm${RESET} ${MAGENTA}[a] to select all${RESET} ${RED}[esc] to cancel${RESET}`,
1293
+ options
1294
+ });
1295
+ let selected = selectedChanges.map((file) => file.trim());
1296
+ if (selected.includes("all")) {
1297
+ selected = changes.map((c2) => c2.value);
1298
+ }
1299
+ if (selected.length > 0) {
1300
+ await this.stageChangesUseCase.execute(selected);
1301
+ if (warnings.size > 0) {
1302
+ console.log(`
1303
+ ${BG_YELLOW}${BLACK} WARNING ${RESET}`);
1304
+ for (const warning of warnings) {
1305
+ console.log(warning);
1306
+ }
1307
+ console.log("");
1308
+ }
1197
1309
  }
1198
- let label = "";
1310
+ }
1311
+ }
1312
+
1313
+ // src/modules/add/domain/change.ts
1314
+ class Change {
1315
+ props;
1316
+ constructor(props) {
1317
+ this.props = props;
1318
+ }
1319
+ get value() {
1320
+ return this.props.value;
1321
+ }
1322
+ get status() {
1323
+ return this.props.status;
1324
+ }
1325
+ get displayPath() {
1326
+ return this.props.displayPath;
1327
+ }
1328
+ get label() {
1329
+ const { status, displayPath } = this.props;
1330
+ let label = `${status}: ${displayPath}`;
1199
1331
  if (status.includes("M")) {
1200
- label = `\x1B[33mmodified:\x1B[0m ${displayPath}`;
1201
- } else if (status.includes("A") || status.includes("?")) {
1202
- label = `\x1B[32mnew file:\x1B[0m ${displayPath}`;
1203
- } else if (status.includes("D")) {
1204
- label = `\x1B[31mdeleted:\x1B[0m ${displayPath}`;
1205
- } else if (status.includes("R")) {
1206
- label = `\x1B[35mrenamed:\x1B[0m ${displayPath}`;
1207
- } else {
1208
- label = `${status}: ${displayPath}`;
1332
+ label = `${YELLOW}modified:${RESET} ${displayPath}`;
1209
1333
  }
1210
- const isSensitive = value.includes(".env") || value.includes("node_modules");
1211
- if (value.includes(".env")) {
1212
- warnings.add("\x1B[33m .env file hidden\x1B[0m (Add to .gitignore to avoid leaks)");
1334
+ if (status.includes("A") || status.includes("?")) {
1335
+ label = `${GREEN}new file:${RESET} ${displayPath}`;
1213
1336
  }
1214
- if (value.includes("node_modules")) {
1215
- warnings.add("\x1B[33m node_modules hidden\x1B[0m (Add to .gitignore to save space)");
1337
+ if (status.includes("D")) {
1338
+ label = `${RED}deleted:${RESET} ${displayPath}`;
1216
1339
  }
1217
- if (!isSensitive) {
1218
- changes.push({ value, label });
1340
+ if (status.includes("R")) {
1341
+ label = `${MAGENTA}renamed:${RESET} ${displayPath}`;
1219
1342
  }
1343
+ return label;
1220
1344
  }
1221
- if (changes.length === 0) {
1222
- console.log("\x1B[32m\u2714\x1B[0m All changes are either staged or sensitive (like .env).");
1223
- return;
1345
+ isSensitive() {
1346
+ return this.value.includes(".env") || this.value.includes("node_modules");
1224
1347
  }
1225
- const options = [
1226
- { value: "all", label: "\x1B[1mall changes\x1B[0m" },
1227
- ...changes
1228
- ];
1229
- const selectedChanges = await yt({
1230
- message: "Select the changes you want to commit. (select with space and confirm with enter)",
1231
- options,
1232
- required: true
1348
+ getWarning() {
1349
+ if (this.value.includes(".env")) {
1350
+ return `${YELLOW} .env file hidden${RESET} (Add to .gitignore to avoid leaks)`;
1351
+ }
1352
+ if (this.value.includes("node_modules")) {
1353
+ return `${YELLOW} node_modules hidden${RESET} (Add to .gitignore to save space)`;
1354
+ }
1355
+ return null;
1356
+ }
1357
+ static fromPorcelain(entries) {
1358
+ const changes = [];
1359
+ for (let i = 0;i < entries.length; i++) {
1360
+ const entry = entries[i];
1361
+ if (!entry)
1362
+ continue;
1363
+ const status = entry.slice(0, 2);
1364
+ const file = entry.slice(3);
1365
+ const isStaged = status[0] !== " " && status[0] !== "?";
1366
+ if (isStaged)
1367
+ continue;
1368
+ let displayPath = file;
1369
+ const value = file;
1370
+ if (status.startsWith("R") || status.startsWith("C")) {
1371
+ const source = entries[++i];
1372
+ displayPath = `${source} -> ${file}`;
1373
+ }
1374
+ changes.push(new Change({ value, status, displayPath }));
1375
+ }
1376
+ return changes;
1377
+ }
1378
+ }
1379
+
1380
+ // src/modules/add/app/get-changes.use-case.ts
1381
+ class GetChangesUseCase {
1382
+ gitRepository;
1383
+ constructor(gitRepository) {
1384
+ this.gitRepository = gitRepository;
1385
+ }
1386
+ async execute() {
1387
+ const entries = await this.gitRepository.getEntries();
1388
+ if (entries.length === 0) {
1389
+ return { changes: [], warnings: new Set };
1390
+ }
1391
+ const allChanges = Change.fromPorcelain(entries);
1392
+ const warnings = new Set;
1393
+ const changes = [];
1394
+ for (const change of allChanges) {
1395
+ const warning = change.getWarning();
1396
+ if (warning) {
1397
+ warnings.add(warning);
1398
+ }
1399
+ if (!change.isSensitive()) {
1400
+ changes.push(change);
1401
+ }
1402
+ }
1403
+ return { changes, warnings };
1404
+ }
1405
+ }
1406
+
1407
+ // src/modules/add/app/stage-changes.use-case.ts
1408
+ class StageChangesUseCase {
1409
+ gitRepository;
1410
+ constructor(gitRepository) {
1411
+ this.gitRepository = gitRepository;
1412
+ }
1413
+ async execute(files) {
1414
+ if (files.length === 0)
1415
+ return;
1416
+ await this.gitRepository.stageFiles(files);
1417
+ }
1418
+ }
1419
+
1420
+ // src/modules/add/infra/bun-git.repository.ts
1421
+ var {$: $2 } = globalThis.Bun;
1422
+
1423
+ class BunGitRepository {
1424
+ async getEntries() {
1425
+ const output = await $2`git status --porcelain -z`.quiet().text();
1426
+ if (!output.trim()) {
1427
+ return [];
1428
+ }
1429
+ return output.split("\x00").filter(Boolean);
1430
+ }
1431
+ async stageFiles(files) {
1432
+ if (files.length === 0)
1433
+ return;
1434
+ const proc = Bun.spawn(["git", "add", ...files], {
1435
+ stdio: ["inherit", "inherit", "inherit"]
1436
+ });
1437
+ const exitCode = await proc.exited;
1438
+ if (exitCode !== 0) {
1439
+ throw new Error(`Git add failed with exit code ${exitCode}`);
1440
+ }
1441
+ }
1442
+ }
1443
+
1444
+ // src/modules/add/main.ts
1445
+ async function addCommand() {
1446
+ const gitRepository = new BunGitRepository;
1447
+ const getChangesUseCase = new GetChangesUseCase(gitRepository);
1448
+ const stageChangesUseCase = new StageChangesUseCase(gitRepository);
1449
+ const addCommand2 = new AddCommand(getChangesUseCase, stageChangesUseCase);
1450
+ await addCommand2.execute();
1451
+ }
1452
+
1453
+ // src/modules/switch/domain/branch.ts
1454
+ class Branch {
1455
+ props;
1456
+ constructor(props) {
1457
+ this.props = props;
1458
+ }
1459
+ get name() {
1460
+ return this.props.name;
1461
+ }
1462
+ get isCurrentBranch() {
1463
+ return this.props.current;
1464
+ }
1465
+ get isRemoteBranch() {
1466
+ return this.props.remote;
1467
+ }
1468
+ get isHeadBranch() {
1469
+ return this.props.name.includes("HEAD");
1470
+ }
1471
+ static fromGitBranch(entries) {
1472
+ const branches = [];
1473
+ for (let i = 0;i < entries?.length; i++) {
1474
+ const entry = entries[i];
1475
+ if (!entry)
1476
+ continue;
1477
+ const current = entry.includes("*");
1478
+ const remote = entry.includes("remotes/");
1479
+ branches.push(new Branch({
1480
+ name: current ? entry.replace("*", "").trim() : entry,
1481
+ current,
1482
+ remote
1483
+ }));
1484
+ }
1485
+ return branches;
1486
+ }
1487
+ }
1488
+
1489
+ // src/modules/switch/app/get-branches.use-case.ts
1490
+ class GetBranchesUseCase {
1491
+ branchRepository;
1492
+ constructor(branchRepository) {
1493
+ this.branchRepository = branchRepository;
1494
+ }
1495
+ async execute() {
1496
+ const branches = await this.branchRepository.getBranches();
1497
+ const allBranches = Branch.fromGitBranch(branches);
1498
+ return allBranches;
1499
+ }
1500
+ }
1501
+
1502
+ // src/modules/switch/app/switch-branch.use-case.ts
1503
+ class SwitchBranch {
1504
+ branchRepository;
1505
+ constructor(branchRepository) {
1506
+ this.branchRepository = branchRepository;
1507
+ }
1508
+ async execute({ branch }) {
1509
+ await this.branchRepository.switchBranch({ branch });
1510
+ }
1511
+ }
1512
+
1513
+ // src/utils/select.ts
1514
+ async function Select({
1515
+ message,
1516
+ options
1517
+ }) {
1518
+ const selected = await _t({
1519
+ message,
1520
+ options
1233
1521
  });
1234
- if (q(selectedChanges)) {
1522
+ if (q(selected)) {
1235
1523
  pt("Operation cancelled.");
1236
1524
  process.exit(0);
1237
1525
  }
1238
- let selected = selectedChanges.map((file) => file.trim());
1239
- if (selected.includes("all")) {
1240
- selected = changes.map((c2) => c2.value);
1526
+ return selected;
1527
+ }
1528
+
1529
+ // src/modules/switch/app/switch-command.ts
1530
+ class SwitchCommand {
1531
+ getBranchesUseCase;
1532
+ switchBranch;
1533
+ constructor(getBranchesUseCase, switchBranch) {
1534
+ this.getBranchesUseCase = getBranchesUseCase;
1535
+ this.switchBranch = switchBranch;
1536
+ }
1537
+ async execute() {
1538
+ const branches = await this.getBranchesUseCase.execute();
1539
+ if (branches.length === 0) {
1540
+ console.log(`${CHECK} No branches found.`);
1541
+ return;
1542
+ }
1543
+ const options = [];
1544
+ for (const branch of branches) {
1545
+ let label = branch.name;
1546
+ let value = branch.name;
1547
+ if (branch.isHeadBranch)
1548
+ continue;
1549
+ if (branch.isCurrentBranch) {
1550
+ console.log(`${CHECK} Current branch: ${branch.name}`);
1551
+ continue;
1552
+ }
1553
+ if (branch.isRemoteBranch) {
1554
+ label = branch.name.replace("remotes/origin/", `${GREEN}remote branch:${RESET} `);
1555
+ value = branch.name.replace("remotes/origin/", "");
1556
+ }
1557
+ options.push({
1558
+ value,
1559
+ label
1560
+ });
1561
+ }
1562
+ const selectedBranch = await Select({
1563
+ message: "Select the branch you want to switch to.",
1564
+ options
1565
+ });
1566
+ await this.switchBranch.execute({ branch: selectedBranch });
1241
1567
  }
1242
- if (selected.length > 0) {
1243
- const proc = Bun.spawn(["git", "add", ...selected], {
1568
+ }
1569
+
1570
+ // src/modules/switch/infra/bun-switch-repository.ts
1571
+ var {$: $3 } = globalThis.Bun;
1572
+
1573
+ class BunSwitchRepository {
1574
+ async getBranches() {
1575
+ const output = await $3`git branch -a`.quiet().text();
1576
+ if (!output.trim()) {
1577
+ return [];
1578
+ }
1579
+ return output.split(`
1580
+ `).map((branch) => branch.trim());
1581
+ }
1582
+ async switchBranch({ branch }) {
1583
+ const proc = Bun.spawn(["git", "checkout", branch], {
1244
1584
  stdio: ["inherit", "inherit", "inherit"]
1245
1585
  });
1246
1586
  const exitCode = await proc.exited;
1247
- if (warnings.size > 0) {
1248
- console.log(`
1249
- \x1B[43m\x1B[30m WARNING \x1B[0m`);
1250
- for (const warning of warnings) {
1251
- console.log(warning);
1252
- }
1253
- console.log("");
1587
+ if (exitCode !== 0) {
1588
+ throw new Error(`Git checkout failed with exit code ${exitCode}`);
1254
1589
  }
1255
- process.exit(exitCode);
1256
1590
  }
1257
1591
  }
1258
1592
 
1593
+ // src/modules/switch/main.ts
1594
+ async function switchCommand() {
1595
+ const switchRepo = new BunSwitchRepository;
1596
+ const switchBranchUseCase = new SwitchBranch(switchRepo);
1597
+ const getBranchesUseCase = new GetBranchesUseCase(switchRepo);
1598
+ const switchCommand2 = new SwitchCommand(getBranchesUseCase, switchBranchUseCase);
1599
+ await switchCommand2.execute();
1600
+ }
1601
+
1259
1602
  // src/index.ts
1260
1603
  var args = Bun.argv.slice(2);
1261
- if (args[0] === "add" && !args[1]) {
1262
- await main();
1263
- } else {
1604
+ if (args[0] === "add" && !args[1])
1605
+ await addCommand();
1606
+ else if (args[0] === "switch" && !args[1])
1607
+ await switchCommand();
1608
+ else {
1264
1609
  const proc = Bun.spawn(["git", ...args], {
1265
1610
  stdio: ["inherit", "inherit", "inherit"]
1266
1611
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jannael/glinter",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "A high-performance, transparent Git wrapper with interactive staging",
5
5
  "type": "module",
6
6
  "private": false,
@@ -36,7 +36,10 @@
36
36
  "scripts": {
37
37
  "dev": "bun run src/index.ts",
38
38
  "build": "bun build ./src/index.ts --outfile=./dist/index.js --target=bun",
39
- "prepublishOnly": "bun run build"
39
+ "prepublishOnly": "bun run build",
40
+ "test": "vitest",
41
+ "lint": "biome check",
42
+ "lint:fix": "biome format --write"
40
43
  },
41
44
  "bin": {
42
45
  "g": "./dist/index.js"
@@ -46,7 +49,8 @@
46
49
  },
47
50
  "devDependencies": {
48
51
  "@biomejs/biome": "2.4.10",
49
- "@types/bun": "latest"
52
+ "@types/bun": "latest",
53
+ "vitest": "4.1.4"
50
54
  },
51
55
  "peerDependencies": {
52
56
  "bun": ">=1.0.0",