Skip to content

Commit 5c500da

Browse files
committed
refactor(runtime): consolidate package resolution and resource embedding
Move package resolution utilities to shared/runtime directory and implement new resource embedding system using Bun's native file embedding. Remove old embedded resource generation system and update build scripts to use the new approach. The changes include: - New pkg.ts, root.ts, and embed.ts modules in shared/runtime - Removal of old embedded resource generation scripts and utilities - Updates to build scripts to use Bun's file embedding - Migration of all package.json/root resolution imports to new location
1 parent 6688a0e commit 5c500da

File tree

22 files changed

+247
-619
lines changed

22 files changed

+247
-619
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ package-lock.json
1818
# Ignore folders starting with .tmp
1919
.tmp*
2020
.vscode
21+
22+
# Generated resource manifest for bundling
23+
src/shared/runtime/resource-manifest.ts
2124
.codemachine/logs
2225
.codemachine/prompts
2326
.codemachine/agents
2427
.codemachine/template.json
2528

2629
# MkDocs build output
2730
site/
28-
src/shared/resources/embedded-resources.json

bun.lock

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
"typescript-eslint": "^8.44.1",
5454
},
5555
"optionalDependencies": {
56-
"codemachine-darwin-arm64": "0.6.7",
57-
"codemachine-darwin-x64": "0.6.7",
58-
"codemachine-linux-x64": "0.6.7",
59-
"codemachine-windows-x64": "0.6.7",
56+
"codemachine-darwin-arm64": "0.7.0",
57+
"codemachine-darwin-x64": "0.7.0",
58+
"codemachine-linux-x64": "0.7.0",
59+
"codemachine-windows-x64": "0.7.0",
6060
},
6161
},
6262
},
@@ -495,6 +495,14 @@
495495

496496
"code-excerpt": ["code-excerpt@4.0.0", "", { "dependencies": { "convert-to-spaces": "^2.0.1" } }, "sha512-xxodCmBen3iy2i0WtAK8FlFNrRzjUqjRsMfho58xT/wvZU1YTM3fCnRjcy1gJPMepaRlgm/0e6w8SpWHpn3/cA=="],
497497

498+
"codemachine-darwin-arm64": ["codemachine-darwin-arm64@0.7.0", "", { "os": "darwin", "cpu": "arm64", "bin": { "cm": "codemachine", "codemachine": "codemachine", "codemachine-workflow": "codemachine-workflow" } }, "sha512-mSd2Q0nXZlhSmvtbMk67jQ721WPtkvV7QN1EufQbaRIG+9LPOBJqxyRsn91EGyQj9ybZVH85IXGeyH3VaQ+Z9g=="],
499+
500+
"codemachine-darwin-x64": ["codemachine-darwin-x64@0.7.0", "", { "os": "darwin", "cpu": "x64", "bin": { "cm": "codemachine", "codemachine": "codemachine", "codemachine-workflow": "codemachine-workflow" } }, "sha512-LA8HZgB+fqszRa0JygtZTXVyJRkOO1Iqmzv/Jobn5qVXSF9r1F7p4WHF6sOSquUclkn3SJGDOs9nUSJQFD120w=="],
501+
502+
"codemachine-linux-x64": ["codemachine-linux-x64@0.7.0", "", { "os": "linux", "cpu": "x64", "bin": { "cm": "codemachine", "codemachine": "codemachine", "codemachine-workflow": "codemachine-workflow" } }, "sha512-Q8ShXnVQ1HMkyX1MK5Egi2zprGgcm+SBUzhJwSuzMbNbn1cC4J0CWBTtdw8jYE27Uxf9G6rXBWvz0VWMOptIew=="],
503+
504+
"codemachine-windows-x64": ["codemachine-windows-x64@0.7.0", "", { "os": "win32", "cpu": "x64", "bin": { "cm": "codemachine.exe", "codemachine": "codemachine.exe", "codemachine-workflow": "codemachine-workflow.exe" } }, "sha512-DcWuLfrbnOQgMytSm7M3IJDtOdMMJKeEhMLhaOaZbZpRwhhizMnkrzcVe5/gfyIg42mvgqDUjaZDt6dqt8GP3A=="],
505+
498506
"color-convert": ["color-convert@2.0.1", "", { "dependencies": { "color-name": "~1.1.4" } }, "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ=="],
499507

