93 lines
3.7 KiB
JavaScript
93 lines
3.7 KiB
JavaScript
// ... imports
|
|
import { copyFileSync, mkdirSync, existsSync, rmSync, writeFileSync, readFileSync, readdirSync, statSync } from 'fs';
|
|
import { execSync } from 'child_process';
|
|
import { join, dirname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
const rootDir = join(__dirname, '..');
|
|
const serverDir = join(rootDir, 'server');
|
|
const androidAssetsDir = join(rootDir, 'android', 'app', 'src', 'main', 'assets', 'nodejs-v3');
|
|
const distNodejsDir = join(rootDir, 'dist', 'nodejs-v3');
|
|
|
|
function copyServerToAndroid() {
|
|
console.log('Starting Bundle & Copy Process...');
|
|
|
|
try {
|
|
// 1. Ensure dist directory exists
|
|
if (!existsSync(distNodejsDir)) {
|
|
mkdirSync(distNodejsDir, { recursive: true });
|
|
}
|
|
|
|
// 2. Run the bundle script
|
|
console.log('Bundling server with esbuild...');
|
|
execSync('node bundle.js', { cwd: serverDir, stdio: 'inherit' });
|
|
|
|
// 3. Prepare Android assets directory
|
|
console.log(`Preparing Android assets: ${androidAssetsDir}`);
|
|
if (existsSync(androidAssetsDir)) {
|
|
// Clean up previous install to ensure no stale files
|
|
rmSync(androidAssetsDir, { recursive: true, force: true });
|
|
}
|
|
mkdirSync(androidAssetsDir, { recursive: true });
|
|
|
|
// 4. Copy the bundled index.js
|
|
const bundledFile = join(distNodejsDir, 'index.js');
|
|
if (existsSync(bundledFile)) {
|
|
console.log('Copying bundled index.js to Android assets...');
|
|
copyFileSync(bundledFile, join(androidAssetsDir, 'index.js'));
|
|
} else {
|
|
throw new Error('Bundled file not found! Bundling must have failed.');
|
|
}
|
|
|
|
// 5. Copy package.json (still good to have for versioning/metadata)
|
|
// We modify it to point to index.js as main if it doesn't already
|
|
const serverPackageJson = join(serverDir, 'package.json');
|
|
if (existsSync(serverPackageJson)) {
|
|
console.log('Copying package.json to Android assets...');
|
|
// We don't really need to parse/edit it if the bundle is index.js and the plugin defaults to it,
|
|
// but it's cleaner to ensure 'main' is 'index.js'.
|
|
const pkg = JSON.parse(readFileSync(serverPackageJson, 'utf8'));
|
|
pkg.main = 'index.js';
|
|
writeFileSync(join(androidAssetsDir, 'package.json'), JSON.stringify(pkg, null, 2));
|
|
|
|
// Also copy to dist for reference
|
|
writeFileSync(join(distNodejsDir, 'package.json'), JSON.stringify(pkg, null, 2));
|
|
}
|
|
|
|
// 6. Handle builtin_modules (Capacitor NodeJS requirement)
|
|
// The plugin expects a builtin_modules folder in assets, but the source only has .gitkeep
|
|
// which Android's AssetManager ignores. We must create a real file.
|
|
const androidBuiltinModules = join(rootDir, 'android', 'app', 'src', 'main', 'assets', 'builtin_modules');
|
|
|
|
if (!existsSync(dirname(androidBuiltinModules))) {
|
|
mkdirSync(dirname(androidBuiltinModules), { recursive: true });
|
|
}
|
|
|
|
if (existsSync(androidBuiltinModules)) {
|
|
rmSync(androidBuiltinModules, { recursive: true, force: true });
|
|
}
|
|
|
|
// Always create the directory with a proper placeholder file
|
|
// Android's AssetManager ignores .gitkeep and empty directories
|
|
console.log('Creating builtin_modules placeholder...');
|
|
mkdirSync(androidBuiltinModules, { recursive: true });
|
|
writeFileSync(
|
|
join(androidBuiltinModules, 'README.txt'),
|
|
'Placeholder for capacitor-nodejs builtin_modules directory.\nThis file ensures Android AssetManager recognizes this folder.'
|
|
);
|
|
|
|
console.log('✓ Server bundled and copied successfully to android/assets/nodejs-v3');
|
|
console.log(' NOTE: node_modules were explicitely SKIPPED because we are using a bundle.');
|
|
|
|
} catch (error) {
|
|
console.error('✗ Error:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
copyServerToAndroid();
|
|
|
|
|