#!/usr/bin/env node /** * Copyright 2026 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const root = path.resolve(__dirname, '..'); const CURRENT_YEAR = new Date().getFullYear(); const YEAR_PLACEHOLDER = `<%= YEAR %>`; const args = new Set(process.argv.slice(2)); const targetGeneration = args.has('--gen=gen2') ? 'gen2' : args.has('--gen=1st-gen') ? '1st-gen' : 'all'; const runCheck = args.has('--check'); // Read copyright header from linters/HEADER.js const COPYRIGHT_HEADER = fs .readFileSync(path.join(root, 'linters/HEADER.js'), 'utf-8') .trim() .replace(YEAR_PLACEHOLDER, CURRENT_YEAR); function getGeneratedContent(packageJsonPath, generationName, coreVersion) { const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); const version = pkg.version; const coreVersionBlock = generationName === '1st-gen' ? '' : ` /** * The version of the core base package. */ export const coreVersion = '${coreVersion || version}';`; return `${COPYRIGHT_HEADER} // Auto-generated from ${path.relative(root, packageJsonPath)} // Generation: ${generationName} // DO NOT EDIT - This file is generated by scripts/generate-versions.js /** * The version of the ${generationName} Spectrum Web Components library. */ export const version = '${version}';${coreVersionBlock} `; } /** * Generate a version TypeScript file from a package.json * * @param {string} packageJsonPath - Path to package.json * @param {string} outputPath - Path to output version.ts file * @param {object} options - Generation options * @param {string} options.generationName - Name of the generation (e.g., '1st-gen', 'gen2') * @param {string} [options.coreVersion] - Optional core version to include */ function generateVersion(packageJsonPath, outputPath, options = {}) { const { generationName, coreVersion } = options; try { const content = getGeneratedContent( packageJsonPath, generationName, coreVersion ); if (runCheck) { if (!fs.existsSync(outputPath)) { console.error( `✗ Missing generated file: ${path.relative(root, outputPath)}` ); process.exit(1); } const existingContent = fs.readFileSync(outputPath, 'utf-8'); if (existingContent !== content) { console.error( `✗ Generated file is out of date: ${path.relative(root, outputPath)}` ); console.error( `Run: node scripts/generate-versions.js${ generationName === 'gen2' ? ' --gen=gen2' : '' }${generationName === '1st-gen' ? ' --gen=1st-gen' : ''}` ); process.exit(1); } console.log(`✓ Checked ${path.relative(root, outputPath)} is up to date`); return; } fs.writeFileSync(outputPath, content); console.log( `✓ Generated ${path.relative(root, outputPath)} with version ${JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')).version}` ); } catch (error) { console.error( `✗ Failed to generate ${path.relative(root, outputPath)}:`, error.message ); process.exit(1); } } // Generate gen2 version first (this is the core) const gen2PkgPath = path.join(root, 'gen2/packages/core/package.json'); const gen2OutputPath = path.join(root, 'gen2/packages/core/element/version.ts'); if (targetGeneration === 'all' || targetGeneration === 'gen2') { generateVersion(gen2PkgPath, gen2OutputPath, { generationName: 'gen2', }); } // Generate 1st-gen version. coreVersion defaults to 1st-gen's own package // version, since @spectrum-web-components/base is 1st-gen's own core base // package and has no relationship to gen2's version. const firstGenPkgPath = path.join(root, '1st-gen/tools/base/package.json'); const firstGenOutputPath = path.join(root, '1st-gen/tools/base/src/version.ts'); if (targetGeneration === 'all' || targetGeneration === '1st-gen') { generateVersion(firstGenPkgPath, firstGenOutputPath, { generationName: '1st-gen', }); } if (!runCheck) { console.log('\n✓ All version files generated successfully'); }