diff --git a/.github/workflows/createConfig.yml b/.github/workflows/createConfig.yml index 02e67d81..1402fdd9 100644 --- a/.github/workflows/createConfig.yml +++ b/.github/workflows/createConfig.yml @@ -23,7 +23,7 @@ jobs: - name: create working-directory: ./ run: | - sudo node ./create.js + sudo node ./app.js env: NODE_ENV: production #部署到github diff --git a/app.js b/app.js index 8f75799d..7250ed12 100644 --- a/app.js +++ b/app.js @@ -1,39 +1,112 @@ -const os = require('os'); -const app = require('express')(); - -// 获取当前主机的网络接口信息 -const networkInterfaces = os.networkInterfaces(); - -// 过滤出 IPv4 地址并排除 localhost -const ipv4Addresses = Object.values(networkInterfaces) - .flat() - .filter((info) => info.family === 'IPv4' && info.internal === false); - -const port = 80; - -app.use((req, res, next) => { - res.header('Access-Control-Allow-Headers', '*'); - res.header('Access-Control-Allow-Origin', '*'); - if (req.method === 'OPTIONS') { - res.sendStatus(200); - } else { - next(); +const fs = require('fs'); +const path = require('path'); + +function excludeDifferentExtensions(data) { + for (const key in data) { + if (key === '_files') { + const filenames = new Set(); + data[key] = data[key].filter((file) => { + const filename = file.name; + if (filenames.has(filename)) { + return false; // 如果已经存在相同文件名,则排除该文件 + } else { + filenames.add(filename); + return true; + } + }); + } else if (typeof data[key] === 'object') { + data[key] = excludeDifferentExtensions(data[key]); // 递归处理嵌套结构 + } + } + + return data; // 返回处理后的数据 +} + +const noCreateFileName = [ + '16w', + '20w', + '32w', + '40w', + '48w', + '60w', + '64w', + '72w', + '80w', + '96w', + '256w', + '16', + '20', + '32', + '40', + '48', + '60', + '64', + '72', + '80', + '96', + '256', +]; +function containsKeyword(fileName) { + for (let i = 0; i < noCreateFileName.length; i++) { + if (fileName.includes(noCreateFileName[i])) { + return true; + } } -}); - -app.get('/', (req, res) => { - res.send('hello world'); -}); - -// 获取第一个可用的 IPv4 地址和端口号 -if (ipv4Addresses.length > 0) { - const { address } = ipv4Addresses[0]; - const port = process.env.PORT || 1314; // 或者您使用的端口号 - - const currentAddress = `http://${address}:${port}`; - app.listen(port, () => { - console.log('当前运行地址:', currentAddress); - }); -} else { - console.log('无法获取当前运行地址'); + return false; } + +// 函数用于遍历目录下的所有文件 +function traverseDirectory(dir, rootDir) { + const companies = {}; + + // 获取目录下的所有文件和文件夹 + const items = fs.readdirSync(dir); + + for (const item of items) { + if (item === '.git' || item === 'web' || item.endsWith('.md')) { + // 如果是 .git 或者 web 目录,则跳过 + continue; + } + + const itemPath = path.join(dir, item); + const relativePath = path.relative(rootDir, itemPath); + const stats = fs.statSync(itemPath); + + if (stats.isDirectory()) { + // 如果是文件夹,则递归遍历 + const software = traverseDirectory(itemPath, rootDir); + companies[item] = software; + } else { + if (dir !== rootDir) { + // 如果是根目录下的单个文件,则跳过 + if (!companies['_files']) { + companies['_files'] = []; + } + // Au16w Au20w Au32w + if (!containsKeyword(item.split('.')[0])) + companies['_files'].push({ + path: relativePath, + name: item.split('.')[0], + type: item.split('.')[1], + }); + } + } + } + + return companies; +} + +// 遍历当前目录 +const rootDir = __dirname; +let result = traverseDirectory(rootDir, rootDir); +result = excludeDifferentExtensions(result); +// 将结果写入config.json文件 +const outputDir = __dirname; +if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir); +} + +const configPath = path.join(outputDir, 'config.json'); +fs.writeFileSync(configPath, JSON.stringify(result, null, 2)); + +console.log('config.json 文件已生成'); diff --git a/create.js b/create.js deleted file mode 100644 index 34e52073..00000000 --- a/create.js +++ /dev/null @@ -1,56 +0,0 @@ -const fs = require('fs'); -const path = require('path'); - -// 函数用于遍历目录下的所有文件 -function traverseDirectory(dir, rootDir) { - const companies = {}; - - // 获取目录下的所有文件和文件夹 - const items = fs.readdirSync(dir); - - for (const item of items) { - if (item === '.git' || item === 'web' || item.endsWith('.md')) { - // 如果是 .git 或者 web 目录,则跳过 - continue; - } - - const itemPath = path.join(dir, item); - const relativePath = path.relative(__dirname, itemPath); - const stats = fs.statSync(itemPath); - - if (stats.isDirectory()) { - // 如果是文件夹,则递归遍历 - const software = traverseDirectory(itemPath, rootDir); - companies[item] = software; - } else { - if (dir !== rootDir) { - // 如果是根目录下的单个文件,则跳过 - if (!companies['_files']) { - companies['_files'] = []; - } - companies['_files'].push({ - path: relativePath, - name: item.split('.')[0], - type: item.split('.')[1], - }); - } - } - } - - return companies; -} - -// 遍历当前目录 -const rootDir = path.join(__dirname, 'imgs'); -const result = traverseDirectory(rootDir, rootDir); - -// 将结果写入config.json文件 -const outputDir = __dirname; -if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir); -} - -const configPath = path.join(outputDir, 'config.json'); -fs.writeFileSync(configPath, JSON.stringify(result, null, 2)); - -console.log('config.json 文件已生成');