mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2025-08-10 00:52:40 +00:00
79 lines
2.8 KiB
JavaScript
79 lines
2.8 KiB
JavaScript
import { version } from '../../package.json';
|
||
import { SETTINGS_KEY, ARTIFACTS_KEY } from '@/constants';
|
||
import $ from '@/core/app';
|
||
import { produceArtifact } from '@/restful/sync';
|
||
import { syncToGist } from '@/restful/artifacts';
|
||
|
||
!(async function () {
|
||
const settings = $.read(SETTINGS_KEY);
|
||
// if GitHub token is not configured
|
||
if (!settings.githubUser || !settings.gistToken) return;
|
||
|
||
const artifacts = $.read(ARTIFACTS_KEY);
|
||
if (!artifacts || artifacts.length === 0) return;
|
||
|
||
const shouldSync = artifacts.some((artifact) => artifact.sync);
|
||
if (shouldSync) await doSync();
|
||
})().finally(() => $.done());
|
||
|
||
async function doSync() {
|
||
console.log(
|
||
`
|
||
┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
|
||
Sub-Store Sync -- v${version}
|
||
┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
|
||
`,
|
||
);
|
||
|
||
$.info('开始同步所有远程配置...');
|
||
const allArtifacts = $.read(ARTIFACTS_KEY);
|
||
const files = {};
|
||
|
||
try {
|
||
await Promise.all(
|
||
allArtifacts.map(async (artifact) => {
|
||
if (artifact.sync) {
|
||
$.info(`正在同步云配置:${artifact.name}...`);
|
||
const output = await produceArtifact({
|
||
type: artifact.type,
|
||
name: artifact.source,
|
||
platform: artifact.platform,
|
||
});
|
||
|
||
files[encodeURIComponent(artifact.name)] = {
|
||
content: output,
|
||
};
|
||
}
|
||
}),
|
||
);
|
||
|
||
const resp = await syncToGist(files);
|
||
const body = JSON.parse(resp.body);
|
||
|
||
for (const artifact of allArtifacts) {
|
||
if (artifact.sync) {
|
||
artifact.updated = new Date().getTime();
|
||
// extract real url from gist
|
||
let files = body.files;
|
||
let isGitLab;
|
||
if (Array.isArray(files)) {
|
||
isGitLab = true;
|
||
files = Object.fromEntries(
|
||
files.map((item) => [item.path, item]),
|
||
);
|
||
}
|
||
const url = files[encodeURIComponent(artifact.name)]?.raw_url;
|
||
artifact.url = isGitLab
|
||
? url
|
||
: url?.replace(/\/raw\/[^/]*\/(.*)/, '/raw/$1');
|
||
}
|
||
}
|
||
|
||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||
$.notify('🌍 Sub-Store', '全部订阅同步成功!');
|
||
} catch (err) {
|
||
$.notify('🌍 Sub-Store', '同步订阅失败', `原因:${err}`);
|
||
$.error(`无法同步订阅配置到 Gist,原因:${err}`);
|
||
}
|
||
}
|