500508
"color-name": ["color-name@1.1.4", "", {}, "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="],

scripts/build-binaries.mjs

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env bun
2-
import { mkdirSync, rmSync, cpSync, readFileSync, writeFileSync } from 'node:fs';
2+
import { mkdirSync, rmSync, cpSync, readFileSync, writeFileSync, readdirSync } from 'node:fs';
33
import { join, dirname, resolve } from 'node:path';
44
import { fileURLToPath } from 'node:url';
55
import { platform, arch } from 'node:os';
6-
import { generateEmbeddedResources } from './generate-embedded-resources.mjs';
76
import { $ } from 'bun';
87

98
// Simple ANSI colors
@@ -43,8 +42,50 @@ console.log(`\n${bold}${cyan}╭────────────────
4342
console.log(`${bold}${cyan}${reset} Building ${bold}CodeMachine${reset} v${mainVersion} ${bold}${cyan}${reset}`);
4443
console.log(`${bold}${cyan}╰────────────────────────────────────────╯${reset}\n`);
4544

46-
await generateEmbeddedResources({ quiet: true, writeJson: true, writeCache: true });
47-
console.log(`${green}${reset} ${dim}Embedded resources refreshed${reset}`);
45+
// Collect resource files for embedding and generate a manifest that imports them.
46+
function collectFiles(dir) {
47+
const results = [];
48+
const stack = [dir];
49+
50+
while (stack.length > 0) {
51+
const current = stack.pop();
52+
const absPath = join(repoRoot, current);
53+
const entries = readdirSync(absPath, { withFileTypes: true });
54+
55+
for (const entry of entries) {
56+
const relPath = join(current, entry.name);
57+
const absEntry = join(repoRoot, relPath);
58+
59+
if (entry.isDirectory()) {
60+
stack.push(relPath);
61+
} else if (entry.isFile()) {
62+
results.push(absEntry);
63+
}
64+
}
65+
}
66+
67+
return results;
68+
}
69+
70+
const resourceFiles = [
71+
...collectFiles('config'),
72+
...collectFiles('prompts'),
73+
...collectFiles('templates'),
74+
join(repoRoot, 'package.json'),
75+
];
76+
77+
// Generate simple imports file (no exports/mapping needed with --asset-naming)
78+
const imports = resourceFiles.map((abs) => {
79+
// Manifest is at src/shared/runtime/, so need ../../../ to reach repo root
80+
const rel = '../../../' + abs.replace(repoRoot + '/', '');
81+
return `import "${rel}" with { type: "file" };`;
82+
});
83+
84+
const manifestContent = `// Auto-generated: tells Bun which files to embed\n${imports.join('\n')}\n`;
85+
const manifestPath = join(repoRoot, 'src', 'shared', 'runtime', 'resource-manifest.ts');
86+
writeFileSync(manifestPath, manifestContent);
87+
88+
console.log(`${green}${reset} ${dim}Collected ${resourceFiles.length} resource files for embedding${reset}`);
4889

4990
// Auto-sync platform package versions before building
5091
if (mainPackage.optionalDependencies) {
@@ -170,18 +211,25 @@ try {
170211
define: {
171212
__CODEMACHINE_VERSION__: JSON.stringify(mainVersion),
172213
},
214+
naming: {
215+
asset: '[dir]/[name].[ext]', // Preserve original filenames (no hashing)
216+
},
173217
compile: {
174218
target: target,
175219
outfile: binaryPath,
176220
},
177-
entrypoints: ['./src/runtime/index.ts'],
221+
entrypoints: ['./src/runtime/index.ts', manifestPath],
178222
});
179223

180224
if (!result.success) {
181225
console.error(` ${red}${reset} Main TUI build failed:`);
226+
console.error(` Logs count: ${result.logs.length}`);
182227
for (const log of result.logs) {
183228
console.error(` ${dim}${log}${reset}`);
184229
}
230+
if (result.logs.length === 0) {
231+
console.error(` ${dim}No build logs available. Result:${reset}`, result);
232+
}
185233
process.exit(1);
186234
}
187235

@@ -198,11 +246,14 @@ try {
198246
define: {
199247
__CODEMACHINE_VERSION__: JSON.stringify(mainVersion),
200248
},
249+
naming: {
250+
asset: '[dir]/[name].[ext]', // Preserve original filenames (no hashing)
251+
},
201252
compile: {
202253
target: target,
203254
outfile: workflowBinaryPath,
204255
},
205-
entrypoints: ['./src/workflows/runner-process.ts'],
256+
entrypoints: ['./src/workflows/runner-process.ts', manifestPath],
206257
});
207258

