153 lines
5.2 KiB
JavaScript
153 lines
5.2 KiB
JavaScript
/**
|
||
* Script to update Storybook story categories according to the new hierarchical structure
|
||
*
|
||
* Usage:
|
||
* 1. Run with Node.js: node update-story-categories.js
|
||
* 2. The script will scan all .stories.ts files and suggest category updates
|
||
* 3. You can review and apply the changes
|
||
*/
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const {execSync} = require('child_process');
|
||
|
||
// Define the mapping from old categories to new categories
|
||
const categoryMapping = {
|
||
// UI Components to composants/boutons
|
||
'UI/Buttons/ExportChatButton': 'composants/boutons/ExportChatButton',
|
||
'UI/Buttons/MainButton': 'composants/boutons/MainButton',
|
||
'UI/Buttons/ToggleButton': 'composants/boutons/ToggleButton',
|
||
'Chatbot/Actions/ExportChatButton': 'composants/boutons/ExportChatButton',
|
||
'Components/Buttons/ExportChatButton': 'composants/boutons/ExportChatButton',
|
||
'Components/MainButton': 'composants/boutons/MainButton',
|
||
'Components/ToggleButton': 'composants/boutons/ToggleButton',
|
||
'Components/FeedbackButton': 'composants/boutons/FeedbackButton',
|
||
|
||
// UI Feedback to composants/feedback
|
||
'UI/Feedback/AlertBox': 'composants/feedback/AlertBox',
|
||
'UI/Feedback/FeedbackButton': 'composants/boutons/FeedbackButton',
|
||
'Components/AlertBox': 'composants/feedback/AlertBox',
|
||
|
||
// Design to fondations
|
||
'Design/Colors/ColorDisplay': 'fondations/couleurs/ColorDisplay',
|
||
'Design/Brand/Logo': 'fondations/brand/Logo',
|
||
'Components/Logo': 'fondations/brand/Logo',
|
||
'Components/ColorDisplay': 'fondations/couleurs/ColorDisplay',
|
||
|
||
// UI Navigation to patterns/breadcrumbs
|
||
'UI/Navigation/Navbar': 'patterns/breadcrumbs/Navbar',
|
||
'Navigation/DsNavbar': 'patterns/breadcrumbs/Navbar',
|
||
|
||
// Chatbot Components to composants
|
||
'Chatbot/Conversation/ConversationItem': 'composants/conversation/ConversationItem',
|
||
'Chatbot/Feedback/WarningBugs': 'composants/feedback/WarningBugs',
|
||
'Chatbot/Messages/MessageBox': 'composants/conversation/MessageBox',
|
||
'Chatbot/Input/PromptInput': 'composants/inputs/PromptInput',
|
||
'Chatbot/Input/NewInput': 'composants/inputs/NewInput',
|
||
'Chatbot/Sources/SourceBlock': 'composants/conversation/SourceBlock',
|
||
'Chatbot/ConversationItem': 'composants/conversation/ConversationItem',
|
||
'Chatbot/WarningBugs': 'composants/feedback/WarningBugs',
|
||
'Components/MessageBox': 'composants/conversation/MessageBox',
|
||
'Components/PromptInput': 'composants/inputs/PromptInput',
|
||
'Components/NewInput': 'composants/inputs/NewInput',
|
||
'Components/SourceBlock': 'composants/conversation/SourceBlock',
|
||
|
||
// App
|
||
'App/Features/Chatbot': 'composants/conversation/Chatbot',
|
||
'App/Chatbot': 'composants/conversation/Chatbot',
|
||
};
|
||
|
||
// Find all story files
|
||
const findStoryFiles = () => {
|
||
try {
|
||
const result = execSync(
|
||
'find /home/poule/encrypted/stockage-syncable/www/development/html/ng-implementation/airwatch/src -name "*.stories.ts"'
|
||
).toString();
|
||
return result.split('\n').filter(Boolean);
|
||
} catch (error) {
|
||
console.error('Error finding story files:', error);
|
||
return [];
|
||
}
|
||
};
|
||
|
||
// Extract current category from a story file
|
||
const extractCurrentCategory = (filePath) => {
|
||
try {
|
||
const content = fs.readFileSync(filePath, 'utf8');
|
||
const match = content.match(/title:\s*['"]([^'"]+)['"]/);
|
||
return match ? match[1] : null;
|
||
} catch (error) {
|
||
console.error(`Error reading file ${filePath}:`, error);
|
||
return null;
|
||
}
|
||
};
|
||
|
||
// Update category in a story file
|
||
const updateCategory = (filePath, oldCategory, newCategory) => {
|
||
try {
|
||
let content = fs.readFileSync(filePath, 'utf8');
|
||
content = content.replace(
|
||
`title: '${oldCategory}'`,
|
||
`title: '${newCategory}'`
|
||
);
|
||
fs.writeFileSync(filePath, content, 'utf8');
|
||
console.log(`✅ Updated ${filePath}: ${oldCategory} -> ${newCategory}`);
|
||
return true;
|
||
} catch (error) {
|
||
console.error(`Error updating file ${filePath}:`, error);
|
||
return false;
|
||
}
|
||
};
|
||
|
||
// Main function
|
||
const main = () => {
|
||
console.log('🔍 Finding story files...');
|
||
const storyFiles = findStoryFiles();
|
||
console.log(`Found ${storyFiles.length} story files.`);
|
||
|
||
let updated = 0;
|
||
let skipped = 0;
|
||
let notMapped = 0;
|
||
|
||
storyFiles.forEach(filePath => {
|
||
const currentCategory = extractCurrentCategory(filePath);
|
||
if (!currentCategory) {
|
||
console.log(`⚠️ Could not extract category from ${filePath}`);
|
||
skipped++;
|
||
return;
|
||
}
|
||
|
||
const newCategory = categoryMapping[currentCategory];
|
||
if (!newCategory) {
|
||
console.log(`ℹ️ No mapping defined for category: ${currentCategory} in ${filePath}`);
|
||
notMapped++;
|
||
return;
|
||
}
|
||
|
||
if (currentCategory === newCategory) {
|
||
console.log(`ℹ️ Category already up to date: ${currentCategory} in ${filePath}`);
|
||
skipped++;
|
||
return;
|
||
}
|
||
|
||
const success = updateCategory(filePath, currentCategory, newCategory);
|
||
if (success) {
|
||
updated++;
|
||
} else {
|
||
skipped++;
|
||
}
|
||
});
|
||
|
||
console.log('\n📊 Summary:');
|
||
console.log(`Total files: ${storyFiles.length}`);
|
||
console.log(`Updated: ${updated}`);
|
||
console.log(`Skipped: ${skipped}`);
|
||
console.log(`Not mapped: ${notMapped}`);
|
||
|
||
if (notMapped > 0) {
|
||
console.log('\n⚠️ Some categories were not mapped. Please update the categoryMapping object in the script.');
|
||
}
|
||
};
|
||
|
||
// Run the script
|
||
main();
|