-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae57261
commit 3535cdb
Showing
13 changed files
with
177 additions
and
83 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { typeSafeObjectEntries } from "@shared/type.util" | ||
import type { SourceID } from "@shared/types" | ||
import * as x from "glob:./sources/{*.ts,**/index.ts}" | ||
import type { SourceGetter } from "./types" | ||
|
||
export const getters = (function () { | ||
const getters = {} as Record<SourceID, SourceGetter> | ||
typeSafeObjectEntries(x).forEach(([id, x]) => { | ||
if (x.default instanceof Function) { | ||
Object.assign(getters, { [id]: x.default }) | ||
} else { | ||
Object.assign(getters, x.default) | ||
} | ||
}) | ||
return getters | ||
})() |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable */ | ||
|
||
declare module 'glob:./sources/{*.ts,**/index.ts}' { | ||
export const _36kr: typeof import('./sources/_36kr') | ||
export const cankaoxiaoxi: typeof import('./sources/cankaoxiaoxi') | ||
export const cls: typeof import('./sources/cls/index') | ||
export const coolapk: typeof import('./sources/coolapk/index') | ||
export const douyin: typeof import('./sources/douyin') | ||
export const fastbull: typeof import('./sources/fastbull') | ||
export const gelonghui: typeof import('./sources/gelonghui') | ||
export const ithome: typeof import('./sources/ithome') | ||
export const sputniknewscn: typeof import('./sources/sputniknewscn') | ||
export const thepaper: typeof import('./sources/thepaper') | ||
export const tieba: typeof import('./sources/tieba') | ||
export const toutiao: typeof import('./sources/toutiao') | ||
export const v2ex: typeof import('./sources/v2ex') | ||
export const wallstreetcn: typeof import('./sources/wallstreetcn') | ||
export const weibo: typeof import('./sources/weibo') | ||
export const xueqiu: typeof import('./sources/xueqiu') | ||
export const zaobao: typeof import('./sources/zaobao') | ||
export const zhihu: typeof import('./sources/zhihu') | ||
} |
File renamed without changes.
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import path from "node:path" | ||
import { writeFile } from "node:fs/promises" | ||
import type { Plugin } from "rollup" | ||
import glob from "fast-glob" | ||
import type { FilterPattern } from "@rollup/pluginutils" | ||
import { createFilter, normalizePath } from "@rollup/pluginutils" | ||
import { projectDir } from "../shared/dir" | ||
|
||
const ID_PREFIX = "glob:" | ||
const root = path.join(projectDir, "server") | ||
type GlobMap = Record<string /* name:pattern */, string[]> | ||
|
||
export function RollopGlob(): Plugin { | ||
const map: GlobMap = {} | ||
const include: FilterPattern = [] | ||
const exclude: FilterPattern = [] | ||
const filter = createFilter(include, exclude) | ||
return { | ||
name: "rollup-glob", | ||
resolveId(id, src) { | ||
if (!id.startsWith(ID_PREFIX)) return | ||
if (!src || !filter(src)) return | ||
|
||
return `${id}:${encodeURIComponent(src)}` | ||
}, | ||
async load(id) { | ||
if (!id.startsWith(ID_PREFIX)) return | ||
|
||
const [_, pattern, encodePath] = id.split(":") | ||
const currentPath = decodeURIComponent(encodePath) | ||
|
||
const files = ( | ||
await glob(pattern, { | ||
cwd: currentPath ? path.dirname(currentPath) : root, | ||
absolute: true, | ||
}) | ||
) | ||
.map(file => normalizePath(file)) | ||
.filter(file => file !== normalizePath(currentPath)) | ||
.sort() | ||
map[pattern] = files | ||
|
||
const contents = files.map((file) => { | ||
const r = file.replace("/index", "") | ||
const name = path.basename(r, path.extname(r)) | ||
return `export * as ${name} from '${file}'\n` | ||
}).join("\n") | ||
|
||
await writeTypeDeclaration(map, path.join(root, "glob")) | ||
|
||
return `${contents}\n` | ||
}, | ||
} | ||
} | ||
|
||
async function writeTypeDeclaration(map: GlobMap, filename: string) { | ||
function relatePath(filepath: string) { | ||
return normalizePath(path.relative(path.dirname(filename), filepath)) | ||
} | ||
|
||
let declare = `/* eslint-disable */\n\n` | ||
|
||
const sortedEntries = Object.entries(map).sort(([a], [b]) => | ||
a.localeCompare(b), | ||
) | ||
|
||
for (const [_idx, [id, files]] of sortedEntries.entries()) { | ||
declare += `declare module '${ID_PREFIX}${id}' {\n` | ||
for (const file of files) { | ||
const relative = `./${relatePath(file)}`.replace(/\.tsx?$/, "") | ||
const r = file.replace("/index", "") | ||
const fileName = path.basename(r, path.extname(r)) | ||
declare += ` export const ${fileName}: typeof import('${relative}')\n` | ||
} | ||
declare += `}\n` | ||
} | ||
await writeFile(`${filename}.d.ts`, declare, "utf-8") | ||
} |
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