forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the heroku building issue DIYgod#15819
- Loading branch information
1 parent
a71410f
commit f61dfa0
Showing
2 changed files
with
121 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,105 @@ | ||
import { namespaces } from '../../lib/registry'; | ||
import { RadarItem } from '../../lib/types'; | ||
import { parse } from 'tldts'; | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
import toSource from 'tosource'; | ||
|
||
import { getCurrentPath } from '../../lib/utils/helpers'; | ||
|
||
const __dirname = getCurrentPath(import.meta.url); | ||
|
||
const maintainers: Record<string, string[]> = {}; | ||
const radar: { | ||
[domain: string]: { | ||
_name: string; | ||
[subdomain: string]: RadarItem[] | string; | ||
}; | ||
} = {}; | ||
async function ensureDirectoryExists(dirPath: string) { | ||
try { | ||
await fs.mkdir(dirPath, { recursive: true }); | ||
} catch (error) { | ||
console.error(`Error creating directory ${dirPath}:`, error); | ||
} | ||
} | ||
|
||
for (const namespace in namespaces) { | ||
let defaultCategory = namespaces[namespace].categories?.[0]; | ||
if (!defaultCategory) { | ||
for (const path in namespaces[namespace].routes) { | ||
if (namespaces[namespace].routes[path].categories) { | ||
defaultCategory = namespaces[namespace].routes[path].categories[0]; | ||
break; | ||
async function writeJsonFile(filePath: string, data: any) { | ||
try { | ||
await fs.writeFile(filePath, JSON.stringify(data, null, 2)); | ||
console.log(`Successfully wrote ${filePath}`); | ||
} catch (error) { | ||
console.error(`Error writing ${filePath}:`, error); | ||
} | ||
} | ||
|
||
async function main() { | ||
const maintainers: Record<string, string[]> = {}; | ||
const radar: { | ||
[domain: string]: { | ||
_name: string; | ||
[subdomain: string]: RadarItem[] | string; | ||
}; | ||
} = {}; | ||
|
||
for (const namespace in namespaces) { | ||
let defaultCategory = namespaces[namespace].categories?.[0]; | ||
if (!defaultCategory) { | ||
for (const path in namespaces[namespace].routes) { | ||
if (namespaces[namespace].routes[path].categories) { | ||
defaultCategory = namespaces[namespace].routes[path].categories[0]; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if (!defaultCategory) { | ||
defaultCategory = 'other'; | ||
} | ||
for (const path in namespaces[namespace].routes) { | ||
const realPath = `/${namespace}${path}`; | ||
const data = namespaces[namespace].routes[path]; | ||
const categories = data.categories || namespaces[namespace].categories || [defaultCategory]; | ||
// maintainers | ||
if (data.maintainers) { | ||
maintainers[realPath] = data.maintainers; | ||
if (!defaultCategory) { | ||
defaultCategory = 'other'; | ||
} | ||
// radar | ||
if (data.radar?.length) { | ||
for (const radarItem of data.radar) { | ||
const parsedDomain = parse(new URL('https://' + radarItem.source[0]).hostname); | ||
const subdomain = parsedDomain.subdomain || '.'; | ||
const domain = parsedDomain.domain; | ||
if (domain) { | ||
if (!radar[domain]) { | ||
radar[domain] = { | ||
_name: namespaces[namespace].name, | ||
}; | ||
} | ||
if (!radar[domain][subdomain]) { | ||
radar[domain][subdomain] = []; | ||
for (const path in namespaces[namespace].routes) { | ||
const realPath = `/${namespace}${path}`; | ||
const data = namespaces[namespace].routes[path]; | ||
const categories = data.categories || namespaces[namespace].categories || [defaultCategory]; | ||
// maintainers | ||
if (data.maintainers) { | ||
maintainers[realPath] = data.maintainers; | ||
} | ||
// radar | ||
if (data.radar?.length) { | ||
for (const radarItem of data.radar) { | ||
const parsedDomain = parse(new URL('https://' + radarItem.source[0]).hostname); | ||
const subdomain = parsedDomain.subdomain || '.'; | ||
const domain = parsedDomain.domain; | ||
if (domain) { | ||
if (!radar[domain]) { | ||
radar[domain] = { | ||
_name: namespaces[namespace].name, | ||
}; | ||
} | ||
if (!radar[domain][subdomain]) { | ||
radar[domain][subdomain] = []; | ||
} | ||
radar[domain][subdomain].push({ | ||
title: radarItem.title || data.name, | ||
docs: `https://docs.rsshub.app/routes/${categories[0]}`, | ||
source: radarItem.source.map((source) => { | ||
const sourceURL = new URL('https://' + source); | ||
return sourceURL.pathname + sourceURL.search + sourceURL.hash; | ||
}), | ||
target: radarItem.target ? `/${namespace}${radarItem.target}` : realPath, | ||
}); | ||
} | ||
radar[domain][subdomain].push({ | ||
title: radarItem.title || data.name, | ||
docs: `https://docs.rsshub.app/routes/${categories[0]}`, | ||
source: radarItem.source.map((source) => { | ||
const sourceURL = new URL('https://' + source); | ||
return sourceURL.pathname + sourceURL.search + sourceURL.hash; | ||
}), | ||
target: radarItem.target ? `/${namespace}${radarItem.target}` : realPath, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Use process.cwd() to get the absolute path to the project root | ||
const projectRoot = process.cwd(); | ||
const buildDir = path.join(projectRoot, 'assets', 'build'); | ||
|
||
// Ensure the build directory exists | ||
await ensureDirectoryExists(buildDir); | ||
|
||
// Write files using absolute paths | ||
await writeJsonFile(path.join(buildDir, 'radar-rules.json'), radar); | ||
await fs.writeFile(path.join(buildDir, 'radar-rules.js'), `(${toSource(radar)})`); | ||
await writeJsonFile(path.join(buildDir, 'maintainers.json'), maintainers); | ||
await writeJsonFile(path.join(buildDir, 'routes.json'), namespaces); | ||
} | ||
|
||
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.json'), JSON.stringify(radar, null, 2)); | ||
fs.writeFileSync(path.join(__dirname, '../../assets/build/radar-rules.js'), `(${toSource(radar)})`); | ||
fs.writeFileSync(path.join(__dirname, '../../assets/build/maintainers.json'), JSON.stringify(maintainers, null, 2)); | ||
fs.writeFileSync(path.join(__dirname, '../../assets/build/routes.json'), JSON.stringify(namespaces, null, 2)); | ||
main().catch(error => { | ||
console.error('An error occurred during the build process:', error); | ||
process.exit(1); | ||
}); |