feat: 新增参数 prettyYaml 输出更易读的块状 YAML, 默认仍是单行 JSON 风格

This commit is contained in:
xream committed 2026-04-06 01:53:57 +08:00
1 parent 404df83d52
commit ed4ae7ade1
12 files changed
+128 -39

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.21.77",
"version": "2.21.78",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
"main": "src/main.js",
"scripts": {
@@ -1,4 +1,7 @@
import { isPresent } from '@/core/proxy-utils/producers/utils';
import {
isPresent,
produceProxyListOutput,
} from '@/core/proxy-utils/producers/utils';
import $ from '@/core/app';
export default function Clash_Producer() {
@@ -213,12 +216,7 @@ export default function Clash_Producer() {
}
return proxy;
});
return type === 'internal'
? list
: 'proxies:\n' +
list
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
.join('');
return produceProxyListOutput(list, type, opts);
};
return { type, produce };
}
@@ -1,4 +1,7 @@
import { isPresent } from '@/core/proxy-utils/producers/utils';
import {
isPresent,
produceProxyListOutput,
} from '@/core/proxy-utils/producers/utils';
const ipVersions = {
dual: 'dual',
@@ -301,12 +304,7 @@ export default function ClashMeta_Producer() {
return proxy;
});
return type === 'internal'
? list
: 'proxies:\n' +
list
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
.join('');
return produceProxyListOutput(list, type, opts);
};
return { type, produce };
}
@@ -1,9 +1,9 @@
import $ from '@/core/app';
import { isPresent } from './utils';
import { isPresent, produceProxyListOutput } from './utils';
export default function Egern_Producer() {
const type = 'ALL';
const produce = (proxies, type) => {
const produce = (proxies, type, opts = {}) => {
// https://egernapp.com/zh-CN/docs/configuration/proxies
const list = proxies
.filter((proxy) => {
@@ -572,12 +572,7 @@ export default function Egern_Producer() {
}
})
.filter(Boolean);
return type === 'internal'
? list
: 'proxies:\n' +
list
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
.join('');
return produceProxyListOutput(list, type, opts);
};
return { type, produce };
}
@@ -1,4 +1,7 @@
import { isPresent } from '@/core/proxy-utils/producers/utils';
import {
isPresent,
produceProxyListOutput,
} from '@/core/proxy-utils/producers/utils';
import $ from '@/core/app';
export default function Shadowrocket_Producer() {
@@ -271,14 +274,7 @@ export default function Shadowrocket_Producer() {
}
return proxy;
});
return type === 'internal'
? list
: 'proxies:\n' +
list
.map((proxy) => {
return ' - ' + JSON.stringify(proxy) + '\n';
})
.join('');
return produceProxyListOutput(list, type, opts);
};
return { type, produce };
}
@@ -1,4 +1,7 @@
import { isPresent } from '@/core/proxy-utils/producers/utils';
import {
isPresent,
produceProxyListOutput,
} from '@/core/proxy-utils/producers/utils';
import $ from '@/core/app';
export default function Stash_Producer() {
@@ -347,12 +350,7 @@ export default function Stash_Producer() {
}
return proxy;
});
return type === 'internal'
? list
: 'proxies:\n' +
list
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
.join('');
return produceProxyListOutput(list, type, opts);
};
return { type, produce };
}
@@ -1,4 +1,5 @@
import _ from 'lodash';
import YAML from '@/utils/yaml';
export class Result {
constructor(proxy) {
@@ -28,3 +29,24 @@ export function isPresent(obj, attr) {
const data = _.get(obj, attr);
return typeof data !== 'undefined' && data !== null;
}
export function produceProxyListOutput(list, type, opts = {}) {
if (type === 'internal') return list;
if (
opts.prettyYaml ||
opts['pretty-yaml']
) {
return YAML.safeDump(
{
proxies: list,
},
{
lineWidth: -1,
},
);
}
return 'proxies:\n' +
list.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n').join('');
}
@@ -157,6 +157,7 @@ async function doSync() {
'include-unsupported-proxy':
artifact.includeUnsupportedProxy,
useMihomoExternal,
prettyYaml: artifact.prettyYaml,
},
});
+10
View File
@@ -107,6 +107,7 @@ async function downloadSubscription(req, res) {
noCache,
_fakeNode,
} = req.query;
const prettyYaml = req.query.prettyYaml ?? req.query['pretty-yaml'];
let $options = {
_req: {
@@ -169,6 +170,9 @@ async function downloadSubscription(req, res) {
`包含官方/商店版/未续费订阅不支持的协议: ${includeUnsupportedProxy}`,
);
}
if (prettyYaml) {
$.info(`指定输出易读 YAML: ${prettyYaml}`);
}
if (useMihomoExternal) {
$.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`);
@@ -208,6 +212,7 @@ async function downloadSubscription(req, res) {
produceOpts: {
'include-unsupported-proxy': includeUnsupportedProxy,
useMihomoExternal,
prettyYaml,
},
$options,
proxy,
@@ -415,6 +420,7 @@ async function downloadCollection(req, res) {
proxy,
noCache,
} = req.query;
const prettyYaml = req.query.prettyYaml ?? req.query['pretty-yaml'];
let $options = {
_req: {
@@ -463,6 +469,9 @@ async function downloadCollection(req, res) {
`包含官方/商店版/未续费订阅不支持的协议: ${includeUnsupportedProxy}`,
);
}
if (prettyYaml) {
$.info(`指定输出易读 YAML: ${prettyYaml}`);
}
if (useMihomoExternal) {
$.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`);
@@ -482,6 +491,7 @@ async function downloadCollection(req, res) {
produceOpts: {
'include-unsupported-proxy': includeUnsupportedProxy,
useMihomoExternal,
prettyYaml,
},
$options,
proxy,
+2
View File
@@ -684,6 +684,7 @@ async function syncArtifacts() {
'include-unsupported-proxy':
artifact.includeUnsupportedProxy,
useMihomoExternal,
prettyYaml: artifact.prettyYaml,
},
});
@@ -838,6 +839,7 @@ async function syncArtifact(req, res) {
produceOpts: {
'include-unsupported-proxy': artifact.includeUnsupportedProxy,
useMihomoExternal,
prettyYaml: artifact.prettyYaml,
},
});
@@ -7,6 +7,7 @@ import {
expectSubset,
loadProducedJson,
loadProducedYaml,
produceExternal,
produceInternal,
} from './helpers';
@@ -571,6 +572,71 @@ describe('Proxy structured producers', function () {
}
});
it('keeps legacy single-line proxy output by default for Clash-style YAML producers', function () {
const proxy = {
type: 'ss',
name: 'Legacy YAML',
server: 'ss.example.com',
port: 8388,
cipher: 'aes-128-gcm',
password: 'secret',
};
for (const platform of [
'Clash',
'ClashMeta',
'Shadowrocket',
'Stash',
'Egern',
]) {
const output = produceExternal(platform, proxy);
expect(output, platform).to.match(/\n - \{/);
}
});
it('emits pretty yaml for Clash-style YAML producers when prettyYaml is enabled', function () {
const proxy = {
type: 'ss',
name: 'Real YAML',
server: 'ss.example.com',
port: 8388,
cipher: 'aes-128-gcm',
password: 'secret',
};
for (const platform of [
'Clash',
'ClashMeta',
'Shadowrocket',
'Stash',
'Egern',
]) {
const output = produceExternal(platform, proxy, {
prettyYaml: true,
});
const external = loadProducedYaml(platform, proxy, {
prettyYaml: true,
});
expect(output, platform).to.not.match(/\n - \{/);
expect(external.proxies, platform).to.have.length(1);
if (platform === 'Egern') {
expectSubset(external.proxies[0], {
shadowsocks: {
name: 'Real YAML',
},
});
} else {
expectSubset(external.proxies[0], {
type: 'ss',
name: 'Real YAML',
});
}
}
});
it('skips invalid Egern nodes and keeps the rest of the subscription', function () {
const proxies = [
{
+3
View File
@@ -258,6 +258,9 @@ function operator(proxies = [], targetPlatform, context) {
// type: 'subscription',
// name: 'sub',
// platform: 'ClashMeta',
// produceOpts: {
// prettyYaml: true // 输出更易读的块状 YAML, 默认仍是单行 JSON 风格
// },
// produceType: 'internal' // 'internal' produces an Array, otherwise produces a String( ProxyUtils.yaml.safeLoad('YAML String').proxies )
// })