Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.8 KB
Raw Blame
/**
 * Transpiles DocumentEditor + EditError to plain JS and writes editor-sandbox-code.ts.
 * Run via `bun scripts/generate-sandbox.ts` (wired into prebuild).
 */
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const t = new Bun.Transpiler({ loader: 'ts' });

async function transpile(relPath: string): Promise<string> {
  const src = await Bun.file(join(root, relPath)).text();
  // Strip import/export — QuickJS evaluates a plain script, not an ES module.
  return t
    .transformSync(src)
    .replace(/^import\s.*$/gm, '')
    .replace(/^export\s+(?:default\s+)?/gm, '')
    .trim();
}

/** Biome formats this generated file with single-quoted string literals. */
function asSingleQuotedTsString(value: string): string {
  return `'${value
    .replace(/\\/g, '\\\\')
    .replace(/'/g, "\\'")
    .replace(/\r/g, '\\r')
    .replace(/\n/g, '\\n')
    .replace(/\u2028/g, '\\u2028')
    .replace(/\u2029/g, '\\u2029')}'`;
}

const code = [
  await transpile('src/ai-editing/editor/errors.ts'),
  await transpile('src/ai-editing/editor/document-editor.ts'),
].join('\n\n');

const out = `// Auto-generated by scripts/generate-sandbox.ts — do not edit.
// Source of truth: src/ai-editing/editor/document-editor.ts
// biome-ignore-all lint/suspicious/noTemplateCurlyInString: generated JS contains template literals
export const SANDBOX_CODE =
  ${asSingleQuotedTsString(code)};
`;

await Bun.write(join(root, 'src/editor-sandbox-code.ts'), out);
console.log('generated src/editor-sandbox-code.ts');

const canvas = await transpile('src/canvas/editor.ts');
await Bun.write(
  join(root, 'src/canvas/sandbox-code.ts'),
  `// Auto-generated by scripts/generate-sandbox.ts — do not edit.\nexport const CANVAS_SANDBOX_CODE =\n  ${asSingleQuotedTsString(canvas)};\n`
);