Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

11.6 KB
Raw Blame
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import arg from 'arg';
import type { ReleaseType } from 'semver';
import semver, { RELEASE_TYPES } from 'semver';
import { generateReadme } from './generate-readme';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

interface PackageJson {
  name: string;
  version: string;
  scripts?: Record<string, string>;
  dependencies?: Record<string, string>;
  devDependencies?: Record<string, string>;
  [key: string]: unknown;
  config?: {
    transformPackageJson?: boolean;
    transformEnvs?: boolean;
    docPath?: string;
    /** Extra entry points published as `<name>/<subpath>` (a `<subpath>.ts` next to index). */
    subpaths?: string[];
  };
}

export interface PackageInfo extends PackageJson {
  nextVersion: string;
  localPath: string;
}

interface PublishConfig {
  registry: string;
  clear: boolean;
  // Stage for a maintainer's 2FA approval (`npm stage approve`) instead of
  // publishing outright; what CI does under a stage-only trusted publisher.
  stage: boolean;
  // Prereleases go under their own dist-tag so `latest` keeps pointing at a
  // stable version.
  distTag?: string;
}

const PRERELEASE_DIST_TAG = 'rc';

const workspacePath = (relativePath: string) =>
  resolve(__dirname, '../../', relativePath);

const savePackageJson = (absPath: string, data: PackageJson) => {
  fs.writeFileSync(absPath, JSON.stringify(data, null, 2), 'utf-8');
  execSync(`bunx biome format ${absPath} --fix`);
};

const exit = (message: string, error?: unknown) => {
  console.error(`\n\n❌ ${message}`);
  if (error) {
    console.error('Error:', error);
  }
  process.exit(1);
};

const checkUncommittedChanges = () => {
  try {
    const uncommittedFiles = execSync('git status --porcelain')
      .toString()
      .trim();
    if (uncommittedFiles) {
      throw new Error('Uncommitted changes detected');
    }
    console.log('✅ No uncommitted changes');
  } catch (error) {
    exit('Uncommitted changes', error);
  }
};

const getNextVersion = (version: string, type: ReleaseType): string => {
  const nextVersion = semver.inc(version, type);
  if (!nextVersion) {
    throw new Error('Invalid version');
  }
  return type.startsWith('pre')
    ? nextVersion.replace(/-.*$/, '-rc')
    : nextVersion;
};

const loadPackages = (
  releaseType: ReleaseType
): Record<string, PackageInfo> => {
  const sdksPath = workspacePath('./packages/sdks');
  const sdks = fs
    .readdirSync(sdksPath, { withFileTypes: true })
    .filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith('.'))
    .map((dirent) => dirent.name);

  return Object.fromEntries(
    sdks.map((sdk) => {
      const pkgPath = join(sdksPath, sdk, 'package.json');
      const pkgJson = JSON.parse(
        fs.readFileSync(pkgPath, 'utf-8')
      ) as PackageJson;
      const version = pkgJson.version.replace(/-local$/, '');
      return [
        pkgJson.name,
        {
          ...pkgJson,
          version,
          nextVersion: getNextVersion(version, releaseType),
          localPath: `./packages/sdks/${sdk}`,
        },
      ];
    })
  );
};

const findDependents = (
  packages: Record<string, PackageInfo>,
  targetName: string
): string[] => {
  const dependents = new Set([targetName]);
  const findDeps = (name: string) => {
    for (const [pkgName, pkg] of Object.entries(packages)) {
      if (pkg.dependencies?.[name] && !dependents.has(pkgName)) {
        dependents.add(pkgName);
        findDeps(pkgName);
      }
    }
  };
  findDeps(targetName);
  return Array.from(dependents);
};

