Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

2.8 KB
Raw Blame
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const PACKAGE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');

export const REQUIRED_PACK_FILES = [
  'README.md',
  'dist/LICENSE',
  'dist/THIRD_PARTY_NOTICES.md',
  'dist/native/native-config.win32-x64-msvc.node',
  'dist/native/native-config.win32-arm64-msvc.node',
  'dist/assets/skills/discovery/SKILL.md',
  'dist/assets/skills/project/SKILL.md',
  'dist/assets/skills/write-skill/SKILL.md',
  'dist/assets/skills/packs/knowledge-base/SKILL.md',
  'dist/assets/skills/packs/software-lifecycle/write-a-spec/SKILL.md',
];

export const REQUIRED_KEYWORDS = ['knowledge-base', 'markdown', 'local-first', 'mcp', 'ai', 'cli'];

export const FORBIDDEN_PACK_PREFIXES = ['scripts/', 'src/', 'tests/'];

export function validatePackageSurface(packageJson, packEntries) {
  const errors = [];
  const pack = Array.isArray(packEntries) ? packEntries[0] : undefined;
  const packedFiles = new Set(pack?.files?.map((file) => file.path) ?? []);

  if (!pack) errors.push('npm pack returned no package entry');
  for (const file of REQUIRED_PACK_FILES) {
    if (!packedFiles.has(file)) errors.push(`packed artifact is missing ${file}`);
  }
  for (const file of packedFiles) {
    const prefix = FORBIDDEN_PACK_PREFIXES.find((candidate) => file.startsWith(candidate));
    if (prefix) errors.push(`packed artifact must not ship ${file} (dev-only ${prefix} tree)`);
  }

  if (typeof packageJson.description !== 'string' || packageJson.description.trim().length < 40) {
    errors.push('package description must be at least 40 characters');
  }
  if (packageJson.homepage !== 'https://openknowledge.ai') {
    errors.push('package homepage must be https://openknowledge.ai');
  }

  const keywords = new Set(Array.isArray(packageJson.keywords) ? packageJson.keywords : []);
  for (const keyword of REQUIRED_KEYWORDS) {
    if (!keywords.has(keyword)) errors.push(`package keywords are missing ${keyword}`);
  }

  return errors;
}

export function verifyPackageSurface(packageRoot = PACKAGE_ROOT) {
  const packageJson = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf8'));
  const packEntries = JSON.parse(
    execFileSync('npm', ['pack', '--json', '--dry-run', '--ignore-scripts'], {
      cwd: packageRoot,
      encoding: 'utf8',
      maxBuffer: 10 * 1024 * 1024,
    }),
  );
  const errors = validatePackageSurface(packageJson, packEntries);

  if (errors.length > 0) {
    throw new Error(`Package surface verification failed:\n- ${errors.join('\n- ')}`);
  }

  console.log(`Package surface verified (${packEntries[0].files.length} packed files).`);
}

if (process.argv[1] && fileURLToPath(import.meta.url) === resolve(process.argv[1])) {
  verifyPackageSurface();
}