Skip to content

Commit 6de7222

Browse files
fix: inject version into SEA bundle and remove incompatible Node v23 flag
- Modified pullcraft.ts to use VERSION_PLACEHOLDER that gets replaced at bundle time - Updated build.bundle.js to read version from package.json and inject via esbuild define - Removed --experimental-default-type=module flag from build.sea.js (incompatible with Node v23) - Version now works correctly for both npm installs and SEA bundles
1 parent 7b5735b commit 6de7222

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

build/scripts/build.bundle.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const __filename = fileURLToPath(import.meta.url);
66
const __dirname = path.dirname(__filename);
77

88
async function createBundle (options) {
9-
const { entryPoint, outfile } = options;
9+
const { entryPoint, outfile, version } = options;
1010

1111
console.log('Starting bundle creation process...');
1212

@@ -17,9 +17,15 @@ async function createBundle (options) {
1717
platform: 'node',
1818
target: 'node18',
1919
outfile,
20-
format: 'cjs'
20+
format: 'cjs',
21+
define: version ? {
22+
'__VERSION_PLACEHOLDER__': `"${version}"`
23+
} : {}
2124
});
2225
console.log(`Bundle created successfully at ${outfile}`);
26+
if (version) {
27+
console.log(`Version ${version} injected into bundle`);
28+
}
2329
} catch (error) {
2430
console.error('Bundling failed:', error);
2531
process.exit(1);
@@ -52,9 +58,21 @@ function validateArgs (options) {
5258
const cliOptions = parseArgs();
5359
validateArgs(cliOptions);
5460

61+
// Read version from package.json to inject into bundle
62+
let version;
63+
try {
64+
const packageJsonPath = path.join(__dirname, '../..', 'package.json');
65+
const { readFileSync } = await import('fs');
66+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
67+
version = packageJson.version;
68+
} catch (error) {
69+
console.warn('Could not read version from package.json:', error.message);
70+
}
71+
5572
const bundleOptions = {
5673
entryPoint: cliOptions.entryPoint,
57-
outfile: cliOptions.outfile || path.join(__dirname, '..', 'bundle.js')
74+
outfile: cliOptions.outfile || path.join(__dirname, '..', 'bundle.js'),
75+
version
5876
};
5977

6078
createBundle(bundleOptions).catch(console.error);

build/scripts/build.sea.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async function createSEA (options) {
6060
console.log('Permissions set');
6161

6262
// Step 3: Generate the blob
63-
const generateBlobCommand = `node --experimental-sea-config ${seaConfigPath} --experimental-default-type=module`;
63+
const generateBlobCommand = `node --experimental-sea-config ${seaConfigPath}`;
6464
console.log(`Generating blob: ${generateBlobCommand}`);
6565
execSync(generateBlobCommand, { stdio: 'inherit' });
6666

src/bin/pullcraft.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ import { fileURLToPath } from 'url';
88
import { dirname, join } from 'path';
99

1010
// Read version from package.json
11-
// This bin file is only used from the ESM build (see package.json "bin" field)
12-
// When compiled, this file is at dist/esm/bin/pullcraft.js
13-
// package.json is at the root, so we go up 3 levels
14-
const __filename = fileURLToPath(import.meta.url);
15-
const __dirname = dirname(__filename);
16-
const packageJsonPath = join(__dirname, '../../../package.json');
17-
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
18-
const VERSION = packageJson.version;
11+
// For npm installs: read from package.json at runtime
12+
// For SEA bundles: VERSION_PLACEHOLDER is replaced at build time by esbuild
13+
let VERSION = '__VERSION_PLACEHOLDER__';
14+
15+
// If running from npm install (not bundled), read version from package.json
16+
if (VERSION === '__VERSION_PLACEHOLDER__') {
17+
try {
18+
if (import.meta.url) {
19+
const __filename = fileURLToPath(import.meta.url);
20+
const __dirname = dirname(__filename);
21+
const packageJsonPath = join(__dirname, '../../../package.json');
22+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
23+
VERSION = packageJson.version;
24+
}
25+
} catch (error) {
26+
VERSION = '0.0.0-dev';
27+
}
28+
}
1929

2030
// Load environment variables from a .env file if it exists
2131
dotenv.config();

0 commit comments

Comments
 (0)