mirror of
https://github.com/block/buzz.git
synced 2026-12-01 13:42:45 +00:00
feat(desktop): grouped system events with collapsible UI
This commit is contained in:
54
desktop/scripts/resolve-at-alias.mjs
Normal file
54
desktop/scripts/resolve-at-alias.mjs
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* ESM resolve hook that maps `@/*` imports to `<projectRoot>/src/*`.
|
||||
* Also resolves extensionless imports to `.ts` files (TypeScript convention).
|
||||
*/
|
||||
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
|
||||
const projectRoot = path.resolve(
|
||||
path.dirname(fileURLToPath(import.meta.url)),
|
||||
"..",
|
||||
);
|
||||
const srcDir = path.join(projectRoot, "src");
|
||||
|
||||
/**
|
||||
* Try to resolve a file path that may be missing its extension.
|
||||
* Returns the resolved file URL or null.
|
||||
*/
|
||||
function tryResolveFile(filePath) {
|
||||
// Try exact path first
|
||||
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
|
||||
return pathToFileURL(filePath).href;
|
||||
}
|
||||
// Try with .ts extension
|
||||
const withTs = `${filePath}.ts`;
|
||||
if (fs.existsSync(withTs)) {
|
||||
return pathToFileURL(withTs).href;
|
||||
}
|
||||
// Try with .tsx extension
|
||||
const withTsx = `${filePath}.tsx`;
|
||||
if (fs.existsSync(withTsx)) {
|
||||
return pathToFileURL(withTsx).href;
|
||||
}
|
||||
// Try as directory with index.ts
|
||||
const indexTs = path.join(filePath, "index.ts");
|
||||
if (fs.existsSync(indexTs)) {
|
||||
return pathToFileURL(indexTs).href;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function resolve(specifier, context, nextResolve) {
|
||||
if (specifier.startsWith("@/")) {
|
||||
const bare = path.join(srcDir, specifier.slice(2));
|
||||
const resolved = tryResolveFile(bare);
|
||||
if (resolved) {
|
||||
return nextResolve(resolved, context);
|
||||
}
|
||||
// Fallback — let Node try (will likely fail with a clear error)
|
||||
return nextResolve(pathToFileURL(bare).href, context);
|
||||
}
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
11
desktop/scripts/test-loader.mjs
Normal file
11
desktop/scripts/test-loader.mjs
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Custom Node ESM loader that resolves `@/` path aliases to `./src/`
|
||||
* relative to the desktop project root.
|
||||
*
|
||||
* Usage:
|
||||
* node --experimental-strip-types --import ./scripts/test-loader.mjs --test src/path/to.test.mjs
|
||||
*/
|
||||
|
||||
import { register } from "node:module";
|
||||
|
||||
register("./resolve-at-alias.mjs", import.meta.url);
|
||||
35
desktop/scripts/test-resolve-hook.mjs
Normal file
35
desktop/scripts/test-resolve-hook.mjs
Normal file
@@ -0,0 +1,35 @@
|
||||
// Node.js customization hooks — resolves @/ path alias and .ts extensions.
|
||||
// Loaded via: node --import ./scripts/test-loader.mjs
|
||||
import { existsSync } from "node:fs";
|
||||
import { resolve as pathResolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const srcDir = pathResolve(fileURLToPath(import.meta.url), "../../src");
|
||||
|
||||
export function resolve(specifier, context, nextResolve) {
|
||||
let mapped = specifier;
|
||||
|
||||
// Resolve @/ alias to src/
|
||||
if (mapped.startsWith("@/")) {
|
||||
mapped = pathToFileURL(pathResolve(srcDir, mapped.slice(2))).href;
|
||||
}
|
||||
|
||||
// If the specifier (after alias resolution) looks like a local/file path
|
||||
// without an extension, try appending .ts
|
||||
if (
|
||||
mapped.startsWith("file://") &&
|
||||
!mapped.endsWith(".ts") &&
|
||||
!mapped.endsWith(".mjs") &&
|
||||
!mapped.endsWith(".js")
|
||||
) {
|
||||
const filePath = fileURLToPath(mapped);
|
||||
if (!existsSync(filePath) && existsSync(`${filePath}.ts`)) {
|
||||
mapped = `${mapped}.ts`;
|
||||
}
|
||||
}
|
||||
|
||||
if (mapped !== specifier) {
|
||||
return nextResolve(mapped, context);
|
||||
}
|
||||
return nextResolve(specifier, context);
|
||||
}
|
||||
Reference in New Issue
Block a user