feat: rewrite of publish to go back to js from binaries (#3)
This commit is contained in:
@@ -70,8 +70,6 @@ export type AdvancedDiffResult =
|
||||
|
||||
function readFileOrNull(p: string): string | null {
|
||||
try {
|
||||
const _file = Bun.file(p);
|
||||
// Note: Bun.file().text() is async, but we need sync for diff preview
|
||||
// Fall back to node:fs for sync reading
|
||||
return require("node:fs").readFileSync(p, "utf-8");
|
||||
} catch {
|
||||
|
||||
@@ -46,7 +46,7 @@ async function main() {
|
||||
let values: Record<string, unknown>;
|
||||
try {
|
||||
const parsed = parseArgs({
|
||||
args: Bun.argv,
|
||||
args: process.argv,
|
||||
options: {
|
||||
help: { type: "boolean", short: "h" },
|
||||
continue: { type: "boolean", short: "c" },
|
||||
@@ -149,7 +149,7 @@ async function main() {
|
||||
await upsertToolsToServer(client);
|
||||
|
||||
const { handleHeadlessCommand } = await import("./headless");
|
||||
await handleHeadlessCommand(Bun.argv);
|
||||
await handleHeadlessCommand(process.argv);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { exists, readFile, writeFile } from "../utils/fs.js";
|
||||
// src/permissions/loader.ts
|
||||
// Load and merge permission settings from hierarchical sources
|
||||
|
||||
@@ -38,10 +39,10 @@ export async function loadPermissions(
|
||||
];
|
||||
|
||||
for (const settingsPath of sources) {
|
||||
const file = Bun.file(settingsPath);
|
||||
try {
|
||||
if (await file.exists()) {
|
||||
const settings = (await file.json()) as SettingsFile;
|
||||
if (exists(settingsPath)) {
|
||||
const content = await readFile(settingsPath);
|
||||
const settings = JSON.parse(content) as SettingsFile;
|
||||
if (settings.permissions) {
|
||||
mergePermissions(merged, settings.permissions as PermissionRules);
|
||||
}
|
||||
@@ -103,11 +104,11 @@ export async function savePermissionRule(
|
||||
}
|
||||
|
||||
// Load existing settings
|
||||
const file = Bun.file(settingsPath);
|
||||
let settings: SettingsFile = {};
|
||||
try {
|
||||
if (await file.exists()) {
|
||||
settings = (await file.json()) as SettingsFile;
|
||||
if (exists(settingsPath)) {
|
||||
const content = await readFile(settingsPath);
|
||||
settings = JSON.parse(content) as SettingsFile;
|
||||
}
|
||||
} catch (_error) {
|
||||
// Start with empty settings if file doesn't exist or is invalid
|
||||
@@ -126,8 +127,8 @@ export async function savePermissionRule(
|
||||
settings.permissions[ruleType].push(rule);
|
||||
}
|
||||
|
||||
// Save settings (Bun.write creates parent directories automatically)
|
||||
await Bun.write(settingsPath, JSON.stringify(settings, null, 2));
|
||||
// Save settings
|
||||
await writeFile(settingsPath, JSON.stringify(settings, null, 2));
|
||||
|
||||
// If saving to .letta/settings.local.json, ensure it's gitignored
|
||||
if (scope === "local") {
|
||||
@@ -142,14 +143,12 @@ async function ensureLocalSettingsIgnored(
|
||||
workingDirectory: string,
|
||||
): Promise<void> {
|
||||
const gitignorePath = join(workingDirectory, ".gitignore");
|
||||
const gitignoreFile = Bun.file(gitignorePath);
|
||||
|
||||
const pattern = ".letta/settings.local.json";
|
||||
|
||||
try {
|
||||
let content = "";
|
||||
if (await gitignoreFile.exists()) {
|
||||
content = await gitignoreFile.text();
|
||||
if (exists(gitignorePath)) {
|
||||
content = await readFile(gitignorePath);
|
||||
}
|
||||
|
||||
// Check if pattern already exists
|
||||
@@ -158,7 +157,7 @@ async function ensureLocalSettingsIgnored(
|
||||
const newContent = `${
|
||||
content + (content.endsWith("\n") ? "" : "\n") + pattern
|
||||
}\n`;
|
||||
await Bun.write(gitignorePath, newContent);
|
||||
await writeFile(gitignorePath, newContent);
|
||||
}
|
||||
} catch (_error) {
|
||||
// Silently fail if we can't update .gitignore
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Manages project-level settings stored in ./.letta/settings.json
|
||||
|
||||
import { join } from "node:path";
|
||||
import { exists, readFile, writeFile } from "./utils/fs.js";
|
||||
|
||||
export interface ProjectSettings {
|
||||
localSharedBlockIds: Record<string, string>; // label -> blockId mapping for project-local blocks
|
||||
@@ -28,14 +29,14 @@ export async function loadProjectSettings(
|
||||
workingDirectory: string = process.cwd(),
|
||||
): Promise<ProjectSettings> {
|
||||
const settingsPath = getProjectSettingsPath(workingDirectory);
|
||||
const file = Bun.file(settingsPath);
|
||||
|
||||
try {
|
||||
if (!(await file.exists())) {
|
||||
if (!exists(settingsPath)) {
|
||||
return DEFAULT_PROJECT_SETTINGS;
|
||||
}
|
||||
|
||||
const settings = (await file.json()) as RawProjectSettings;
|
||||
const content = await readFile(settingsPath);
|
||||
const settings = JSON.parse(content) as RawProjectSettings;
|
||||
|
||||
// Extract only localSharedBlockIds (permissions and other fields handled elsewhere)
|
||||
return {
|
||||
@@ -56,13 +57,13 @@ export async function saveProjectSettings(
|
||||
updates: Partial<ProjectSettings>,
|
||||
): Promise<void> {
|
||||
const settingsPath = getProjectSettingsPath(workingDirectory);
|
||||
const file = Bun.file(settingsPath);
|
||||
|
||||
try {
|
||||
// Read existing settings (might have permissions, etc.)
|
||||
let existingSettings: RawProjectSettings = {};
|
||||
if (await file.exists()) {
|
||||
existingSettings = (await file.json()) as RawProjectSettings;
|
||||
if (exists(settingsPath)) {
|
||||
const content = await readFile(settingsPath);
|
||||
existingSettings = JSON.parse(content) as RawProjectSettings;
|
||||
}
|
||||
|
||||
// Merge updates with existing settings
|
||||
@@ -71,8 +72,7 @@ export async function saveProjectSettings(
|
||||
...updates,
|
||||
};
|
||||
|
||||
// Bun.write automatically creates parent directories (.letta/)
|
||||
await Bun.write(settingsPath, JSON.stringify(newSettings, null, 2));
|
||||
await writeFile(settingsPath, JSON.stringify(newSettings, null, 2));
|
||||
} catch (error) {
|
||||
console.error("Error saving project settings:", error);
|
||||
throw error;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { PermissionRules } from "./permissions/types";
|
||||
import { exists, readFile, writeFile } from "./utils/fs.js";
|
||||
|
||||
export type UIMode = "simple" | "rich";
|
||||
|
||||
@@ -32,18 +33,18 @@ function getSettingsPath(): string {
|
||||
*/
|
||||
export async function loadSettings(): Promise<Settings> {
|
||||
const settingsPath = getSettingsPath();
|
||||
const file = Bun.file(settingsPath);
|
||||
|
||||
try {
|
||||
// Check if settings file exists
|
||||
if (!(await file.exists())) {
|
||||
// Create default settings file (Bun.write auto-creates parent directories)
|
||||
if (!exists(settingsPath)) {
|
||||
// Create default settings file
|
||||
await saveSettings(DEFAULT_SETTINGS);
|
||||
return DEFAULT_SETTINGS;
|
||||
}
|
||||
|
||||
// Read and parse settings using Bun's built-in JSON parser
|
||||
const settings = (await file.json()) as Settings;
|
||||
// Read and parse settings
|
||||
const content = await readFile(settingsPath);
|
||||
const settings = JSON.parse(content) as Settings;
|
||||
|
||||
// Merge with defaults in case new fields were added
|
||||
return { ...DEFAULT_SETTINGS, ...settings };
|
||||
@@ -60,8 +61,7 @@ export async function saveSettings(settings: Settings): Promise<void> {
|
||||
const settingsPath = getSettingsPath();
|
||||
|
||||
try {
|
||||
// Bun.write automatically creates parent directories
|
||||
await Bun.write(settingsPath, JSON.stringify(settings, null, 2));
|
||||
await writeFile(settingsPath, JSON.stringify(settings, null, 2));
|
||||
} catch (error) {
|
||||
console.error("Error saving settings:", error);
|
||||
throw error;
|
||||
|
||||
37
src/utils/fs.ts
Normal file
37
src/utils/fs.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File system utilities using Node.js APIs
|
||||
* Compatible with both Node.js and Bun
|
||||
*/
|
||||
|
||||
import {
|
||||
existsSync,
|
||||
readFileSync as fsReadFileSync,
|
||||
writeFileSync as fsWriteFileSync,
|
||||
mkdirSync,
|
||||
} from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
|
||||
/**
|
||||
* Read a file and return its contents as text
|
||||
*/
|
||||
export async function readFile(path: string): Promise<string> {
|
||||
return fsReadFileSync(path, { encoding: "utf-8" });
|
||||
}
|
||||
|
||||
/**
|
||||
* Write content to a file, creating parent directories if needed
|
||||
*/
|
||||
export async function writeFile(path: string, content: string): Promise<void> {
|
||||
const dir = dirname(path);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
fsWriteFileSync(path, content, { encoding: "utf-8", flush: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file exists
|
||||
*/
|
||||
export function exists(path: string): boolean {
|
||||
return existsSync(path);
|
||||
}
|
||||
Reference in New Issue
Block a user