const updatePackageJsonForRelease = (
  packages: Record<string, PackageInfo>,
  name: string,
  dependents: string[]
): void => {
  const { nextVersion, localPath, ...restPkgJson } = packages[name]!;
  let newPkgJson: PackageJson = {
    ...restPkgJson,
    private: false,
    type: 'module',
    version: nextVersion,
    dependencies: Object.fromEntries(
      Object.entries(restPkgJson.dependencies || {}).map(
        ([depName, depVersion]) => [
          depName,
          dependents.includes(depName)
            ? packages[depName]?.nextVersion ||
              depVersion.replace(/-local$/, '').replace(/^workspace:/, '')
            : depVersion.replace(/-local$/, '').replace(/^workspace:/, ''),
        ]
      )
    ),
  };

  if (packages[name]!.config?.transformPackageJson !== false) {
    newPkgJson = {
      ...newPkgJson,
      main: './dist/index.js',
      module: './dist/index.js',
      types: './dist/index.d.ts',
      files: ['dist', 'README.md', 'LICENSE'],
      exports: restPkgJson.exports ?? {
        '.': {
          import: './dist/index.js',
          require: './dist/index.cjs',
          types: './dist/index.d.ts',
        },
        ...Object.fromEntries(
          (packages[name]!.config?.subpaths ?? []).map((subpath) => [
            `./${subpath}`,
            {
              import: `./dist/${subpath}.js`,
              require: `./dist/${subpath}.cjs`,
              types: `./dist/${subpath}.d.ts`,
            },
          ])
        ),
      },
    };
  }

  savePackageJson(workspacePath(`${localPath}/package.json`), newPkgJson);
  packages[name]!.dependencies = newPkgJson.dependencies;
};

const searchAndReplace = (path: string, search: RegExp, replace: string) => {
  const entries = fs.readdirSync(path, { withFileTypes: true });
  for (const entry of entries) {
    if (entry.name === 'node_modules') {
      continue;
    }
    const fullpath = join(path, entry.name);
    if (entry.isDirectory()) {
      searchAndReplace(fullpath, search, replace);
      continue;
    }
    if (!entry.isFile()) {
      continue;
    }
    const content = fs.readFileSync(fullpath, {
      encoding: 'utf-8',
    });

    const match = content.match(search);
    if (match) {
      console.log(`✏️ Will replace ${search} with ${replace} in ${entry.name}`);
      const newContent = content.replaceAll(search, replace);
      fs.writeFileSync(fullpath, newContent, {
        encoding: 'utf-8',
      });
    }
  }
};

const transformPackages = (
  packages: Record<string, PackageInfo>,
  dependents: string[]
): void => {
  for (const dep of dependents) {
    const pkg = packages[dep];
    if (pkg && pkg.config?.transformEnvs === true) {
      const currentVersion = pkg.version;
      const nextVersion = pkg.nextVersion;
      searchAndReplace(
        workspacePath(pkg.localPath),
        new RegExp(`${currentVersion}`, 'g'),
        nextVersion
      );
    }
  }
};

