Skip to content

Commit ed4ae7a

Browse files
committed
feat: 新增参数 prettyYaml 输出更易读的块状 YAML, 默认仍是单行 JSON 风格
1 parent 404df83 commit ed4ae7a

12 files changed

Lines changed: 128 additions & 39 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.21.77",
3+
"version": "2.21.78",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/core/proxy-utils/producers/clash.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { isPresent } from '@/core/proxy-utils/producers/utils';
1+
import {
2+
isPresent,
3+
produceProxyListOutput,
4+
} from '@/core/proxy-utils/producers/utils';
25
import $ from '@/core/app';
36

47
export default function Clash_Producer() {
@@ -213,12 +216,7 @@ export default function Clash_Producer() {
213216
}
214217
return proxy;
215218
});
216-
return type === 'internal'
217-
? list
218-
: 'proxies:\n' +
219-
list
220-
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
221-
.join('');
219+
return produceProxyListOutput(list, type, opts);
222220
};
223221
return { type, produce };
224222
}

backend/src/core/proxy-utils/producers/clashmeta.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { isPresent } from '@/core/proxy-utils/producers/utils';
1+
import {
2+
isPresent,
3+
produceProxyListOutput,
4+
} from '@/core/proxy-utils/producers/utils';
25

36
const ipVersions = {
47
dual: 'dual',
@@ -301,12 +304,7 @@ export default function ClashMeta_Producer() {
301304
return proxy;
302305
});
303306

304-
return type === 'internal'
305-
? list
306-
: 'proxies:\n' +
307-
list
308-
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
309-
.join('');
307+
return produceProxyListOutput(list, type, opts);
310308
};
311309
return { type, produce };
312310
}

backend/src/core/proxy-utils/producers/egern.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import $ from '@/core/app';
2-
import { isPresent } from './utils';
2+
import { isPresent, produceProxyListOutput } from './utils';
33

44
export default function Egern_Producer() {
55
const type = 'ALL';
6-
const produce = (proxies, type) => {
6+
const produce = (proxies, type, opts = {}) => {
77
// https://egernapp.com/zh-CN/docs/configuration/proxies
88
const list = proxies
99
.filter((proxy) => {
@@ -572,12 +572,7 @@ export default function Egern_Producer() {
572572
}
573573
})
574574
.filter(Boolean);
575-
return type === 'internal'
576-
? list
577-
: 'proxies:\n' +
578-
list
579-
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
580-
.join('');
575+
return produceProxyListOutput(list, type, opts);
581576
};
582577
return { type, produce };
583578
}

backend/src/core/proxy-utils/producers/shadowrocket.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { isPresent } from '@/core/proxy-utils/producers/utils';
1+
import {
2+
isPresent,
3+
produceProxyListOutput,
4+
} from '@/core/proxy-utils/producers/utils';
25
import $ from '@/core/app';
36

47
export default function Shadowrocket_Producer() {
@@ -271,14 +274,7 @@ export default function Shadowrocket_Producer() {
271274
}
272275
return proxy;
273276
});
274-
return type === 'internal'
275-
? list
276-
: 'proxies:\n' +
277-
list
278-
.map((proxy) => {
279-
return ' - ' + JSON.stringify(proxy) + '\n';
280-
})
281-
.join('');
277+
return produceProxyListOutput(list, type, opts);
282278
};
283279
return { type, produce };
284280
}

backend/src/core/proxy-utils/producers/stash.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { isPresent } from '@/core/proxy-utils/producers/utils';
1+
import {
2+
isPresent,
3+
produceProxyListOutput,
4+
} from '@/core/proxy-utils/producers/utils';
25
import $ from '@/core/app';
36

47
export default function Stash_Producer() {
@@ -347,12 +350,7 @@ export default function Stash_Producer() {
347350
}
348351
return proxy;
349352
});
350-
return type === 'internal'
351-
? list
352-
: 'proxies:\n' +
353-
list
354-
.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n')
355-
.join('');
353+
return produceProxyListOutput(list, type, opts);
356354
};
357355
return { type, produce };
358356
}

backend/src/core/proxy-utils/producers/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _ from 'lodash';
2+
import YAML from '@/utils/yaml';
23

34
export class Result {
45
constructor(proxy) {
@@ -28,3 +29,24 @@ export function isPresent(obj, attr) {
2829
const data = _.get(obj, attr);
2930
return typeof data !== 'undefined' && data !== null;
3031
}
32+
33+
export function produceProxyListOutput(list, type, opts = {}) {
34+
if (type === 'internal') return list;
35+
36+
if (
37+
opts.prettyYaml ||
38+
opts['pretty-yaml']
39+
) {
40+
return YAML.safeDump(
41+
{
42+
proxies: list,
43+
},
44+
{
45+
lineWidth: -1,
46+
},
47+
);
48+
}
49+
50+
return 'proxies:\n' +
51+
list.map((proxy) => ' - ' + JSON.stringify(proxy) + '\n').join('');
52+
}

backend/src/products/cron-sync-artifacts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ async function doSync() {
157157
'include-unsupported-proxy':
158158
artifact.includeUnsupportedProxy,
159159
useMihomoExternal,
160+
prettyYaml: artifact.prettyYaml,
160161
},
161162
});
162163

backend/src/restful/download.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ async function downloadSubscription(req, res) {
107107
noCache,
108108
_fakeNode,
109109
} = req.query;
110+
const prettyYaml = req.query.prettyYaml ?? req.query['pretty-yaml'];
110111

111112
let $options = {
112113
_req: {
@@ -169,6 +170,9 @@ async function downloadSubscription(req, res) {
169170
`包含官方/商店版/未续费订阅不支持的协议: ${includeUnsupportedProxy}`,
170171
);
171172
}
173+
if (prettyYaml) {
174+
$.info(`指定输出易读 YAML: ${prettyYaml}`);
175+
}
172176

173177
if (useMihomoExternal) {
174178
$.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`);
@@ -208,6 +212,7 @@ async function downloadSubscription(req, res) {
208212
produceOpts: {
209213
'include-unsupported-proxy': includeUnsupportedProxy,
210214
useMihomoExternal,
215+
prettyYaml,
211216
},
212217
$options,
213218
proxy,
@@ -415,6 +420,7 @@ async function downloadCollection(req, res) {
415420
proxy,
416421
noCache,
417422
} = req.query;
423+
const prettyYaml = req.query.prettyYaml ?? req.query['pretty-yaml'];
418424

419425
let $options = {
420426
_req: {
@@ -463,6 +469,9 @@ async function downloadCollection(req, res) {
463469
`包含官方/商店版/未续费订阅不支持的协议: ${includeUnsupportedProxy}`,
464470
);
465471
}
472+
if (prettyYaml) {
473+
$.info(`指定输出易读 YAML: ${prettyYaml}`);
474+
}
466475

467476
if (useMihomoExternal) {
468477
$.info(`手动指定了 target 为 SurgeMac, 将使用 Mihomo External`);
@@ -482,6 +491,7 @@ async function downloadCollection(req, res) {
482491
produceOpts: {
483492
'include-unsupported-proxy': includeUnsupportedProxy,
484493
useMihomoExternal,
494+
prettyYaml,
485495
},
486496
$options,
487497
proxy,

backend/src/restful/sync.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ async function syncArtifacts() {
684684
'include-unsupported-proxy':
685685
artifact.includeUnsupportedProxy,
686686
useMihomoExternal,
687+
prettyYaml: artifact.prettyYaml,
687688
},
688689
});
689690

@@ -838,6 +839,7 @@ async function syncArtifact(req, res) {
838839
produceOpts: {
839840
'include-unsupported-proxy': artifact.includeUnsupportedProxy,
840841
useMihomoExternal,
842+
prettyYaml: artifact.prettyYaml,
841843
},
842844
});
843845

0 commit comments

Comments
 (0)