@kubohiroya/turbowarp-plus-editor-project-folder 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,8 +11,8 @@ export interface BrowserProject {
11
11
  id: string;
12
12
  title: string;
13
13
  /**
14
- * A folder inside the stored folder, `''` for the stored folder itself. Only for compatibility: the Editor opens the
15
- * project at the root of the stored folder, so an entry with another path is not a project folder there.
14
+ * A folder inside the stored folder, `''` for the stored folder itself. Only for entries kept by an earlier version:
15
+ * the Editor opens the project at the root of the stored folder, and `moveProjectToRoot` moves such a project there.
16
16
  */
17
17
  projectPath: string;
18
18
  createdAt: number;
@@ -35,6 +35,12 @@ export interface BrowserProjects {
35
35
  root(id: string): Promise<FileSystemDirectoryHandle | null>;
36
36
  /** The folder `projectPath` inside the entry's stored folder. Opening uses `root` instead. */
37
37
  directory(id: string): Promise<FileSystemDirectoryHandle | null>;
38
+ /**
39
+ * Moves the project of an entry kept by an earlier version, in the folder `projectPath`, to the root of the stored
40
+ * folder, where the Editor opens it, and records that. Nothing moves when a name in that folder is already taken
41
+ * at the root. Resolves with whether the project is at the root.
42
+ */
43
+ moveProjectToRoot(id: string): Promise<boolean>;
38
44
  /** The entry that holds `handle`, or `null` when it is not in the browser's storage. */
39
45
  find(handle: FileSystemHandle): Promise<BrowserProject | null>;
40
46
  touch(id: string): Promise<void>;
package/dist/index.js CHANGED
@@ -1041,6 +1041,40 @@ async function writeText(directory, name, text) {
1041
1041
  throw error;
1042
1042
  }
1043
1043
  }
1044
+ var manifestFiles = ["project.source.json", "sb3-source.json"];
1045
+ async function copyEntry(entry, target) {
1046
+ if (entry.kind === "file") {
1047
+ const file = await entry.getFile();
1048
+ const writable = await (await target.getFileHandle(entry.name, { create: true })).createWritable();
1049
+ try {
1050
+ await writable.write(file);
1051
+ await writable.close();
1052
+ } catch (error) {
1053
+ await writable.abort().catch(() => {});
1054
+ throw error;
1055
+ }
1056
+ return;
1057
+ }
1058
+ const directory = await target.getDirectoryHandle(entry.name, { create: true });
1059
+ var _iteratorAbruptCompletion = false;
1060
+ var _didIteratorError = false;
1061
+ var _iteratorError;
1062
+ try {
1063
+ for (var _iterator = _asyncIterator(entry.values()), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) {
1064
+ const child = _step.value;
1065
+ await copyEntry(child, directory);
1066
+ }
1067
+ } catch (err) {
1068
+ _didIteratorError = true;
1069
+ _iteratorError = err;
1070
+ } finally {
1071
+ try {
1072
+ if (_iteratorAbruptCompletion && _iterator.return != null) await _iterator.return();
1073
+ } finally {
1074
+ if (_didIteratorError) throw _iteratorError;
1075
+ }
1076
+ }
1077
+ }
1044
1078
  async function resolvePath(root, path) {
1045
1079
  let directory = root;
1046
1080
  for (const part of path ? path.split("/") : []) directory = await directory.getDirectoryHandle(part);
@@ -1101,12 +1135,12 @@ function openBrowserProjects(view) {
1101
1135
  async list() {
1102
1136
  const { info } = await folders();
1103
1137
  const found = [];
1104
- var _iteratorAbruptCompletion = false;
1105
- var _didIteratorError = false;
1106
- var _iteratorError;
1138
+ var _iteratorAbruptCompletion2 = false;
1139
+ var _didIteratorError2 = false;
1140
+ var _iteratorError2;
1107
1141
  try {
1108
- for (var _iterator = _asyncIterator(info.values()), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) {
1109
- const entry = _step.value;
1142
+ for (var _iterator2 = _asyncIterator(info.values()), _step2; _iteratorAbruptCompletion2 = !(_step2 = await _iterator2.next()).done; _iteratorAbruptCompletion2 = false) {
1143
+ const entry = _step2.value;
1110
1144
  {
1111
1145
  const id = entry.kind === "file" && entry.name.endsWith(".json") ? entry.name.slice(0, -5) : "";
1112
1146
  if (!idPattern.test(id)) continue;
@@ -1115,13 +1149,13 @@ function openBrowserProjects(view) {
1115
1149
  }
1116
1150
  }
1117
1151
  } catch (err) {
1118
- _didIteratorError = true;
1119
- _iteratorError = err;
1152
+ _didIteratorError2 = true;
1153
+ _iteratorError2 = err;
1120
1154
  } finally {
1121
1155
  try {
1122
- if (_iteratorAbruptCompletion && _iterator.return != null) await _iterator.return();
1156
+ if (_iteratorAbruptCompletion2 && _iterator2.return != null) await _iterator2.return();
1123
1157
  } finally {
1124
- if (_didIteratorError) throw _iteratorError;
1158
+ if (_didIteratorError2) throw _iteratorError2;
1125
1159
  }
1126
1160
  }
1127
1161
  return found.sort((a, b) => b.updatedAt - a.updatedAt || a.title.localeCompare(b.title));
@@ -1159,6 +1193,64 @@ function openBrowserProjects(view) {
1159
1193
  throw error;
1160
1194
  }
1161
1195
  },
1196
+ moveProjectToRoot: (id) => exclusive(id, async () => {
1197
+ const project = await readInfo(id);
1198
+ const top = project && await root(id);
1199
+ if (!project || !top) return false;
1200
+ if (!project.projectPath) return true;
1201
+ const parts = project.projectPath.split("/");
1202
+ let source;
1203
+ try {
1204
+ source = await resolvePath(top, project.projectPath);
1205
+ } catch (error) {
1206
+ if (missing(error)) return false;
1207
+ throw error;
1208
+ }
1209
+ const taken = /* @__PURE__ */ new Set();
1210
+ var _iteratorAbruptCompletion3 = false;
1211
+ var _didIteratorError3 = false;
1212
+ var _iteratorError3;
1213
+ try {
1214
+ for (var _iterator3 = _asyncIterator(top.values()), _step3; _iteratorAbruptCompletion3 = !(_step3 = await _iterator3.next()).done; _iteratorAbruptCompletion3 = false) {
1215
+ const entry = _step3.value;
1216
+ taken.add(entry.name);
1217
+ }
1218
+ } catch (err) {
1219
+ _didIteratorError3 = true;
1220
+ _iteratorError3 = err;
1221
+ } finally {
1222
+ try {
1223
+ if (_iteratorAbruptCompletion3 && _iterator3.return != null) await _iterator3.return();
1224
+ } finally {
1225
+ if (_didIteratorError3) throw _iteratorError3;
1226
+ }
1227
+ }
1228
+ const entries = [];
1229
+ var _iteratorAbruptCompletion4 = false;
1230
+ var _didIteratorError4 = false;
1231
+ var _iteratorError4;
1232
+ try {
1233
+ for (var _iterator4 = _asyncIterator(source.values()), _step4; _iteratorAbruptCompletion4 = !(_step4 = await _iterator4.next()).done; _iteratorAbruptCompletion4 = false) {
1234
+ const entry = _step4.value;
1235
+ entries.push(entry);
1236
+ }
1237
+ } catch (err) {
1238
+ _didIteratorError4 = true;
1239
+ _iteratorError4 = err;
1240
+ } finally {
1241
+ try {
1242
+ if (_iteratorAbruptCompletion4 && _iterator4.return != null) await _iterator4.return();
1243
+ } finally {
1244
+ if (_didIteratorError4) throw _iteratorError4;
1245
+ }
1246
+ }
1247
+ if (entries.some((entry) => taken.has(entry.name))) return false;
1248
+ entries.sort((a, b) => Number(manifestFiles.includes(a.name)) - Number(manifestFiles.includes(b.name)));
1249
+ for (const entry of entries) await copyEntry(entry, top);
1250
+ await (await resolvePath(top, parts.slice(0, -1).join("/"))).removeEntry(parts[parts.length - 1], { recursive: true });
1251
+ await writeInfo(_objectSpread2(_objectSpread2({}, project), {}, { projectPath: "" }));
1252
+ return true;
1253
+ }),
1162
1254
  async find(handle) {
1163
1255
  const path = await (await folders()).projects.resolve(handle).catch(() => null);
1164
1256
  return (path === null || path === void 0 ? void 0 : path.length) && idPattern.test(path[0]) ? readInfo(path[0]) : null;
@@ -1613,6 +1705,7 @@ function chooseBrowserProject({ document, projects, locale = "en", purpose, titl
1613
1705
  return element;
1614
1706
  };
1615
1707
  item.append(text, button(message.open, async () => {
1708
+ await projects.moveProjectToRoot(project.id);
1616
1709
  const directory = await projects.root(project.id);
1617
1710
  if (!directory) return render();
1618
1711
  finish({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-project-folder",
3
- "version": "0.19.1",
3
+ "version": "0.20.0",
4
4
  "description": "Read and save expanded Scratch project folders in TurboWarp Plus Editor",
5
5
  "type": "module",
6
6
  "license": "MPL-2.0",
@@ -222,8 +222,10 @@ export function chooseBrowserProject({document, projects, locale = 'en', purpose
222
222
  return element;
223
223
  };
224
224
  item.append(text,
225
- // The project is at the root of the stored folder, as on the device.
225
+ // The project is at the root of the stored folder, as on the device. A folder kept by an earlier
226
+ // version, with its project in project/, moves it there first.
226
227
  button(message.open, async () => {
228
+ await projects.moveProjectToRoot(project.id);
227
229
  const directory = await projects.root(project.id);
228
230
  if (!directory) return render();
229
231
  finish({project, directory});
@@ -12,8 +12,8 @@ export interface BrowserProject {
12
12
  id: string;
13
13
  title: string;
14
14
  /**
15
- * A folder inside the stored folder, `''` for the stored folder itself. Only for compatibility: the Editor opens the
16
- * project at the root of the stored folder, so an entry with another path is not a project folder there.
15
+ * A folder inside the stored folder, `''` for the stored folder itself. Only for entries kept by an earlier version:
16
+ * the Editor opens the project at the root of the stored folder, and `moveProjectToRoot` moves such a project there.
17
17
  */
18
18
  projectPath: string;
19
19
  createdAt: number;
@@ -34,6 +34,12 @@ export interface BrowserProjects {
34
34
  root(id: string): Promise<FileSystemDirectoryHandle | null>;
35
35
  /** The folder `projectPath` inside the entry's stored folder. Opening uses `root` instead. */
36
36
  directory(id: string): Promise<FileSystemDirectoryHandle | null>;
37
+ /**
38
+ * Moves the project of an entry kept by an earlier version, in the folder `projectPath`, to the root of the stored
39
+ * folder, where the Editor opens it, and records that. Nothing moves when a name in that folder is already taken
40
+ * at the root. Resolves with whether the project is at the root.
41
+ */
42
+ moveProjectToRoot(id: string): Promise<boolean>;
37
43
  /** The entry that holds `handle`, or `null` when it is not in the browser's storage. */
38
44
  find(handle: FileSystemHandle): Promise<BrowserProject | null>;
39
45
  touch(id: string): Promise<void>;
@@ -90,6 +96,26 @@ async function writeText(directory: FileSystemDirectoryHandle, name: string, tex
90
96
  }
91
97
  }
92
98
 
99
+ // The files that make a folder a project folder go last, so a copy cut short is no project folder.
100
+ const manifestFiles = ['project.source.json', 'sb3-source.json'];
101
+
102
+ async function copyEntry(entry: FileSystemHandle, target: FileSystemDirectoryHandle): Promise<void> {
103
+ if (entry.kind === 'file') {
104
+ const file = await (entry as FileSystemFileHandle).getFile();
105
+ const writable = await (await target.getFileHandle(entry.name, {create: true})).createWritable();
106
+ try {
107
+ await writable.write(file);
108
+ await writable.close();
109
+ } catch (error) {
110
+ await writable.abort().catch(() => {});
111
+ throw error;
112
+ }
113
+ return;
114
+ }
115
+ const directory = await target.getDirectoryHandle(entry.name, {create: true});
116
+ for await (const child of (entry as IterableDirectory).values()) await copyEntry(child, directory);
117
+ }
118
+
93
119
  async function resolvePath(root: FileSystemDirectoryHandle, path: string) {
94
120
  let directory = root;
95
121
  for (const part of path ? path.split('/') : []) directory = await directory.getDirectoryHandle(part);
@@ -179,6 +205,33 @@ export function openBrowserProjects(view: Window): BrowserProjects | null {
179
205
  throw error;
180
206
  }
181
207
  },
208
+ moveProjectToRoot: id => exclusive(id, async () => {
209
+ const project = await readInfo(id);
210
+ const top = project && await root(id);
211
+ if (!project || !top) return false;
212
+ if (!project.projectPath) return true;
213
+ const parts = project.projectPath.split('/');
214
+ let source: FileSystemDirectoryHandle;
215
+ try {
216
+ source = await resolvePath(top, project.projectPath);
217
+ } catch (error) {
218
+ if (missing(error)) return false;
219
+ throw error;
220
+ }
221
+ // `project/` itself is a name at the root, so a project holding a `project` entry stays where it is.
222
+ const taken = new Set<string>();
223
+ for await (const entry of (top as IterableDirectory).values()) taken.add(entry.name);
224
+ const entries: FileSystemHandle[] = [];
225
+ for await (const entry of (source as IterableDirectory).values()) entries.push(entry);
226
+ if (entries.some(entry => taken.has(entry.name))) return false;
227
+ entries.sort((a, b) => Number(manifestFiles.includes(a.name)) - Number(manifestFiles.includes(b.name)));
228
+ for (const entry of entries) await copyEntry(entry, top);
229
+ // The old folder goes only after the copy is complete.
230
+ const parent = await resolvePath(top, parts.slice(0, -1).join('/'));
231
+ await parent.removeEntry(parts[parts.length - 1], {recursive: true});
232
+ await writeInfo({...project, projectPath: ''});
233
+ return true;
234
+ }),
182
235
  async find(handle) {
183
236
  const path = await (await folders()).projects.resolve(handle).catch(() => null);
184
237
  return path?.length && idPattern.test(path[0]) ? readInfo(path[0]) : null;