|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/*! |
| 4 | + * Copyright 2025 Adobe. All rights reserved. |
| 5 | + * |
| 6 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 8 | + * of the License at <http://www.apache.org/licenses/LICENSE-2.0> |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 11 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 12 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 13 | + * governing permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +/* eslint-disable no-console */ |
| 17 | + |
| 18 | +/** |
| 19 | + * This script scans the components directory and generates a list of components |
| 20 | + * with a migrated status. The output can be saved to a JSON file or printed to console. |
| 21 | + * |
| 22 | + * Usage: |
| 23 | + * node tasks/migrated-component-scanner.js [--output=path/to/output.json] |
| 24 | + */ |
| 25 | + |
| 26 | +const fs = require("fs"); |
| 27 | +const path = require("path"); |
| 28 | + |
| 29 | +/** |
| 30 | + * Gets all component directory names from the components folder |
| 31 | + * @returns {string[]} Array of component directory names |
| 32 | + */ |
| 33 | +function getAllComponentDirectories() { |
| 34 | + try { |
| 35 | + // Get the absolute path to the components directory |
| 36 | + const componentsDir = path.resolve(process.cwd(), "components"); |
| 37 | + |
| 38 | + // Read all directories in the components folder |
| 39 | + const directories = fs.readdirSync(componentsDir, { withFileTypes: true }) |
| 40 | + .filter(dirent => dirent.isDirectory()) |
| 41 | + .map(dirent => dirent.name) |
| 42 | + .sort(); |
| 43 | + |
| 44 | + return directories; |
| 45 | + } catch (error) { |
| 46 | + console.error("Error getting component directories:", error); |
| 47 | + return []; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Gets all component directories that have a specific status |
| 53 | + * @param {string} statusType Status type to filter by (e.g., 'migrated') |
| 54 | + * @returns {string[]} Array of matching component directory names |
| 55 | + */ |
| 56 | +function getComponentsByStatus(statusType) { |
| 57 | + try { |
| 58 | + const componentsDir = path.resolve(process.cwd(), "components"); |
| 59 | + const directories = getAllComponentDirectories(); |
| 60 | + |
| 61 | + if (!statusType) return directories; |
| 62 | + |
| 63 | + // Filter directories that have status type in their stories |
| 64 | + const matchingComponents = directories.filter(dir => { |
| 65 | + const storiesDir = path.join(componentsDir, dir, "stories"); |
| 66 | + |
| 67 | + // Check if stories directory exists |
| 68 | + if (!fs.existsSync(storiesDir)) return false; |
| 69 | + |
| 70 | + // Get all story files |
| 71 | + const storyFiles = fs.readdirSync(storiesDir) |
| 72 | + .filter(file => file.endsWith(".stories.js")); |
| 73 | + |
| 74 | + // Check each story file for status type |
| 75 | + return storyFiles.some(file => { |
| 76 | + const storyContent = fs.readFileSync(path.join(storiesDir, file), "utf8"); |
| 77 | + return storyContent.includes(`type: "${statusType}"`); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + return matchingComponents; |
| 82 | + } catch (error) { |
| 83 | + console.error(`Error getting components with status ${statusType}:`, error); |
| 84 | + return []; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Generates a list of migrated components |
| 90 | + * @returns {Object} Information about migrated components |
| 91 | + */ |
| 92 | +function generateMigratedComponentsReport() { |
| 93 | + const allComponents = getAllComponentDirectories(); |
| 94 | + const migratedComponents = getComponentsByStatus("migrated"); |
| 95 | + |
| 96 | + return { |
| 97 | + total: allComponents.length, |
| 98 | + migrated: migratedComponents.length, |
| 99 | + components: migratedComponents, |
| 100 | + generatedAt: new Date().toISOString() |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +// Export the functions for use in other modules |
| 105 | +module.exports = { |
| 106 | + getAllComponentDirectories, |
| 107 | + getComponentsByStatus, |
| 108 | + generateMigratedComponentsReport |
| 109 | +}; |
| 110 | + |
| 111 | +// Main execution - only runs when script is executed directly in the terminal |
| 112 | +if (require.main === module) { |
| 113 | + (async () => { |
| 114 | + const args = process.argv.slice(2); |
| 115 | + const outputArg = args.find(arg => arg.startsWith("--output=")); |
| 116 | + const outputPath = outputArg ? outputArg.split("=")[1] : null; |
| 117 | + |
| 118 | + console.log("Scanning for migrated components..."); |
| 119 | + const report = generateMigratedComponentsReport(); |
| 120 | + |
| 121 | + if (outputPath) { |
| 122 | + const outputDir = path.dirname(outputPath); |
| 123 | + if (!fs.existsSync(outputDir)) { |
| 124 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 125 | + } |
| 126 | + |
| 127 | + fs.writeFileSync(outputPath, JSON.stringify(report, null, 2)); |
| 128 | + console.log(`Report saved to ${outputPath}`); |
| 129 | + console.log(`Found ${report.migrated} migrated components out of ${report.total} total components.`); |
| 130 | + } else { |
| 131 | + console.log("Migrated Components:"); |
| 132 | + console.log(report.components.join(", ")); |
| 133 | + console.log(`\nTotal: ${report.migrated} out of ${report.total} components are migrated.`); |
| 134 | + } |
| 135 | + })(); |
| 136 | +} |
0 commit comments