const buildPackages = (
  packages: Record<string, PackageInfo>,
  dependents: string[]
): void => {
  const versionEnvs = dependents.map((dep) => {
    const envName = dep
      .replace(/@openpanel\//g, '')
      .toUpperCase()
      .replace(/[/-]/g, '_');
    return `--env.${envName}_VERSION=${packages[dep]!.nextVersion}`;
  });

  for (const dep of dependents) {
    if (!packages[dep]?.scripts?.build) {
      console.log(`🔨 Skipping build for ${dep}`);
      continue;
    }
    console.log(`🔨 Building ${dep}`);
    const cmd = `bun run build ${versionEnvs.join(' ')}`;
    console.log(`  Running: ${cmd}`);
    execSync(cmd, {
      cwd: workspacePath(packages[dep]!.localPath),
    });
  }
};

const publishPackages = (
  packages: Record<string, PackageInfo>,
  dependents: string[],
  config: PublishConfig
): void => {
  if (config.clear) {
    execSync('rm -rf ~/.local/share/verdaccio/storage/@openpanel');
  }

  const publishCommand = config.stage ? 'npm stage publish' : 'npm publish';
  for (const dep of dependents) {
    console.log(
      `🚀 ${config.stage ? 'Staging' : 'Publishing'} ${dep} to ${config.registry}`
    );
    execSync(
      `${publishCommand} --access=public --registry ${config.registry}`,
      {
        cwd: workspacePath(packages[dep]!.localPath),
      }
    );

    if (dep === '@openpanel/web') {
      execSync(
        `cp ${workspacePath('packages/sdks/web/dist/src/tracker.global.js')} ${workspacePath('./apps/public/public/op1.js')}`
      );
      execSync(
        `cp ${workspacePath('packages/sdks/web/dist/src/replay.global.js')} ${workspacePath('./apps/public/public/op1-replay.js')}`
      );
    }
  }
};

const restoreAndUpdateLocal = (
  packages: Record<string, PackageInfo>,
  dependents: string[],
  generatedReadmes: string[]
): void => {
  const filesToRestore = dependents
    .map((dep) => join(workspacePath(packages[dep]!.localPath), 'package.json'))
    .join(' ');

  execSync(`git checkout ${filesToRestore}`);

  // Clean up auto-generated README files
  for (const readmePath of generatedReadmes) {
    if (fs.existsSync(readmePath)) {
      console.log(`🧹 Removing auto-generated README: ${readmePath}`);
      fs.unlinkSync(readmePath);
    }
  }

  for (const dep of dependents) {
    const { nextVersion, localPath, ...restPkgJson } = packages[dep]!;
    console.log(`🚀 Updating ${dep} (${nextVersion}-local)`);

    const updatedPkgJson: PackageJson = {
      ...restPkgJson,
      version: `${nextVersion}-local`,
      dependencies: Object.fromEntries(
        Object.entries(restPkgJson.dependencies || {}).map(
          ([depName, depVersion]) => [
            depName,
            dependents.includes(depName)
              ? `workspace:${packages[depName]!.nextVersion}-local`
              : packages[depName]
                ? `workspace:${packages[depName]!.version}-local`
                : depVersion,
          ]
        )
      ),
      devDependencies: Object.fromEntries(
        Object.entries(restPkgJson.devDependencies || {}).map(
          ([depName, depVersion]) => [
            depName,
            dependents.includes(depName)
              ? `${packages[depName]!.nextVersion}-local`
              : packages[depName]
                ? `${packages[depName]!.version}-local`
                : depVersion,
          ]
        )
      ),
    };

    savePackageJson(workspacePath(`${localPath}/package.json`), updatedPkgJson);
  }
};

function main() {
  const args = arg({
    '--name': String,
    '--publish': Boolean,
    '--npm': Boolean,
    '--skip-git': Boolean,
    '--type': String,
    '--clear': Boolean,
    '--stage': Boolean,
  });

  if (!args['--skip-git']) {
    checkUncommittedChanges();
  }

  if (!args['--name']) {
    return exit('--name is required');
  }

  if (!RELEASE_TYPES.includes(args['--type'] as ReleaseType)) {
    return exit(
      `Invalid release type. Valid types are: ${RELEASE_TYPES.join(', ')}`
    );
  }

  const originalPackages = loadPackages(args['--type'] as ReleaseType);
  const packages = loadPackages(args['--type'] as ReleaseType);
  const target = packages[args['--name']];

  if (!target) {
    return exit('Selected package does not exist');
  }

  const dependents = findDependents(packages, target.name);

  for (const dep of dependents) {
    console.log(
      `📦 ${dep} · Old Version: ${packages[dep]!.version} · Next Version: ${packages[dep]!.nextVersion}`
    );
    updatePackageJsonForRelease(packages, dep, dependents);
  }

  transformPackages(packages, dependents);

  buildPackages(packages, dependents);

  const generatedReadmes = generateReadme(packages, dependents);

  if (args['--publish']) {
    const config: PublishConfig = {
      registry: args['--npm']
        ? 'https://registry.npmjs.org'
        : 'http://localhost:4873',
      clear: args['--clear'] ?? false,
      stage: args['--stage'] ?? false,
      distTag: args['--type']!.startsWith('pre')
        ? PRERELEASE_DIST_TAG
        : undefined,
    };

    publishPackages(packages, dependents, config);
    restoreAndUpdateLocal(originalPackages, dependents, generatedReadmes);
  }

  console.log('✅ All done!');
}

main();