drivekit 1.0.0 → 1.0.2

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/dist/index.js CHANGED
@@ -578,6 +578,9 @@ var ResumableUploader = class {
578
578
  let chunkIndex = 0;
579
579
  try {
580
580
  while (bytesSent < totalBytes) {
581
+ if (options.signal?.aborted) {
582
+ throw new Error("Upload aborted by user signal");
583
+ }
581
584
  chunkIndex++;
582
585
  const currentChunkSize = Math.min(chunkSize, totalBytes - bytesSent);
583
586
  const buffer = Buffer.alloc(currentChunkSize);
@@ -638,6 +641,9 @@ var ResumableUploader = class {
638
641
  const totalChunks = Math.ceil(totalBytes / chunkSize) || 1;
639
642
  let chunkIndex = 0;
640
643
  while (bytesSent < totalBytes) {
644
+ if (options.signal?.aborted) {
645
+ throw new Error("Upload aborted by user signal");
646
+ }
641
647
  chunkIndex++;
642
648
  const currentChunkSize = Math.min(chunkSize, totalBytes - bytesSent);
643
649
  const chunk = buffer.subarray(bytesSent, bytesSent + currentChunkSize);
@@ -1546,6 +1552,155 @@ ${authUrl}
1546
1552
  });
1547
1553
  }
1548
1554
 
1555
+ // src/core/resource-refs.ts
1556
+ var DriveFileRef = class {
1557
+ drive;
1558
+ pathOrId;
1559
+ constructor(drive2, pathOrId) {
1560
+ this.drive = drive2;
1561
+ this.pathOrId = pathOrId;
1562
+ }
1563
+ /** Gets file metadata */
1564
+ async get() {
1565
+ return await this.drive.getFile(this.pathOrId);
1566
+ }
1567
+ /** Checks if the file exists */
1568
+ async exists() {
1569
+ return await this.drive.exists(this.pathOrId);
1570
+ }
1571
+ /** Reads file as UTF-8 string */
1572
+ async getText() {
1573
+ return await this.drive.getText(this.pathOrId);
1574
+ }
1575
+ /** Reads and parses JSON file */
1576
+ async getJson() {
1577
+ return await this.drive.getJson(this.pathOrId);
1578
+ }
1579
+ /** Downloads the file to destination */
1580
+ async download(optionsOrDestination) {
1581
+ const opts = typeof optionsOrDestination === "string" ? { destination: optionsOrDestination } : optionsOrDestination;
1582
+ return await this.drive.download(this.pathOrId, opts);
1583
+ }
1584
+ /** Moves the file to a new folder path or ID */
1585
+ async moveTo(newParentPathOrId) {
1586
+ return await this.drive.move(this.pathOrId, newParentPathOrId);
1587
+ }
1588
+ /** Copies the file to a target directory */
1589
+ async copyTo(parentPathOrId, newName) {
1590
+ return await this.drive.copy(this.pathOrId, { parent: parentPathOrId, name: newName });
1591
+ }
1592
+ /** Renames the file */
1593
+ async rename(newName) {
1594
+ return await this.drive.rename(this.pathOrId, newName);
1595
+ }
1596
+ /** Deletes the file (or moves to trash if permanent is false) */
1597
+ async delete(permanent = false) {
1598
+ return await this.drive.delete(this.pathOrId, permanent);
1599
+ }
1600
+ /** Moves file to trash */
1601
+ async trash() {
1602
+ return await this.drive.trash(this.pathOrId);
1603
+ }
1604
+ /** Shares file */
1605
+ async share(options) {
1606
+ return await this.drive.share(this.pathOrId, options);
1607
+ }
1608
+ /** Makes file publicly accessible */
1609
+ async sharePublic(role = "reader") {
1610
+ return await this.drive.sharePublic(this.pathOrId, role);
1611
+ }
1612
+ /** Gets public/preview links */
1613
+ async getLink() {
1614
+ return await this.drive.getShareableLink(this.pathOrId);
1615
+ }
1616
+ };
1617
+ var DriveFolderRef = class {
1618
+ drive;
1619
+ pathOrId;
1620
+ constructor(drive2, pathOrId) {
1621
+ this.drive = drive2;
1622
+ this.pathOrId = pathOrId;
1623
+ }
1624
+ /** Lists contents of this folder */
1625
+ async ls() {
1626
+ return await this.drive.ls(this.pathOrId);
1627
+ }
1628
+ /** Ensures this folder and subpaths exist (mkdir -p) */
1629
+ async mkdirp(subPath) {
1630
+ const target = subPath ? this.pathOrId.endsWith("/") ? `${this.pathOrId}${subPath}` : `${this.pathOrId}/${subPath}` : this.pathOrId;
1631
+ return await this.drive.mkdirp(target);
1632
+ }
1633
+ /** Uploads a file directly into this folder */
1634
+ async upload(source, options = {}) {
1635
+ return await this.drive.upload({
1636
+ ...options,
1637
+ source,
1638
+ parent: this.pathOrId
1639
+ });
1640
+ }
1641
+ /** Synchronizes a local directory into this remote folder */
1642
+ async syncFrom(localDir, options = {}) {
1643
+ return await this.drive.syncFolder(localDir, this.pathOrId, options);
1644
+ }
1645
+ /** Pulls this remote folder down to local directory */
1646
+ async pullTo(localDestinationDir, options = {}) {
1647
+ return await this.drive.pull(this.pathOrId, localDestinationDir, options);
1648
+ }
1649
+ /** Watches this folder for real-time changes */
1650
+ async watch(options = {}) {
1651
+ return await this.drive.watch(this.pathOrId, options);
1652
+ }
1653
+ };
1654
+
1655
+ // src/core/plugins.ts
1656
+ var PluginManager = class {
1657
+ plugins = [];
1658
+ use(plugin) {
1659
+ this.plugins.push(plugin);
1660
+ return this;
1661
+ }
1662
+ async runBeforeUpload(options) {
1663
+ let current = { ...options };
1664
+ for (const plugin of this.plugins) {
1665
+ if (plugin.beforeUpload) {
1666
+ const modified = await plugin.beforeUpload(current);
1667
+ if (modified) current = modified;
1668
+ }
1669
+ }
1670
+ return current;
1671
+ }
1672
+ async runAfterUpload(file) {
1673
+ let current = file;
1674
+ for (const plugin of this.plugins) {
1675
+ if (plugin.afterUpload) {
1676
+ const modified = await plugin.afterUpload(current);
1677
+ if (modified) current = modified;
1678
+ }
1679
+ }
1680
+ return current;
1681
+ }
1682
+ async runBeforeDownload(fileId, options) {
1683
+ let current = { ...options };
1684
+ for (const plugin of this.plugins) {
1685
+ if (plugin.beforeDownload) {
1686
+ const modified = await plugin.beforeDownload(fileId, current);
1687
+ if (modified) current = modified;
1688
+ }
1689
+ }
1690
+ return current;
1691
+ }
1692
+ async runAfterDownload(result) {
1693
+ let current = result;
1694
+ for (const plugin of this.plugins) {
1695
+ if (plugin.afterDownload) {
1696
+ const modified = await plugin.afterDownload(current);
1697
+ if (modified) current = modified;
1698
+ }
1699
+ }
1700
+ return current;
1701
+ }
1702
+ };
1703
+
1549
1704
  // src/core/client.ts
1550
1705
  var DriveKit = class {
1551
1706
  options;
@@ -1555,11 +1710,31 @@ var DriveKit = class {
1555
1710
  uploaderInstance = null;
1556
1711
  downloaderInstance = null;
1557
1712
  rootFolderId;
1713
+ pluginManager = new PluginManager();
1558
1714
  constructor(options = {}) {
1559
1715
  this.options = options;
1560
1716
  this.rootFolderId = options.rootFolderId || "root";
1561
1717
  this.authenticator = new DriveAuthenticator(options.auth);
1562
1718
  }
1719
+ /**
1720
+ * Registers a middleware plugin into DriveKit lifecycle
1721
+ */
1722
+ use(plugin) {
1723
+ this.pluginManager.use(plugin);
1724
+ return this;
1725
+ }
1726
+ /**
1727
+ * Returns a fluent, chained reference to a specific file
1728
+ */
1729
+ file(pathOrId) {
1730
+ return new DriveFileRef(this, pathOrId);
1731
+ }
1732
+ /**
1733
+ * Returns a fluent, chained reference to a specific folder
1734
+ */
1735
+ folder(pathOrId) {
1736
+ return new DriveFolderRef(this, pathOrId);
1737
+ }
1563
1738
  /**
1564
1739
  * Initializes the Google Drive API client (lazy-loaded if not called explicitly)
1565
1740
  */
@@ -1668,21 +1843,23 @@ var DriveKit = class {
1668
1843
  * - Real-time progress callback: % complete, transfer speed (MB/s), ETA
1669
1844
  */
1670
1845
  async upload(options) {
1846
+ const opts = await this.pluginManager.runBeforeUpload(options);
1671
1847
  const uploader = await this.getUploader();
1672
- let parentId = options.parent;
1848
+ let parentId = opts.parent;
1673
1849
  if (parentId && parentId.startsWith("/")) {
1674
1850
  parentId = await this.mkdirp(parentId);
1675
1851
  }
1676
- if (options.overwrite && options.name && parentId) {
1677
- const existing = await this.query().inFolder(parentId).nameEquals(options.name).notTrashed().first();
1852
+ if (opts.overwrite && opts.name && parentId) {
1853
+ const existing = await this.query().inFolder(parentId).nameEquals(opts.name).notTrashed().first();
1678
1854
  if (existing?.id) {
1679
1855
  await this.delete(existing.id, true);
1680
1856
  }
1681
1857
  }
1682
- return await uploader.upload({
1683
- ...options,
1858
+ const uploaded = await uploader.upload({
1859
+ ...opts,
1684
1860
  parent: parentId
1685
1861
  });
1862
+ return await this.pluginManager.runAfterUpload(uploaded);
1686
1863
  }
1687
1864
  /**
1688
1865
  * Downloads a file from Google Drive:
@@ -1692,8 +1869,10 @@ var DriveKit = class {
1692
1869
  */
1693
1870
  async download(fileIdOrPath, options = {}) {
1694
1871
  const fileId = await this.resolveId(fileIdOrPath);
1872
+ const processedOpts = await this.pluginManager.runBeforeDownload(fileId, options);
1695
1873
  const downloader = await this.getDownloader();
1696
- return await downloader.download(fileId, options);
1874
+ const result = await downloader.download(fileId, processedOpts);
1875
+ return await this.pluginManager.runAfterDownload(result);
1697
1876
  }
1698
1877
  /**
1699
1878
  * Gets file metadata
@@ -2108,6 +2287,158 @@ var DriveKit = class {
2108
2287
  }
2109
2288
  };
2110
2289
 
2290
+ // src/cli/terminal-ui.ts
2291
+ import readline from "readline";
2292
+ import chalk from "chalk";
2293
+ function terminalLink(text, url) {
2294
+ return `\x1B]8;;${url}\x07${text}\x1B]8;;\x07`;
2295
+ }
2296
+ function renderFileRow(item, index) {
2297
+ const isDir = item.mimeType === "application/vnd.google-apps.folder";
2298
+ const icon = isDir ? "\u{1F4C1}" : getFileIcon(item.mimeType || "");
2299
+ const prefix = index !== void 0 ? `${chalk.dim((index + 1).toString().padStart(2, " ") + ".")} ` : "";
2300
+ let nameFormatted = isDir ? chalk.bold.blue(item.name || "untitled") : chalk.white(item.name || "untitled");
2301
+ if (item.webViewLink) {
2302
+ nameFormatted = terminalLink(nameFormatted, item.webViewLink);
2303
+ }
2304
+ const sizeFormatted = item.size ? chalk.dim(`[${formatBytes(parseInt(item.size, 10)).padStart(9, " ")}]`) : chalk.dim(isDir ? "[ FOLDER ]" : "[ DOC ]");
2305
+ const idFormatted = chalk.dim(`ID: ${item.id}`);
2306
+ return `${prefix}${icon} ${nameFormatted.padEnd(45)} ${sizeFormatted} ${idFormatted}`;
2307
+ }
2308
+ function getFileIcon(mimeType) {
2309
+ if (mimeType.includes("pdf")) return "\u{1F4D5}";
2310
+ if (mimeType.includes("image")) return "\u{1F5BC}\uFE0F ";
2311
+ if (mimeType.includes("video")) return "\u{1F3AC}";
2312
+ if (mimeType.includes("audio")) return "\u{1F3B5}";
2313
+ if (mimeType.includes("zip") || mimeType.includes("compressed") || mimeType.includes("tar")) return "\u{1F4E6}";
2314
+ if (mimeType.includes("spreadsheet") || mimeType.includes("excel")) return "\u{1F4CA}";
2315
+ if (mimeType.includes("document") || mimeType.includes("word")) return "\u{1F4DD}";
2316
+ if (mimeType.includes("presentation")) return "\u{1F4FD}\uFE0F ";
2317
+ if (mimeType.includes("json") || mimeType.includes("javascript") || mimeType.includes("typescript")) return "\u2699\uFE0F ";
2318
+ return "\u{1F4C4}";
2319
+ }
2320
+ async function runInteractiveExplorer(client, initialFolder = "/") {
2321
+ let currentFolder = initialFolder;
2322
+ let items = [];
2323
+ let selectedIndex = 0;
2324
+ let history = [];
2325
+ const refreshItems = async () => {
2326
+ items = await client.ls(currentFolder);
2327
+ selectedIndex = Math.min(selectedIndex, Math.max(0, items.length - 1));
2328
+ };
2329
+ await refreshItems();
2330
+ if (!process.stdin.isTTY) {
2331
+ console.log(chalk.yellow("Interactive explorer requires a TTY terminal. Listing items instead:"));
2332
+ for (const item of items) {
2333
+ console.log(renderFileRow(item));
2334
+ }
2335
+ return;
2336
+ }
2337
+ readline.emitKeypressEvents(process.stdin);
2338
+ process.stdin.setRawMode(true);
2339
+ const renderScreen = () => {
2340
+ console.clear();
2341
+ console.log(chalk.bold.cyan("\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557"));
2342
+ console.log(chalk.bold.cyan("\u2551") + chalk.bold.yellow(" \u26A1 DriveKit Interactive Terminal Explorer ") + chalk.bold.cyan("\u2551"));
2343
+ console.log(chalk.bold.cyan("\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563"));
2344
+ console.log(chalk.bold.cyan("\u2551") + ` \u{1F4C2} Path: ${chalk.bold.green(currentFolder.padEnd(72))} ` + chalk.bold.cyan("\u2551"));
2345
+ console.log(chalk.bold.cyan("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D"));
2346
+ console.log(chalk.dim(" [\u2191/\u2193] Navigate \u2022 [Enter] Open Folder \u2022 [B] Back \u2022 [S] Share \u2022 [Q] Exit\n"));
2347
+ if (items.length === 0) {
2348
+ console.log(chalk.dim(" (This folder is empty)\n"));
2349
+ return;
2350
+ }
2351
+ const pageSize = 12;
2352
+ const startIdx = Math.max(0, Math.min(selectedIndex - Math.floor(pageSize / 2), items.length - pageSize));
2353
+ const endIdx = Math.min(items.length, startIdx + pageSize);
2354
+ for (let i = startIdx; i < endIdx; i++) {
2355
+ const isSelected = i === selectedIndex;
2356
+ const cursor = isSelected ? chalk.bold.cyan(" \u25BA ") : " ";
2357
+ const row = renderFileRow(items[i]);
2358
+ if (isSelected) {
2359
+ console.log(chalk.bgHex("#1e293b")(cursor + row));
2360
+ } else {
2361
+ console.log(cursor + row);
2362
+ }
2363
+ }
2364
+ console.log(chalk.dim(`
2365
+ Item ${selectedIndex + 1} of ${items.length}`));
2366
+ };
2367
+ renderScreen();
2368
+ return new Promise((resolve) => {
2369
+ const onKeypress = async (str, key) => {
2370
+ if (key.ctrl && key.name === "c") {
2371
+ cleanup();
2372
+ process.exit(0);
2373
+ }
2374
+ if (key.name === "q" || key.name === "escape" && history.length === 0) {
2375
+ cleanup();
2376
+ resolve();
2377
+ return;
2378
+ }
2379
+ if (key.name === "up") {
2380
+ selectedIndex = selectedIndex > 0 ? selectedIndex - 1 : items.length - 1;
2381
+ renderScreen();
2382
+ } else if (key.name === "down") {
2383
+ selectedIndex = selectedIndex < items.length - 1 ? selectedIndex + 1 : 0;
2384
+ renderScreen();
2385
+ } else if (key.name === "return") {
2386
+ const item = items[selectedIndex];
2387
+ if (item && item.mimeType === "application/vnd.google-apps.folder") {
2388
+ history.push(currentFolder);
2389
+ currentFolder = currentFolder === "/" ? `/${item.name}` : `${currentFolder}/${item.name}`;
2390
+ selectedIndex = 0;
2391
+ await refreshItems();
2392
+ renderScreen();
2393
+ } else if (item && item.webViewLink) {
2394
+ const { exec: exec2 } = await import("child_process");
2395
+ const openCmd = process.platform === "darwin" ? `open "${item.webViewLink}"` : `xdg-open "${item.webViewLink}"`;
2396
+ exec2(openCmd);
2397
+ renderScreen();
2398
+ }
2399
+ } else if (key.name === "b" || key.name === "backspace") {
2400
+ if (history.length > 0) {
2401
+ currentFolder = history.pop();
2402
+ selectedIndex = 0;
2403
+ await refreshItems();
2404
+ renderScreen();
2405
+ }
2406
+ } else if (str === "s" || str === "S") {
2407
+ const item = items[selectedIndex];
2408
+ if (item && item.id) {
2409
+ process.stdin.pause();
2410
+ console.log(`
2411
+
2412
+ ${chalk.cyan("\u{1F517} Getting shareable link for:")} ${chalk.bold(item.name)}...`);
2413
+ try {
2414
+ const links = await client.sharePublic(item.id);
2415
+ console.log(`Preview URL: ${chalk.underline(links.previewLink)}`);
2416
+ console.log(`Direct Download URL: ${chalk.underline(links.directDownloadLink)}`);
2417
+ console.log(chalk.dim("\nPress any key to return to explorer..."));
2418
+ process.stdin.resume();
2419
+ process.stdin.once("data", () => {
2420
+ renderScreen();
2421
+ });
2422
+ } catch (err) {
2423
+ console.error(chalk.red(`Failed to share: ${err.message}`));
2424
+ setTimeout(renderScreen, 2e3);
2425
+ }
2426
+ }
2427
+ }
2428
+ };
2429
+ const cleanup = () => {
2430
+ process.stdin.removeListener("keypress", onKeypress);
2431
+ if (process.stdin.setRawMode) {
2432
+ process.stdin.setRawMode(false);
2433
+ }
2434
+ process.stdin.pause();
2435
+ console.clear();
2436
+ console.log(chalk.green("\u{1F44B} Exited DriveKit explorer."));
2437
+ };
2438
+ process.stdin.on("keypress", onKeypress);
2439
+ });
2440
+ }
2441
+
2111
2442
  // src/index.ts
2112
2443
  async function createDrive(options) {
2113
2444
  const instance = new DriveKit(options);
@@ -2123,6 +2454,8 @@ export {
2123
2454
  Downloader,
2124
2455
  DriveAuthError,
2125
2456
  DriveAuthenticator,
2457
+ DriveFileRef,
2458
+ DriveFolderRef,
2126
2459
  DriveKit,
2127
2460
  DriveKitError,
2128
2461
  DriveNotFoundError,
@@ -2134,6 +2467,7 @@ export {
2134
2467
  MIN_CHUNK_SIZE,
2135
2468
  MULTIPART_THRESHOLD,
2136
2469
  PathResolver,
2470
+ PluginManager,
2137
2471
  ResumableUploader,
2138
2472
  SmartSyncEngine,
2139
2473
  StorageCleaner,
@@ -2144,10 +2478,14 @@ export {
2144
2478
  formatDuration,
2145
2479
  formatSpeed,
2146
2480
  getExtensionForMime,
2481
+ getFileIcon,
2147
2482
  isGoogleWorkspaceDoc,
2148
2483
  isRetryableError,
2149
2484
  loginWithBrowser,
2150
2485
  lookupMimeType,
2486
+ renderFileRow,
2487
+ runInteractiveExplorer,
2488
+ terminalLink,
2151
2489
  withRetry
2152
2490
  };
2153
2491
  //# sourceMappingURL=index.js.map