Skip to content

Commit f91c4ed

Browse files
authored
Update lib_class.gs
1 parent 2e34bd1 commit f91c4ed

File tree

1 file changed

+127
-4
lines changed

1 file changed

+127
-4
lines changed

src/lib_class.gs

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* ScriptSync Library
3-
* @version 2.0.5
3+
* @version 2.0.6
44
* @description This script performs an update,
55
* adding new files from the template project
66
* to the current user script.
@@ -184,11 +184,11 @@ class ScriptSync {
184184
_compareFilesByContent(fn1, fn2, compare_to) {
185185
let file1, file2;
186186
switch (compare_to) {
187-
case 'script':
187+
case TARGET.SCRIPT:
188188
file1 = this.user_json_file.files.find(file => file.name === fn1);
189189
file2 = this.user_json_file.files.find(file => file.name === fn2);
190190
break;
191-
case 'template':
191+
case TARGET.TEMPLATE:
192192
file1 = this.template_json_file.files.find(file => file.name === fn1);
193193
file2 = this.template_json_file.files.find(file => file.name === fn2);
194194
break;
@@ -399,6 +399,97 @@ class ScriptSync {
399399
return this;
400400
}
401401

402+
// *** LIBRARIES ***
403+
/**
404+
* Retrieves information about a library based on its ID or user-defined name.
405+
* @param {string} target - The target environment, either 'script' or 'template'.
406+
* This determines whether to fetch information from the
407+
* script or the template file.
408+
* @param {string} [libraryId] - The unique identifier of the library to retrieve.
409+
* If provided, the function will search for a library with
410+
* this ID.
411+
* @param {string} [libraryName] - The user-defined name (userSymbol) of the library to retrieve.
412+
* If provided, the function will search for a library with
413+
* this user-defined name.
414+
* @returns {LibraryInfo|null} - Returns an object containing information about the library
415+
* if found, or null if the library is not found.
416+
*/
417+
_getLibraryInfo(target, libraryId, libraryName) {
418+
if (target !== TARGET.TEMPLATE && target !== TARGET.SCRIPT) throw new Error("Target is not valid.");
419+
if (libraryId === '' && libraryName === '') throw new Error("Value of the library is not defined.");
420+
let library;
421+
422+
let appsscript_json = this._jsonGetFileFromScript(target, "appsscript", true, false);
423+
424+
if (appsscript_json !== null) {
425+
try {
426+
appsscript_json = JSON.parse(appsscript_json);
427+
library = appsscript_json.dependencies?.libraries?.find(lib => {
428+
return (
429+
lib.libraryId === libraryId
430+
|| lib.userSymbol === libraryName
431+
);
432+
});
433+
} catch (e) {
434+
LE(e.message);
435+
}
436+
}
437+
438+
return library || null;
439+
}
440+
441+
/**
442+
* Update library information.
443+
* @param {LibraryInfo} libraryInfo - A structured JSON object: `{ userSymbol: 'string',
444+
* libraryId: 'string', version: 'number',
445+
* developmentMode: boolean }`.\
446+
* Requered parameter. It will be used to update
447+
* the library information.
448+
* @param {string} [libraryId] - The unique ID of the library.\
449+
* Optional parameter. If this parameter is provided,
450+
* it will be used to identify the library in the update process.
451+
* @param {string} [libraryName] - User-defined library name (userSymbol).\
452+
* Optional parameter. If 'libraryId' is not provided,
453+
* and this parameter is present, it will be used
454+
* to identify the library in the update process.
455+
* @returns {ScriptSync} Returns an instance of ScriptSync for method chaining.
456+
*/
457+
_updateLibraryInfo(libraryInfo, libraryId, libraryName) {
458+
if(!libraryInfo) throw new Error("LibraryInfo is not defined.");
459+
if (!libraryId && !libraryName) {
460+
libraryId = libraryInfo.libraryId || "";
461+
}
462+
463+
const fn = "appsscript";
464+
465+
var data = this.user_json_file.files.find(file => {
466+
return file.name === fn
467+
});
468+
469+
if (data) {
470+
try {
471+
var appsscript_json = JSON.parse(data.source);
472+
var library = appsscript_json.dependencies?.libraries?.find(lib => {
473+
return (
474+
lib.libraryId === libraryId
475+
|| lib.userSymbol === libraryName
476+
);
477+
});
478+
if (library) {
479+
for (let key in libraryInfo) library[key] = libraryInfo[key];
480+
data.source = JSON.stringify(appsscript_json, null, 2);
481+
this.changes.push(`File '${fn}' will be updated.`);
482+
L("File '%s' will be updated", fn);
483+
}
484+
} catch (e) {
485+
this.result = false;
486+
LE(e.message);
487+
}
488+
}
489+
490+
return this;
491+
}
492+
402493
// *** ADVANCED ***
403494
/**
404495
* Set whole the source a file in the user script object.
@@ -475,6 +566,33 @@ class ScriptSync {
475566
// ► │ ══ PRIVATE ══ │
476567
// ► ╘════════════════╛
477568

569+
/**
570+
* @private
571+
* @returns {string}
572+
*/
573+
_jsonGetFileFromScript(target, fn, sourceOnly=true, throwWithError=false ) {
574+
if (!fn) throw new Error("File name is not defined.");
575+
var data;
576+
577+
if (target === TARGET.TEMPLATE) {
578+
data = this.template_json_file.files.find(file => {
579+
return file.name === fn
580+
});
581+
}
582+
else if (target === TARGET.SCRIPT) {
583+
data = this.user_json_file.files.find(file => {
584+
return file.name === fn
585+
});
586+
}
587+
588+
if (!data && throwWithError) {
589+
throw new Error(`No such file name in the template script: '${fn}'.`);
590+
}
591+
592+
return sourceOnly ? data?.source || null : data || null;
593+
}
594+
595+
478596
/**
479597
* Makes a request to a server.
480598
* @private
@@ -521,6 +639,12 @@ class ScriptSync {
521639
}
522640
}
523641

642+
// for libabries switch
643+
var TARGET = {
644+
SCRIPT: 'script',
645+
TEMPLATE: 'template'
646+
}
647+
524648
// for log changes
525649
const fileTypes = new Map([
526650
["server_js", "gs"],
@@ -538,4 +662,3 @@ const dependencies = {
538662
},
539663
]
540664
}
541-

0 commit comments

Comments
 (0)