forked from dompling/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2YaInstall.js
70 lines (62 loc) · 2.18 KB
/
2YaInstall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: brown; icon-glyph: download;
//订阅地址
const subscriptionURL = 'https://raw.githubusercontent.com/dompling/Scriptable/master/install.json';
class YaYaInstall {
constructor() {
this.request = new Request('');
this.files = FileManager.iCloud();
this.rootPath = this.files.documentsDirectory();
this.defaultHeaders = {
Accept: '*/*',
'Content-Type': 'application/json',
};
}
saveFileName = (fileName) => {
const hasSuffix = fileName.lastIndexOf('.') + 1;
return !hasSuffix ? `${fileName}.js` : fileName;
};
write = (fileName, content) => {
let file = this.saveFileName(fileName);
const filePath = `${this.rootPath}/${file}`;
FileManager.iCloud().writeString(filePath, content);
return true;
};
fetchUrlString = async ({url, headers = {}}, callback = () => {}) => {
this.request.url = url;
this.request.method = 'GET';
this.request.headers = {
...headers,
...this.defaultHeaders,
};
const data = await this.request.loadString();
callback(this.request.response, data);
return data;
};
saveFile = async ({moduleName, url}) => {
const header = `// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: file-code;\n`;
const content = await this.fetchUrlString({url});
const fileHeader = content.includes('icon-color') ? `` : header;
this.write(`${moduleName}`, `${fileHeader}${content}`);
};
install = async () => {
console.log('🤖更新开始!');
const req = new Request(subscriptionURL);
const subscription = await req.loadJSON();
const apps = subscription.apps;
for (const script of apps) {
await this.saveFile({moduleName: script.name, url: script.scriptURL});
if (script.depend) {
for (const item of script.depend) {
await this.saveFile({moduleName: item.name, url: item.scriptURL});
}
}
// console.log(script.moduleName + ':更新成功');
}
console.log('🤖更新结束!');
};
}
await new YaYaInstall().install();