Skip to content

Commit 04fe42c

Browse files
feat. accept arrayBuffer or string as input in writeFile
1 parent 6eef661 commit 04fe42c

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

src/fileSystem/internalFs.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,23 @@ const internalFs = {
5858
* Instead, it must be possible for it to be created newly at call time. The default is true.
5959
* @returns {Promise<string>} URL where the file was written into.
6060
*/
61-
writeFile(filename, data, create = false, exclusive = true) {
61+
writeFile(filename, udata, create = false, exclusive = true) {
6262
exclusive = create ? exclusive : false;
6363
const name = filename.split("/").pop();
6464

6565
return new Promise((resolve, reject) => {
66+
if(udata === undefined || udata == null){
67+
reject("udata is null")
68+
}
69+
70+
let data
71+
if (udata instanceof ArrayBuffer || Object.prototype.toString.call(udata) === "[object ArrayBuffer]") {
72+
const decoder = new TextDecoder('utf-8');
73+
data = decoder.decode(udata);
74+
}else{
75+
data = udata
76+
}
77+
6678
reject = setMessage(reject);
6779
Filesystem.writeFile({
6880
path: filename,
@@ -93,8 +105,11 @@ const internalFs = {
93105
*/
94106

95107
delete(filename) {
108+
console.log("deletion skipped")
109+
return
96110
return new Promise((resolve, reject) => {
97111
console.log("Deleting " + filename);
112+
98113

99114
Filesystem.stat({ path: filename })
100115
.then((stats) => {
@@ -126,7 +141,6 @@ const internalFs = {
126141
reject = setMessage(reject);
127142
Filesystem.readFile({ path: filename, encoding: Encoding.UTF8 })
128143
.then((readFileResult) => {
129-
130144
const encoder = new TextEncoder();
131145
const buffer = encoder.encode(readFileResult.data).buffer;
132146

@@ -142,6 +156,24 @@ const internalFs = {
142156
});
143157
},
144158

159+
readStringFile(filename) {
160+
return new Promise((resolve, reject) => {
161+
reject = setMessage(reject);
162+
Filesystem.readFile({ path: filename, encoding: Encoding.UTF8 })
163+
.then((readFileResult) => {
164+
resolve({data : readFileResult.data})
165+
})
166+
.catch((error) => {
167+
console.error(
168+
`Failed to Read File contents of "${filename}", error: `,
169+
error,
170+
);
171+
reject(error);
172+
});
173+
});
174+
},
175+
176+
145177
/**
146178
* Rename a file or directory
147179
* @param {string} url
@@ -182,14 +214,14 @@ const internalFs = {
182214
reject = setMessage(reject);
183215
// TODO!: ask about `recursive` option
184216
Filesystem.mkdir({
185-
path: `${path}${dirname}`,
186-
recursive: false,
217+
path: `${path}/${dirname}`,
218+
recursive: true,
187219
})
188220
.then(() => {
189221
console.log(
190-
`Created "${dirname}" Directory successfully on path: ${path}`,
222+
`Created ${path}/${dirname}`,
191223
);
192-
Filesystem.stat({ path: `${path}${dirname}` })
224+
Filesystem.stat({ path: `${path}/${dirname}` })
193225
.then((stats) => resolve(stats.url))
194226
.catch(reject);
195227
})
@@ -475,6 +507,7 @@ function createFs(url) {
475507
return files;
476508
},
477509
async readFile(encoding) {
510+
console.log("fs read "+url)
478511
let { data } = await internalFs.readFile(url, encoding);
479512

480513
if (encoding) {

src/lib/acode.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export default class Acode {
179179
return;
180180
}
181181

182-
fsOperation(Url.join(PLUGIN_DIR, pluginId))
182+
fsOperation(`${PLUGIN_DIR}/${pluginId}`)
183183
.exists()
184184
.then((isPluginExists) => {
185185
if (isPluginExists) {
@@ -189,12 +189,9 @@ export default class Acode {
189189

190190
let purchaseToken;
191191
let product;
192-
const pluginUrl = Url.join(
193-
constants.API_BASE,
194-
`plugin/${pluginId}`,
195-
);
196-
fsOperation(pluginUrl)
197-
.readFile("json")
192+
const pluginUrl = `${PLUGIN_DIR}/${pluginId}`
193+
fsOperation(`${pluginUrl}/plugin.json`)
194+
.readFile()
198195
.catch(() => {
199196
reject(new Error("Failed to fetch plugin details"));
200197
return null;

src/lib/installPlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default async function installPlugin(
2929
purchaseToken,
3030
isDependency,
3131
) {
32+
console.log(`Installing ${name}`)
3233
if (!isDependency) {
3334
loaderDialog = loader.create(name || "Plugin", strings.installing, {
3435
timeout: 6000,
@@ -48,6 +49,7 @@ export default async function installPlugin(
4849
window.log("error", error);
4950
}
5051

52+
console.log("installing -------------------------")
5153
if (!/^(https?|file|content):/.test(id)) {
5254
pluginUrl = Url.join(
5355
constants.API_BASE,

src/lib/loadPlugin.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import fsOperation from "fileSystem";
33
import Url from "utils/Url";
44
import helpers from "utils/helpers";
55
import actionStack from "./actionStack";
6+
import internalFs from "fileSystem/internalFs";
67

78
export default async function loadPlugin(pluginId, justInstalled = false) {
8-
const baseUrl = await helpers.toInternalUri(Url.join(PLUGIN_DIR, pluginId));
9+
const baseUrl = Url.join(PLUGIN_DIR, pluginId)
10+
11+
console.log("Base url "+baseUrl)
12+
913
const cacheFile = Url.join(CACHE_STORAGE, pluginId);
1014

1115
const pluginJson = await fsOperation(
@@ -22,7 +26,17 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
2226
}
2327

2428
return new Promise((resolve, reject) => {
25-
const $script = <script src={mainUrl}></script>;
29+
30+
if(pluginId === undefined){
31+
console.error("Skiping loading plugin with undefined id")
32+
reject("Skiping loading plugin with undefined id")
33+
return
34+
}
35+
36+
37+
const data = internalFs.readStringFile(mainUrl).data
38+
39+
const $script = <script dangerouslySetInnerHTML={{ __html: data }} />;
2640

2741
$script.onerror = (error) => {
2842
reject(

src/lib/loadPlugins.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default async function loadPlugins(loadOnlyTheme = false) {
5252

5353
// Load plugins concurrently
5454
const loadPromises = pluginsToLoad.map(async (pluginDir) => {
55+
console.log("loading")
5556
const pluginId = Url.basename(pluginDir.url);
5657

5758
if (loadOnlyTheme && currentTheme) {
@@ -98,6 +99,8 @@ function isThemePlugin(pluginId) {
9899

99100
async function cleanupFailedPlugins(pluginIds) {
100101
for (const pluginId of pluginIds) {
102+
console.log("skiping delete "+pluginId)
103+
continue;
101104
try {
102105
const pluginDir = Url.join(PLUGIN_DIR, pluginId);
103106
if (await fsOperation(pluginDir).exists()) {

0 commit comments

Comments
 (0)