MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / parseScriptFromCode

Function parseScriptFromCode

src/pkg/utils/script.ts:67–129  ·  view source on GitHub ↗
(code: string, origin: string, uuid?: string)

Source from the content-addressed store, hash-verified

65
66// 通过代码解析出脚本基本信息 (不含数据库查询)
67export function parseScriptFromCode(code: string, origin: string, uuid?: string): Script {
68 const metadata = parseMetadata(code);
69 if (!metadata) {
70 throw new Error(i18n_t("error_metadata_invalid"));
71 }
72 // 不接受空白name
73 if (!metadata.name?.[0]) {
74 throw new Error(i18n_t("error_script_name_required"));
75 }
76 // 可接受空白namespace
77 if (metadata.namespace === undefined) {
78 throw new Error(i18n_t("error_script_namespace_required"));
79 }
80 // 可接受空白version
81 let type = SCRIPT_TYPE_NORMAL;
82 if (metadata.crontab !== undefined) {
83 type = SCRIPT_TYPE_CRONTAB;
84 try {
85 extractCronExpr(metadata.crontab[0]);
86 } catch {
87 throw new Error(i18n_t("error_cron_invalid", { expr: metadata.crontab[0] }));
88 }
89 } else if (metadata.background !== undefined) {
90 type = SCRIPT_TYPE_BACKGROUND;
91 }
92 let domain = "";
93 let checkUpdateUrl = "";
94 let downloadUrl = origin;
95 if (metadata.updateurl && metadata.downloadurl) {
96 [checkUpdateUrl] = metadata.updateurl;
97 [downloadUrl] = metadata.downloadurl;
98 } else {
99 checkUpdateUrl = origin.replace("user.js", "meta.js");
100 }
101 if (origin.startsWith("http://") || origin.startsWith("https://")) {
102 const u = new URL(origin);
103 domain = u.hostname;
104 }
105 const newUUID = uuid || uuidv4();
106 const config: UserConfig | undefined = parseUserConfig(code);
107 const now = Date.now();
108 return {
109 uuid: newUUID,
110 name: metadata.name[0],
111 author: metadata.author && metadata.author[0],
112 namespace: metadata.namespace[0], // 上面的代码已检查 meta.namespace, 不会为undefined
113 originDomain: domain,
114 origin,
115 checkUpdate: true,
116 checkUpdateUrl,
117 downloadUrl,
118 config,
119 metadata,
120 selfMetadata: {},
121 sort: -1,
122 type,
123 status: SCRIPT_STATUS_DISABLE,
124 runStatus: SCRIPT_RUN_STATUS_COMPLETE,

Callers 2

script.test.tsFile · 0.90
prepareScriptByCodeFunction · 0.85

Calls 3

extractCronExprFunction · 0.90
parseUserConfigFunction · 0.90
parseMetadataFunction · 0.85

Tested by

no test coverage detected