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.

2.2 KB
Raw Blame
import { execSync } from 'node:child_process';
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

const packageDir = dirname(dirname(fileURLToPath(import.meta.url)));
const packageJson = JSON.parse(readFileSync(join(packageDir, 'package.json'), 'utf8'));

function parseBuildNumber(name, fallback) {
  const value = process.env[name];
  if (value == null || value === '') return fallback;
  if (!/^\d+$/.test(value)) {
    throw new Error(`${name} must be an unsigned integer, got ${JSON.stringify(value)}`);
  }
  const parsed = Number(value);
  if (!Number.isSafeInteger(parsed)) throw new Error(`${name} exceeds safe integer range`);
  return parsed;
}

function gitSha() {
  try {
    return execSync('git rev-parse --short HEAD', {
      cwd: packageDir,
      encoding: 'utf8',
      stdio: ['ignore', 'pipe', 'ignore'],
    }).trim();
  } catch {
    return null;
  }
}

const androidMinimum = parseBuildNumber('MIN_NATIVE_BUILD_ANDROID', undefined);
const iosMinimum = parseBuildNumber('MIN_NATIVE_BUILD_IOS', undefined);
if ((androidMinimum === undefined) !== (iosMinimum === undefined)) {
  throw new Error('Set both MIN_NATIVE_BUILD_ANDROID and MIN_NATIVE_BUILD_IOS');
}
const platformMinima = androidMinimum === undefined ? undefined : {
  android: androidMinimum,
  ios: iosMinimum,
};

const manifest = {
  schemaVersion: platformMinima ? 3 : 2,
  bundleBuild: parseBuildNumber('BUNDLE_BUILD_NUMBER', Date.now()),
  minNativeBuild: parseBuildNumber('MIN_NATIVE_BUILD', 0),
  ...(platformMinima ? { minNativeBuilds: platformMinima } : {}),
  gitSha: gitSha(),
  appVersion: packageJson.version,
};

const outPath = join(packageDir, 'dist', 'bundle-manifest.json');
mkdirSync(dirname(outPath), { recursive: true });
writeFileSync(outPath, `${JSON.stringify(manifest, null, 2)}\n`);

const indexPath = join(packageDir, 'dist', 'index.html');
const bundleBuildPlaceholder = '__MACRO_BUNDLE_BUILD__';
const indexHtml = readFileSync(indexPath, 'utf8');
if (!indexHtml.includes(bundleBuildPlaceholder)) {
  throw new Error(`${indexPath} is missing ${bundleBuildPlaceholder}`);
}
writeFileSync(
  indexPath,
  indexHtml.replaceAll(bundleBuildPlaceholder, String(manifest.bundleBuild))
);