From 63598292984e8b33c9ee489ab2a24d6e963b0979 Mon Sep 17 00:00:00 2001 From: coolzr Date: Fri, 24 Oct 2025 01:26:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8E=BB=E9=99=A4=20migrate-config.js?= =?UTF-8?q?=20=E4=B9=85=E9=85=8D=E7=BD=AE=E8=BF=81=E7=A7=BB=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - src/migrate-config.js | 161 ------------------------------------------ 2 files changed, 162 deletions(-) delete mode 100644 src/migrate-config.js diff --git a/package.json b/package.json index 61528f7..9f5a248 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "clean": "node ./scripts/clean.js", "build": "npm run clean && npm run generate", "restructure": "node restructure.js", - "migrate-config": "node src/migrate-config.js", "import-bookmarks": "node src/bookmark-processor.js" }, "keywords": [ diff --git a/src/migrate-config.js b/src/migrate-config.js deleted file mode 100644 index c6fc89a..0000000 --- a/src/migrate-config.js +++ /dev/null @@ -1,161 +0,0 @@ -/** - * MeNav 配置迁移工具 - * 将旧版双文件配置转换为新版模块化配置 - */ - -const fs = require('fs'); -const path = require('path'); -const yaml = require('js-yaml'); - -// 配置文件路径 -const LEGACY_CONFIG_FILE = 'config.yml'; -const LEGACY_USER_CONFIG_FILE = 'config.user.yml'; -const LEGACY_BOOKMARKS_FILE = 'bookmarks.yml'; -const LEGACY_USER_BOOKMARKS_FILE = 'bookmarks.user.yml'; - -// 模块化配置目录 -const CONFIG_USER_DIR = 'config/user'; -const CONFIG_USER_PAGES_DIR = path.join(CONFIG_USER_DIR, 'pages'); - -/** - * 迁移旧式配置文件到模块化格式 - */ -function migrateConfiguration() { - console.log('MeNav 配置迁移工具'); - - // 检查是否存在旧式配置文件 - const hasUserConfig = fs.existsSync(LEGACY_USER_CONFIG_FILE); - const hasDefaultConfig = fs.existsSync(LEGACY_CONFIG_FILE); - const hasUserBookmarks = fs.existsSync(LEGACY_USER_BOOKMARKS_FILE); - const hasDefaultBookmarks = fs.existsSync(LEGACY_BOOKMARKS_FILE); - - if (!hasUserConfig && !hasDefaultConfig && !hasUserBookmarks && !hasDefaultBookmarks) { - console.log('未找到任何旧式配置文件,无需迁移。'); - return; - } - - // 确保目标目录存在 - if (!fs.existsSync(CONFIG_USER_DIR)) { - fs.mkdirSync(CONFIG_USER_DIR, { recursive: true }); - } - - if (!fs.existsSync(CONFIG_USER_PAGES_DIR)) { - fs.mkdirSync(CONFIG_USER_PAGES_DIR, { recursive: true }); - } - - // 优先使用用户配置,如果不存在则使用默认配置 - const configFile = hasUserConfig ? LEGACY_USER_CONFIG_FILE : hasDefaultConfig ? LEGACY_CONFIG_FILE : null; - - // 迁移主配置文件 - if (configFile) { - try { - const configContent = fs.readFileSync(configFile, 'utf8'); - const config = yaml.load(configContent); - - // 提取站点配置 - if (config.site) { - const siteConfig = { - title: config.site.title || '', - description: config.site.description || '', - author: config.site.author || '', - favicon: config.site.favicon || 'favicon.ico' - }; - - // 添加字体配置 - if (config.fonts) { - siteConfig.fonts = config.fonts; - } - - // 添加个人资料配置 - if (config.profile) { - siteConfig.profile = config.profile; - } - - // 添加社交媒体配置 - if (config.social && config.social.length > 0) { - siteConfig.social = config.social; - } - - // 写入站点配置 - const siteYaml = yaml.dump(siteConfig, { indent: 2, lineWidth: -1, quotingType: '"' }); - fs.writeFileSync( - path.join(CONFIG_USER_DIR, 'site.yml'), - `# 由migrate-config.js从${configFile}迁移\n# 生成于 ${new Date().toISOString()}\n\n${siteYaml}`, - 'utf8' - ); - } - - // 提取导航配置 - if (config.navigation && config.navigation.length > 0) { - const navigationYaml = yaml.dump(config.navigation, { indent: 2, lineWidth: -1, quotingType: '"' }); - fs.writeFileSync( - path.join(CONFIG_USER_DIR, 'navigation.yml'), - `# 由migrate-config.js从${configFile}迁移\n# 生成于 ${new Date().toISOString()}\n\n${navigationYaml}`, - 'utf8' - ); - } - - // 提取所有页面配置 - const pageIds = config.navigation ? config.navigation.map(nav => nav.id) : []; - pageIds.forEach(pageId => { - if (config[pageId]) { - const pageConfig = { ...config[pageId] }; - - // 特殊处理首页的categories - if (pageId === 'home' && !pageConfig.categories && config.categories) { - pageConfig.categories = config.categories; - } - - const pageYaml = yaml.dump(pageConfig, { indent: 2, lineWidth: -1, quotingType: '"' }); - fs.writeFileSync( - path.join(CONFIG_USER_PAGES_DIR, `${pageId}.yml`), - `# 由migrate-config.js从${configFile}迁移\n# 生成于 ${new Date().toISOString()}\n\n${pageYaml}`, - 'utf8' - ); - } - }); - - // 如果首页不在pageIds中但存在categories,创建一个home.yml - if (!pageIds.includes('home') && config.categories) { - const homeConfig = { - title: '我的导航', - subtitle: '个人收藏网址', - categories: config.categories - }; - - const homeYaml = yaml.dump(homeConfig, { indent: 2, lineWidth: -1, quotingType: '"' }); - fs.writeFileSync( - path.join(CONFIG_USER_PAGES_DIR, 'home.yml'), - `# 由migrate-config.js从${configFile}迁移\n# 生成于 ${new Date().toISOString()}\n\n${homeYaml}`, - 'utf8' - ); - } - } catch (error) { - console.error(`迁移配置文件${configFile}时出错:`, error); - } - } - - // 迁移书签配置文件 - const bookmarksFile = hasUserBookmarks ? LEGACY_USER_BOOKMARKS_FILE : hasDefaultBookmarks ? LEGACY_BOOKMARKS_FILE : null; - - if (bookmarksFile) { - try { - // 直接复制书签配置文件 - fs.copyFileSync( - bookmarksFile, - path.join(CONFIG_USER_PAGES_DIR, 'bookmarks.yml') - ); - } catch (error) { - console.error(`迁移书签配置文件${bookmarksFile}时出错:`, error); - } - } - - console.log('迁移完成!'); -} - -// 如果直接运行该脚本,则执行迁移 -if (require.main === module) { - migrateConfiguration(); -} - -module.exports = { migrateConfiguration }; \ No newline at end of file