-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScript Installer.js
More file actions
212 lines (175 loc) · 6.96 KB
/
Script Installer.js
File metadata and controls
212 lines (175 loc) · 6.96 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: purple; icon-glyph: download;
const { Files } = importModule("Files");
const { modal } = importModule("Modal");
const { tr } = importModule("Localization");
const { metadata, cacheRequest } = importModule("Cache");
const { JS_EXTENSION, EMPTY_STRING } = importModule("Constants");
const { bundleScript } = importModule("Bundler");
/**
* Main entry point for the Script Installer.
* Orchestrates repository downloading, user selection via modal, and script installation.
*/
async function main() {
const installer = new ScriptInstaller();
await installer.downloadRepository();
// Make sure script installer is up-to-date.
await installer.installScript("Script Installer");
const result = await modal()
.title(tr("installer_scriptSelectionModalTitle"))
.actions(installer.getScriptList())
.present();
if (!result.isCancelled()) {
const fileInfo = await installer.installScript(result.choice());
const targetScriptPath = Files.joinPaths(Files.getScriptableDirectory(), fileInfo.name());
// Move script requested by user to scriptable root directory.
Files.forceMove(fileInfo.path(), targetScriptPath);
}
installer.cleanupResources();
}
/**
* Handles the downloading and installation of scripts from a remote GitHub repository.
* Manages caching, file system operations, and asset synchronization.
*/
class ScriptInstaller {
/**
* The GitHub API endpoint for the repository tree.
* @type {string}
* @private
*/
#REPO_URL = "https://api.github.com/repos/pikulo-kama/scriptable-projects/git/trees/main?recursive=1"
/**
* Downloads the entire repository structure to a local temporary directory.
* Utilizes caching to respect GitHub API rate limits.
* @async
*/
async downloadRepository() {
const fm = Files.manager();
// Due to GitHub API limitations we are caching responses for 24 hours
// before fetching new data.
const treeRequest = cacheRequest(this.#treeRequestMetadata(), 24);
const fileRequest = cacheRequest(this.#fileRequestMetadata(), 24);
const treeData = await treeRequest.get(this.#REPO_URL);
if (!fm.isDirectory(this.#repositoryDirectory())) {
fm.createDirectory(this.#repositoryDirectory());
}
for (const item of treeData.tree) {
const itemPath = Files.joinPaths(this.#repositoryDirectory(), item.path);
if (item.type === "tree") {
fm.createDirectory(itemPath, true);
} else if (item.type === "blob") {
const fileInfo = await fileRequest.get(item.url);
const content = fileInfo.content.replace(/\s/g, "");
const fileData = Data.fromBase64String(content);
fm.write(itemPath, fileData);
}
}
}
/**
* Bundles a specific script and moves it to the Scriptable documents folder.
* @async
* @param {string} scriptName - The name of the script to install.
*/
async installScript(scriptName) {
const bundledFileInfo = await bundleScript(scriptName, this.#repositoryDirectory());
this.#installScriptResources(scriptName, bundledFileInfo.dependencies());
return bundledFileInfo;
}
/**
* Synchronizes associated resources (Features, i18n, Resources) for a given script.
* @async
* @param {string} scriptName - The name of the script whose resources are being installed.
* @param {Iterable<string>} dependencies - List of dependency script names.
*/
async #installScriptResources(scriptName, dependencies) {
const scripts = Array.of(scriptName);
scripts.push(...dependencies);
const directoriesToSync = [
Files.FeaturesDirectory,
Files.ResourcesDirectory,
Files.LocalesDirectory
];
// Move script data if available.
for (const directory of directoriesToSync) {
for (const script of scripts) {
this.#syncDirectory(directory, script);
}
}
}
/**
* Synchronizes a specific directory for a script by moving files from
* the downloaded repository to the local Scriptable environment.
* * It mirrors the source structure, creates target directories if they
* are missing, and uses a force move to overwrite existing files.
*
* @private
* @param {string} directory - The base category directory (e.g., 'i18n' or 'Resources').
* @param {string} scriptName - The name of the script directory to sync.
* @memberof ScriptInstaller
*/
#syncDirectory(directory, scriptName) {
const fm = Files.manager();
const sourceDirectoryPath = Files.joinPaths(this.#repositoryDirectory(), directory, scriptName);
const targetDirectoryPath = Files.joinPaths(Files.getScriptableDirectory(), directory, scriptName);
// Script doesn't have files in repository directory.
if (!fm.isDirectory(sourceDirectoryPath)) {
return;
}
for (const directoryFile of fm.listContents(sourceDirectoryPath)) {
const sourceFilePath = Files.joinPaths(sourceDirectoryPath, directoryFile);
const targetFilePath = Files.joinPaths(targetDirectoryPath, directoryFile);
if (!fm.isDirectory(targetDirectoryPath)) {
fm.createDirectory(targetDirectoryPath, true);
}
Files.forceMove(sourceFilePath, targetFilePath);
}
}
/**
* Retrieves a list of available scripts from the downloaded repository.
* @returns {string[]} Sorted list of script names without extensions.
*/
getScriptList() {
return Files.findScripts(this.#repositoryDirectory())
.map((script) => script.replace(JS_EXTENSION, EMPTY_STRING))
.sort();
}
/**
* Removes the temporary repository directory after installation.
*/
cleanupResources() {
const fm = Files.manager();
fm.remove(this.#repositoryDirectory());
}
/**
* Resolves the path to the temporary repository storage.
* @returns {string} Path to the Repository directory.
*/
#repositoryDirectory() {
return Files.resolveLocalResource("Repository");
}
/**
* Internal metadata schema for repository tree requests.
* @private
*/
#treeRequestMetadata() {
return metadata()
.list().property("tree")
.data().property("path").add()
.data().property("type").add()
.data().property("url").add()
.add()
.create();
}
/**
* Internal metadata schema for file content requests.
* @private
*/
#fileRequestMetadata() {
return metadata()
.data().property("content").add()
.create();
}
}
await main();
Script.complete();