forked from espruino/BangleApps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_from_files.js
More file actions
142 lines (129 loc) · 4.44 KB
/
install_from_files.js
File metadata and controls
142 lines (129 loc) · 4.44 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
/**
* Apploader - Install App from selected files
*
* This function allows users to install BangleJS apps by selecting files from their local filesystem.
* It reads metadata.json and uploads all referenced files to the watch using the standard upload pipeline.
*/
function installFromFiles() {
return new Promise(resolve => {
// Request multi-file selection from user
Espruino.Core.Utils.fileOpenDialog({
id:"installappfiles",
type:"arraybuffer",
multi:true,
mimeType:"*/*",
onComplete: function(files) {
try {
if (!files) return resolve(); // user cancelled
const mapped = files.map(function(f) {
return { name: f.fileName, data: f.contents };
});
processFiles(mapped, resolve);
} catch (err) {
showToast('Install failed: ' + err, 'error');
console.error(err);
resolve();
}
}
});
});
}
function processFiles(files, resolve) {
if (!files || files.length === 0) {
return resolve();
}
const metadataFile = files.find(f => f.name === 'metadata.json' || f.name.endsWith('/metadata.json'));
if (!metadataFile) {
showToast('No metadata.json found in selected files', 'error');
return resolve();
}
// Parse metadata.json
let app;
try {
const metadataText = new TextDecoder().decode(new Uint8Array(metadataFile.data));
app = JSON.parse(metadataText);
} catch(err) {
showToast('Failed to parse metadata.json: ' + err, 'error');
return resolve();
}
if (!app.id || !app.storage || !Array.isArray(app.storage)) {
showToast('Invalid metadata.json', 'error');
return resolve();
}
// Build file map for lookup (both simple filename and full path)
const fileMap = {};
files.forEach(f => {
const simpleName = f.name.split('/').pop();
fileMap[simpleName] = f;
fileMap[f.name] = f;
});
// Populate content directly into storage entries so AppInfo.getFiles doesn't fetch URLs
app.storage.forEach(storageEntry => {
const fileName = storageEntry.url || storageEntry.name;
const file = fileMap[fileName];
if (file) {
const data = new Uint8Array(file.data);
let content = "";
for (let i = 0; i < data.length; i++) {
content += String.fromCharCode(data[i]);
}
storageEntry.content = content;
}
});
// Populate content into data entries as well
if (app.data && Array.isArray(app.data)) {
app.data.forEach(dataEntry => {
if (dataEntry.content) return; // already has inline content
const fileName = dataEntry.url || dataEntry.name;
const file = fileMap[fileName];
if (file) {
const data = new Uint8Array(file.data);
let content = "";
for (let i = 0; i < data.length; i++) {
content += String.fromCharCode(data[i]);
}
dataEntry.content = content;
}
});
}
showPrompt("Install App from Files",
`Install "${app.name}" (${app.id}) v${app.version}?\n\nThis will delete the existing version if installed.`
).then(() => {
// Use standard updateApp flow (remove old, check deps, upload new)
return getInstalledApps().then(() => {
const isInstalled = device.appsInstalled.some(i => i.id === app.id);
// If installed, use update flow; otherwise use install flow
const uploadPromise = isInstalled
? Comms.getAppInfo(app).then(remove => {
return Comms.removeApp(remove, {containsFileList:true});
}).then(() => {
device.appsInstalled = device.appsInstalled.filter(a => a.id != app.id);
return checkDependencies(app, {checkForClashes:false});
})
: checkDependencies(app);
return uploadPromise.then(() => {
return Comms.uploadApp(app, {
device: device,
language: LANGUAGE
});
}).then((appJSON) => {
if (appJSON) device.appsInstalled.push(appJSON);
showToast(`"${app.name}" installed!`, 'success');
refreshMyApps();
refreshLibrary();
});
});
}).then(resolve).catch(err => {
showToast('Install failed: ' + err, 'error');
console.error(err);
resolve();
});
}
// Attach UI handler to the button on window load
window.addEventListener('load', (event) => {
const btn = document.getElementById("installappfromfiles");
if (!btn) return;
btn.addEventListener("click", () => {
startOperation({name:"Install App from Files"}, installFromFiles);
});
});