Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

程序化配置文件 #279

Open
liblaf opened this issue Aug 8, 2024 · 2 comments
Open

程序化配置文件 #279

liblaf opened this issue Aug 8, 2024 · 2 comments

Comments

@liblaf
Copy link

liblaf commented Aug 8, 2024

希望能够将 surgio 作为第三方库使用, 例如:

import { defineClashProvider } from "surgio";
import { getProvider } from "surgio/provider";

const provider = await getProvider(
  "NAME",
  defineClashProvider({ url: "https://example.com" }),
);
console.log(provider.getNodeList());

但得到了如下错误:

50 | };
51 | exports.loadConfig = loadConfig;
52 | const getConfig = () => {
53 |     // istanbul ignore next
54 |     if (!finalConfig) {
55 |         throw new Error('请先调用 loadConfig 方法');
                   ^
error: 请先调用 loadConfig 方法
      at getConfig (***/node_modules/surgio/build/config.js:55:15)
      at new Provider (***/node_modules/surgio/build/provider/Provider.js:59:26)
      at new ClashProvider (***/node_modules/surgio/build/provider/ClashProvider.js:25:9)
      at ***/node_modules/surgio/build/provider/index.js:44:20
      at getProvider (***/node_modules/surgio/build/provider/index.js:26:28)
      at ***/hello.ts:4:24

Bun v1.1.21 (Linux x64)

surgio/src/config.ts

Lines 20 to 61 in 7bc53d4

export const loadConfig = (
cwd: string,
override?: Partial<CommandConfig>,
): CommandConfig => {
const absPath = path.join(cwd, 'surgio.conf.js')
// istanbul ignore next
if (!fs.existsSync(absPath)) {
throw new Error(`配置文件 ${absPath} 不存在`)
}
const userConfig = validateConfig(_.cloneDeep(require(absPath)))
if (userConfig.flags) {
Object.keys(userConfig.flags).forEach((emoji) => {
if (userConfig.flags) {
if (typeof userConfig.flags[emoji] === 'string') {
addFlagMap(userConfig.flags[emoji] as string, emoji)
} else if (_.isRegExp(userConfig.flags[emoji])) {
addFlagMap(userConfig.flags[emoji] as RegExp, emoji)
} else {
;(userConfig.flags[emoji] as ReadonlyArray<string | RegExp>).forEach(
(name) => {
addFlagMap(name, emoji)
},
)
}
}
})
}
if (override) {
return {
...normalizeConfig(cwd, userConfig),
...override,
}
}
finalConfig = normalizeConfig(cwd, userConfig)
return finalConfig
}

注意到 loadConfig() 需要从文件系统中读取 surgio.conf.js 并保存在 finalConfig 中. 是否可以允许程序化设置 finalConfig 而无需 surgio.conf.js 文件?

@geekdada
Copy link
Member

geekdada commented Sep 9, 2024

请问这么做是要实现什么功能?

@liblaf
Copy link
Author

liblaf commented Sep 9, 2024

请问这么做是要实现什么功能?

@geekdada 我希望在 serverless (例如 cf worker) 上部署代理规则生成器, 我希望能够复用 surgio 部分代码.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants