#!/usr/bin/env node /** * Generates `@vaadin/vaadin-core` (Apache-2.0 packages) and `@vaadin/vaadin` * (commercial packages + vaadin-core): the `version` and `dependencies` of * their `package.json`, and the imports of their entry point. * * Usage: yarn release:cem && yarn release:meta [--check] */ import fs from 'node:fs'; import path from 'node:path'; import lerna from '../lerna.json' with { type: 'json' }; import { byName, isElement, listPackages, packageDir, readManifest, readPackageJson, rootDir, } from '../manifest/lib/cem-helpers.js'; const { version } = lerna; const META_PACKAGES = [ { dir: 'vaadin-core', name: '@vaadin/vaadin-core', entryPoint: 'vaadin-core.js', core: true }, { dir: 'vaadin', name: '@vaadin/vaadin', entryPoint: 'vaadin.js', core: false }, ]; // Register an iconset, not a custom element, so the manifest does not list them const ICONSET_PACKAGES = new Set(['icons']); // Not built here: their version is kept from the committed package.json const EXTERNAL_DEPENDENCIES = new Set([ '@vaadin/router', '@vaadin/vaadin-development-mode-detector', '@vaadin/vaadin-usage-statistics', ]); // React wrappers are published on their own const skippedDirs = new Set([ ...META_PACKAGES.map((metaPackage) => metaPackage.dir), 'react-components', 'react-components-pro', ]); /** * @return {{ core: object[], commercial: object[] }} */ function collectPackages() { const core = []; const commercial = []; for (const dir of listPackages().filter((dir) => !skippedDirs.has(dir))) { const packageJson = readPackageJson(dir); const group = packageJson.license === 'Apache-2.0' ? core : commercial; group.push({ dir, ...packageJson }); } return { core, commercial }; } /** * @param {object} packageJson * @return {string[]} e.g. ["@vaadin/login", "@vaadin/login/vaadin-login-form.js"] */ function collectImports(packageJson) { if (ICONSET_PACKAGES.has(packageJson.dir)) { return [packageJson.name]; } // Theme packages have no manifest const manifest = readManifest(packageJson.dir); if (!manifest) { return []; } // Root level entry points that define a custom element, not mixin-only ones const { modules } = manifest; const definesElement = ({ declaration }) => modules.some((module) => module.path === declaration.module && module.declarations.some(isElement)); const entryPoints = modules .filter((module) => !module.path.includes('/') && module.exports.some(definesElement)) .map((module) => module.path) .sort(byName); // Main entry point first, imported by package name const main = entryPoints.indexOf(packageJson.main); if (main > 0) { entryPoints.unshift(entryPoints.splice(main, 1)[0]); } return entryPoints.map((file) => (file === packageJson.main ? packageJson.name : `${packageJson.name}/${file}`)); } /** * @param {string[]} imports * @return {string} */ function createEntryPoint(imports) { return [ '// This file is generated by scripts/generateMetaPackages.js. Do not edit it by hand.', ...imports.map((module) => `import '${module}';`), '', ].join('\n'); } /** * @param {object} packageJson the committed `package.json` * @param {string[]} dependencies workspace packages, pinned to the repository version * @return {string} */ function createPackageJson(packageJson, dependencies) { const external = Object.entries(packageJson.dependencies ?? {}).filter(([name]) => EXTERNAL_DEPENDENCIES.has(name)); const pinned = dependencies.map((name) => [name, version]); const generated = { ...packageJson, version, dependencies: Object.fromEntries([...external, ...pinned].sort(([a], [b]) => byName(a, b))), }; return `${JSON.stringify(generated, null, 2)}\n`; } const check = process.argv.includes('--check'); if (!fs.existsSync(path.join(rootDir, 'custom-elements.json'))) { console.error('The custom elements manifests are missing. Run `yarn release:cem` first.'); process.exit(1); } const { core, commercial } = collectPackages(); const outdated = []; for (const metaPackage of META_PACKAGES) { const packageJson = readPackageJson(metaPackage.dir); const packages = metaPackage.core ? core : commercial; const dependencies = packages.map((pkg) => pkg.name); const imports = packages.flatMap((pkg) => collectImports(pkg)); if (!metaPackage.core) { dependencies.unshift('@vaadin/vaadin-core'); imports.unshift('@vaadin/vaadin-core'); } const files = { 'package.json': createPackageJson(packageJson, dependencies), [metaPackage.entryPoint]: createEntryPoint(imports), }; for (const [name, content] of Object.entries(files)) { const file = path.join(packageDir(metaPackage.dir), name); if (check) { if (!fs.existsSync(file) || fs.readFileSync(file, 'utf-8') !== content) { outdated.push(path.relative(rootDir, file)); } } else { fs.writeFileSync(file, content); console.log(`Generated ${path.relative(rootDir, file)}`); } } if (!check) { console.log(`${metaPackage.name}: ${dependencies.length} packages, ${imports.length} imports`); } } if (outdated.length > 0) { console.error( [ `The meta packages are out of date: ${outdated.join(', ')}`, 'Run `yarn release:meta` and commit the result.', ].join('\n'), ); process.exit(1); }