-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFiles.js
More file actions
536 lines (475 loc) · 15.7 KB
/
Files.js
File metadata and controls
536 lines (475 loc) · 15.7 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: copy;
const { JS_EXTENSION } = importModule("Constants");
/**
* Helper class.
* Used to simplify work with the Scriptable file system.
*
* Files only operates in iCloud; no changes to the local
* device file system are being made.
* * @class Files
*/
class Files {
/**
* Internal iCloud File Manager instance.
* @type {FileManager}
* @private
*/
static #manager = FileManager.iCloud();
/**
* Name of the directory where feature configurations are stored.
* @type {string}
*/
static FeaturesDirectory = "Features";
/**
* Name of the directory where script resources are stored.
* @type {string}
*/
static ResourcesDirectory = "Resources";
/**
* Name of the directory where localization files are stored.
* @type {string}
*/
static LocalesDirectory = "i18n";
/**
* Default filename for feature configuration files.
* @type {string}
* @private
*/
static #FEATURE_FILE_NAME = "feature.json";
/**
* Returns the active file manager instance.
* @returns {FileManager} The iCloud file manager.
*/
static manager() {
return this.#manager;
}
/**
* Returns the root documents directory path.
* @returns {string} Path to documents.
*/
static rootDirectory() {
return this.#manager.documentsDirectory();
}
/**
* Moves a file from the source path to the destination path.
* If a file already exists at the destination, it is deleted
* before the move operation to prevent errors.
*
* @static
* @param {string} sourcePath - The current full path of the file.
* @param {string} destinationPath - The target full path for the file.
* @memberof Files
*/
static forceMove(sourcePath, destinationPath) {
if (this.#manager.fileExists(destinationPath)) {
this.#manager.remove(destinationPath);
}
this.#manager.move(sourcePath, destinationPath);
}
/**
* Writes raw content to a specific file path.
* @param {string} filePath - Full path to the file.
* @param {string} content - String content to write.
*/
static updateScriptableFile(filePath, content) {
this.#manager.write(filePath, Data.fromString(content));
}
/**
* Used to update features object for provided script.
*
* @static
* @param {String} scriptName - Name of script.
* @param {Object} content - Features object to stringify.
* @memberof Files
*/
static async updateFeatureFile(scriptName, content) {
await this.#updateFileInternal(
scriptName,
this.#FEATURE_FILE_NAME,
JSON.stringify(content, null, 4),
this.getFeaturesDirectory()
);
}
/**
* Used to update Scriptable script content.
*
* @static
* @param {String} scriptName - Name of the script file.
* @param {String} script - The JavaScript source code.
* @memberof Files
*/
static async updateScript(scriptName, script) {
const scriptPath = this.joinPaths(
this.getScriptableDirectory(),
scriptName
);
await this.#manager.write(scriptPath, Data.fromString(script));
}
/**
* Used to update locale file associated with the script and provided language code.
*
* @static
* @param {String} scriptName - Script name for which locale is being updated.
* @param {String} languageCode - Language code (e.g., 'en_US').
* @param {Object} content - Locale object content.
* @memberof Files
*/
static async updateLocale(scriptName, languageCode, content) {
await this.#updateFileInternal(
scriptName,
this.#getLocaleFileName(languageCode),
JSON.stringify(content, null, 4),
this.getLocalesDirectory()
);
}
/**
* Used to update JSON resource associated with the current running script.
*
* @static
* @param {String} fileName - Name of resource file.
* @param {Object} content - JSON object to save.
* @memberof Files
*/
static async updateLocalJson(fileName, content) {
await this.updateJson(Script.name(), fileName, content);
}
/**
* Used to update JSON resource associated with a specific script.
*
* @static
* @param {String} scriptName - Name of script for which file is updated.
* @param {String} fileName - Name of resource file.
* @param {Object} content - JSON object to save.
* @memberof Files
*/
static async updateJson(scriptName, fileName, content) {
await this.updateFile(scriptName, fileName, JSON.stringify(content, null, 4));
}
/**
* Used to update file resource associated with the current script.
*
* @static
* @param {String} fileName - Name of resource file.
* @param {string} content - File content.
* @memberof Files
*/
static async updateLocalFile(fileName, content) {
await this.updateFile(Script.name(), fileName, content);
}
/**
* Used to update file resource associated with a provided script.
*
* @static
* @param {String} scriptName - Name of script for which file is updated.
* @param {String} fileName - Name of resource file.
* @param {string} content - File content.
* @memberof Files
*/
static async updateFile(scriptName, fileName, content) {
await this.#updateFileInternal(scriptName, fileName, content, this.getResourcesDirectory());
}
/**
* Internal logic for handling directory creation and file writing.
*
* @static
* @private
* @param {String} scriptName - Name of script directory.
* @param {String} fileName - File name to write.
* @param {string} content - Raw content.
* @param {String} directory - Base directory path.
*/
static async #updateFileInternal(scriptName, fileName, content, directory) {
const targetDirectory = this.joinPaths(directory, scriptName);
if (!this.#manager.isDirectory(targetDirectory)) {
this.#manager.createDirectory(targetDirectory, true);
}
const targetFile = this.joinPaths(targetDirectory, fileName);
await this.#manager.write(targetFile, Data.fromString(String(content)));
}
/**
* Checks whether feature file exists for provided script.
*
* @static
* @param {String} scriptName - Name of script.
* @return {Boolean} True if feature file exists.
* @memberof Files
*/
static featureFileExists(scriptName) {
return this.#fileExistsInternal(
scriptName, this.#FEATURE_FILE_NAME, this.getFeaturesDirectory()
);
}
/**
* Checks whether script has locale with provided language code.
*
* @static
* @param {String} scriptName - Name of script.
* @param {String} languageCode - Language code.
* @return {Boolean} True if locale exists.
* @memberof Files
*/
static localeExists(scriptName, languageCode) {
return this.#fileExistsInternal(
scriptName,
this.#getLocaleFileName(languageCode),
this.getLocalesDirectory()
);
}
/**
* Checks whether script has resource file with provided name.
*
* @static
* @param {String} scriptName - Name of script.
* @param {String} fileName - Name of file.
* @return {Boolean} True if file exists.
* @memberof Files
*/
static fileExists(scriptName, fileName) {
return this.#fileExistsInternal(scriptName, fileName, this.getResourcesDirectory());
}
/**
* Internal existence check logic.
* @private
*/
static #fileExistsInternal(scriptName, fileName, directory) {
const targetFile = this.joinPaths(directory, scriptName, fileName);
return this.#manager.fileExists(targetFile);
}
/**
* Reads a string from a specific file path.
* @param {string} filePath - Path to the file.
* @returns {string} File content.
*/
static readScriptableFile(filePath) {
return this.#manager.readString(filePath);
}
/**
* Reads feature file for the current script.
* @returns {Object} Feature configuration object.
* @memberof Files
*/
static readLocalFeatureFile() {
return this.readFeatureFile(Script.name());
}
/**
* Reads feature file for a specific script.
* @param {String} scriptName - Name of script.
* @returns {Object} Feature configuration object.
* @memberof Files
*/
static readFeatureFile(scriptName) {
const defaultValue = {
__debug: {
__enabled: false
}
};
const content = this.#readFileInternal(
scriptName,
this.#FEATURE_FILE_NAME,
defaultValue,
this.getFeaturesDirectory()
);
return this.#toJSON(content);
}
/**
* Reads content of a Scriptable script.
* @param {String} scriptName - Name of script.
* @returns {String} script source.
* @memberof Files
*/
static readScript(scriptName) {
const scriptPath = this.joinPaths(this.getScriptableDirectory(), scriptName);
return this.#manager.readString(scriptPath);
}
/**
* Reads locale object for a script and language code.
* @param {String} scriptName - Name of script.
* @param {String} languageCode - Language code.
* @returns {Object} Locale object or empty object if not found.
* @memberof Files
*/
static readLocale(scriptName, languageCode) {
const content = this.#readFileInternal(
scriptName,
this.#getLocaleFileName(languageCode),
{},
this.getLocalesDirectory()
);
return this.#toJSON(content);
}
/**
* Reads JSON file associated with current script.
* @param {String} fileName - Name of JSON file.
* @param {Object} defaultValue - Fallback value.
* @returns {Object} Parsed JSON or default.
* @memberof Files
*/
static readLocalJson(fileName, defaultValue) {
return this.readJson(Script.name(), fileName, defaultValue);
}
/**
* Reads JSON file associated with provided script.
* @param {String} scriptName - script name.
* @param {String} fileName - Name of JSON file.
* @param {Object} defaultValue - Fallback value.
* @returns {Object} Parsed JSON or default.
* @memberof Files
*/
static readJson(scriptName, fileName, defaultValue) {
const content = this.readFile(scriptName, fileName, defaultValue);
return this.#toJSON(content);
}
/**
* Reads resource file associated with current script.
* @param {String} fileName - file name.
* @param {any} defaultValue - Fallback value.
* @returns {string|any} content or default value.
* @memberof Files
*/
static readLocalFile(fileName, defaultValue) {
return this.readFile(Script.name(), fileName, defaultValue);
}
/**
* Reads resource file associated with provided script.
* @param {String} scriptName - script name.
* @param {String} fileName - file name.
* @param {any} defaultValue - Fallback value.
* @returns {string|any} content or default value.
* @memberof Files
*/
static readFile(scriptName, fileName, defaultValue) {
return this.#readFileInternal(scriptName, fileName, defaultValue, this.getResourcesDirectory());
}
/**
* Internal read logic.
* @private
*/
static #readFileInternal(scriptName, fileName, defaultValue, directory) {
const targetFile = this.joinPaths(directory, scriptName, fileName);
if (!this.#manager.fileExists(targetFile)) {
console.warn(`File does not exist: ${targetFile}`);
return defaultValue;
}
return this.#manager.readString(targetFile);
}
/**
* List all directories within the Features folder.
* @returns {string[]} List of directory names.
*/
static findFeatureDirectories() {
return this.#manager.listContents(this.getFeaturesDirectory());
}
/**
* List all directories within the Locales folder.
* @returns {string[]} List of directory names.
*/
static findLocaleDirectories() {
return this.#manager.listContents(this.getLocalesDirectory());
}
/**
* Finds scripts in a directory filtering by JS extension.
* @param {string} [targetDirectory=null] - Directory to search. Defaults to Scriptable root.
* @returns {string[]} List of script filenames.
*/
static findScripts(targetDirectory = null) {
if (targetDirectory === null) {
targetDirectory = this.getScriptableDirectory();
}
return this.#manager.listContents(targetDirectory)
.filter((script) => script.endsWith(JS_EXTENSION));
}
/**
* Resolves the full path for a resource in the current script's folder.
* @param {String} fileName - Resource name.
* @returns {String} Full path.
*/
static resolveLocalResource(fileName) {
return this.resolveResource(Script.name(), fileName);
}
/**
* Resolves the full path for a resource in a specific script's folder.
* @param {String} scriptName - Script directory name.
* @param {String} fileName - Resource name.
* @returns {String} Full path.
*/
static resolveResource(scriptName, fileName) {
return this.joinPaths(
this.getResourcesDirectory(),
scriptName,
fileName
);
}
/**
* Returns the full path to the Features directory.
* @returns {String} Path.
*/
static getFeaturesDirectory() {
return this.joinPaths(
this.getScriptableDirectory(),
this.FeaturesDirectory
);
}
/**
* Returns the full path to the Resources directory.
* @returns {String} Path.
*/
static getResourcesDirectory() {
return this.joinPaths(
this.getScriptableDirectory(),
this.ResourcesDirectory
);
}
/**
* Returns the full path to the Locales directory.
* @returns {String} Path.
*/
static getLocalesDirectory() {
return this.joinPaths(
this.getScriptableDirectory(),
this.LocalesDirectory
);
}
/**
* Returns the root Scriptable documents directory.
* @returns {String} Path.
*/
static getScriptableDirectory() {
return this.#manager.documentsDirectory();
}
/**
* Combines multiple path segments into a single path string.
* @param {...string} segments - Path parts to join.
* @returns {String} Full joined path.
*/
static joinPaths(...segments) {
let filePath = "";
for (const segment of segments) {
filePath = this.#manager.joinPath(filePath, segment);
}
return filePath;
}
/**
* Parses content into JSON if it is a string.
* @private
*/
static #toJSON(content) {
if (typeof content === 'string') {
return JSON.parse(content);
}
return content;
}
/**
* Generates the filename for a locale JSON.
* @private
* @param {String} languageCode - e.g., 'en'.
* @returns {string} e.g., 'locale_en.json'.
*/
static #getLocaleFileName(languageCode) {
return `locale_${languageCode}.json`;
}
}
module.exports = {
Files
};