/
githubmirror
/
30-seconds-of-code
Обзор
Документация
Войти
/
githubmirror
/
30-seconds-of-code
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
bin/prepare
124 строки
4 KB
Angelos Chalaris
Add content component creation utility
19 апр 2025, 11:26
Не верифицирован
19 апр 2025, 11:26
04ae837
Код
Авторство
О чём код?
#!/usr/bin/env node import { exec } from 'child_process'; import ContentUtils from '#src/lib/contentUtils/contentUtils.js'; import Loader from '#src/lib/loader.js'; const args = process.argv.slice(2); if (args.length === 0 || args[0] === 'help') { console.log('Usage: prepare [command] [options]\n'); console.log('Commands:'); console.log( ' full - Prepare all assets, content, and generate all files' ); console.log( ' dev - Prepare content and generate Astro content and search index' ); console.log(' content - Prepare content'); console.log(' watch - Watch content and prepare content on change'); console.log(' assets - Prepare assets'); console.log(' --force, -f - Force assets preparation'); console.log(' create [type] [directory] [name] - Create new content'); console.log( ' [type] - Type of content to create (snippet or collection)' ); console.log(' [directory] - Directory to create content in'); console.log(' [name] - Name of content to create'); console.log(' create-component [name] - Create new component'); console.log( ' [name] - Name of component to create (e.g. my-component)' ); console.log(' help - Show this help'); } else if (args[0] === 'full') { // 0. Run ContentUtils to prepare the assets await ContentUtils.prepareAssets({ force: false }); // 1. Run ContentUtils to prepare the .content/content.json file // Note: fastHighlight is set to false, as we want to use the full Shiki // highlighter for the full build. await ContentUtils.prepareContent({ fastHighlight: false }); // 2. Load modules const modules = Loader.loadModules(); const { AstroContent, SearchIndex, Redirects, Feed, Sitemap, TimestampDump, ContentComponents, } = modules; // 3. Import the content const data = Loader.importData(); // 4. Generate Astro content AstroContent.generate(); // 5. Generate search index SearchIndex.generate(); // 6. Generate redirects Redirects.generate(); // 7. Generate feed Feed.generate(); // 8. Generate sitemap Sitemap.generate(); // 9. Generate timestamp dump TimestampDump.generate(); // 10. Copy scripts ContentComponents.prepare(); } else if (args[0] === 'dev') { // 0. Set NODE_ENV to development process.env.NODE_ENV = 'development'; // Read the --fast-highlight flag from the command line arguments. // Note: The --fast-highlight flag is used to speed up the preparation // of the content by using the Prism highlighter instead of Shiki. // This is useful for development, as it speeds up the preparation // of the content significantly and allows for hot reloading. const fastHighlight = args[1] === '--fast-highlight'; // 1. Run ContentUtils to prepare the .content/content.json file await ContentUtils.prepareContent({ fastHighlight }); // 2. Load modules const modules = Loader.loadModules(); const { AstroContent, SearchIndex, TimestampDump, ContentComponents } = modules; // 3. Import the content const data = Loader.importData(); // 4. Generate Astro content AstroContent.generate(); // 5. Generate search index SearchIndex.generate(); // 6. Generate timestamp dump TimestampDump.generate(); // 7. Copy scripts ContentComponents.prepare(); } else if (args[0] === 'watch') { // Use the --fast-highlight flag to determine if we should use the fast // highlighter or not. This is useful if you want to run watch mode // with the fast higlighter disabled. const fastHighlight = args[1] === '--fast-highlight'; if (!fastHighlight) console.log('Watch mode started with fast highlighting disabled.'); const command = `bin/prepare dev${fastHighlight ? ' --fast-highlight' : ''}`; ContentUtils.watchContent(() => exec(command)); } else if (args[0] === 'content') { ContentUtils.prepareContent(); } else if (args[0] === 'assets') { const force = args[1] === '--force' || args[1] === '-f'; ContentUtils.prepareAssets({ force }); } else if (args[0] === 'create') { if (process.argv.length < 4) { console.log('Usage: prepare create [type] [directory] [name]'); console.error('Not enough arguments'); process.exit(1); } ContentUtils.createContent(args[1], args[2], args[3]); } else if (args[0] === 'create-component') { if (process.argv.length < 3) { console.log('Usage: prepare create-component [name]'); console.error('Not enough arguments'); process.exit(1); } ContentUtils.createComponent(args[1]); } else { console.log('Invalid command'); process.exit(1); }