208259
if (!workflowResult.success) {
@@ -252,9 +303,12 @@ try {
252303
console.log(`\n${green}${reset} ${bold}Build complete for ${targets.length} target(s)${reset}\n`);
253304
} catch (error) {
254305
console.error(`\n${red}${reset} ${bold}Build failed${reset}`);
255-
console.error(`${dim}${error.message}${reset}`);
306+
console.error(`${dim}Error type: ${error?.constructor?.name}${reset}`);
307+
console.error(`${dim}Error message: ${error?.message || 'No message'}${reset}`);
308+
console.error(`${dim}Full error:${reset}`, error);
256309
if (error.stack) {
257-
console.error(`\n${dim}${error.stack}${reset}`);
310+
console.error(`\n${dim}Stack:${reset}`);
311+
console.error(`${dim}${error.stack}${reset}`);
258312
}
259313
process.exit(1);
260314
}

scripts/generate-embedded-resources.mjs

Lines changed: 0 additions & 164 deletions
This file was deleted.

src/cli/commands/templates.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { hasTemplateChanged, setActiveTemplate } from '../../shared/workflows/in
77
import { bootstrapWorkspace } from '../../runtime/services/workspace/index.js';
88
import { selectFromMenu, type SelectionChoice } from '../utils/selection-menu.js';
99
import { isModuleStep } from '../../workflows/templates/types.js';
10-
import { resolvePackageRoot } from '../../shared/utils/package-json.js';
10+
import { resolvePackageRoot } from '../../shared/runtime/pkg.js';
1111

1212
const packageRoot = resolvePackageRoot(import.meta.url, 'templates command');
1313

src/cli/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
registerStepCommand,
99
registerAgentsCommand,
1010
} from './commands/index.js';
11-
import { resolvePackageJson } from '../shared/utils/package-json.js';
11+
import { resolvePackageJson } from '../shared/runtime/pkg.js';
1212

1313
export async function registerCli(program: Command): Promise<void> {
1414
const packageJsonPath = resolvePackageJson(import.meta.url, 'CLI module');

src/cli/tui/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { UpdateNotifierProvider, useUpdateNotifier } from "@tui/context/update-n
1111
import { Home } from "@tui/routes/home"
1212
import { homedir } from "os"
1313
import { createRequire } from "node:module"
14-
import { resolvePackageJson } from "../../shared/utils/package-json.js"
14+
import { resolvePackageJson } from "../../shared/runtime/pkg.js"
1515
import { initTUILogger, closeTUILogger } from "@tui/utils/tui-logger"
1616

1717
/**

src/cli/tui/context/update-notifier.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createSignal, onMount } from "solid-js"
33
import updateNotifier from "update-notifier"
44
import { createSimpleContext } from "./helper"
55
import { createRequire } from "node:module"
6-
import { resolvePackageJson } from "../../../shared/utils/package-json.js"
6+
import { resolvePackageJson } from "../../../shared/runtime/pkg.js"
77

88
export const { use: useUpdateNotifier, provider: UpdateNotifierProvider } = createSimpleContext({
99
name: "UpdateNotifier",

src/cli/tui/routes/home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { useRenderer } from "@opentui/solid"
1313
import { TextAttributes } from "@opentui/core"
1414
import { createRequire } from "node:module"
1515
import { fileURLToPath } from "node:url"
16-
import { resolvePackageJson } from "../../../shared/utils/package-json.js"
16+
import { resolvePackageJson } from "../../../shared/runtime/pkg.js"
1717
import { onMount } from "solid-js"
1818
import * as path from "node:path"
1919
import type { InitialToast } from "../app"

0 commit comments

Comments
 (0)