@promptbook/core 0.52.0-17 → 0.52.0-18

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.
package/umd/index.umd.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('moment')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'moment'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.moment));
5
- })(this, (function (exports, spaceTrim, prettier, parserHtml, moment) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('path'), require('moment')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'path', 'moment'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.path, global.moment));
5
+ })(this, (function (exports, spaceTrim, prettier, parserHtml, path, moment) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -454,7 +454,7 @@
454
454
  /**
455
455
  * The version of the Promptbook library
456
456
  */
457
- var PROMPTBOOK_VERSION = '0.52.0-16';
457
+ var PROMPTBOOK_VERSION = '0.52.0-17';
458
458
 
459
459
  /**
460
460
  * Parses the template and returns the list of all parameter names
@@ -3175,6 +3175,23 @@
3175
3175
  */
3176
3176
  new Function("\n try {\n if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {\n return true;\n } else {\n return false;\n }\n } catch (e) {\n return false;\n }\n");
3177
3177
 
3178
+ /**
3179
+ * Returns the same value that is passed as argument.
3180
+ * No side effects.
3181
+ *
3182
+ * Note: It can be usefull for leveling indentation
3183
+ *
3184
+ * @param value any values
3185
+ * @returns the same values
3186
+ */
3187
+ function just(value) {
3188
+ if (value === undefined) {
3189
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3190
+ return undefined;
3191
+ }
3192
+ return value;
3193
+ }
3194
+
3178
3195
  /**
3179
3196
  * This error indicates that promptbook not found in the library
3180
3197
  */
@@ -3258,6 +3275,9 @@
3258
3275
  var _this = this;
3259
3276
  var promptbook = this.library.get(url);
3260
3277
  if (!promptbook) {
3278
+ if (this.listPromptbooks().length === 0) {
3279
+ throw new PromptbookNotFoundError(spaceTrim.spaceTrim("\n Promptbook with url \"".concat(url, "\" not found\n\n No promptbooks available\n ")));
3280
+ }
3261
3281
  throw new PromptbookNotFoundError(spaceTrim.spaceTrim(function (block) { return "\n Promptbook with url \"".concat(url, "\" not found\n\n Available promptbooks:\n ").concat(block(_this.listPromptbooks()
3262
3282
  .map(function (promptbookUrl) { return "- ".concat(promptbookUrl); })
3263
3283
  .join('\n')), "\n\n "); }));
@@ -3413,39 +3433,96 @@
3413
3433
  * @param options - Misc options for the library
3414
3434
  * @returns PromptbookLibrary
3415
3435
  */
3416
- function createPromptbookLibraryFromDirectory(path, options) {
3436
+ function createPromptbookLibraryFromDirectory(path$1, options) {
3417
3437
  var _this = this;
3418
3438
  if (!isRunningInNode()) {
3419
3439
  throw new Error('Function `createPromptbookLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3420
3440
  }
3421
- var _a = (options || {}).isRecursive, isRecursive = _a === void 0 ? true : _a;
3441
+ var _a = options || {}, _b = _a.isRecursive, isRecursive = _b === void 0 ? true : _b, _c = _a.isVerbose, isVerbose = _c === void 0 ? false : _c;
3422
3442
  return createPromptbookLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3423
- return __generator(this, function (_a) {
3424
- /*
3425
- const justR_equire = require; /* <- Note: [1] * /
3426
- const readdir = justR_equire(just('fs/promises') /* <- Note: [2] * /).readdir as typeof readdirType;
3427
-
3428
- // TODO: !!!! Implement recursive reading
3429
- // TODO: !!!!! readAllFiles util
3430
-
3431
- const dirents = await readdir(path, {
3432
- withFileTypes: true /* Note: This is not working: recursive: isRecursive * /,
3433
- });
3434
- const fileNames = dirents.filter((dirent) => dirent.isFile()).map(({ name }) => join(name, path));
3435
-
3436
- // TODO: !!! Implement
3437
-
3438
- console.info('createPromptbookLibraryFromDirectory', { path, isRecursive, fileNames });
3439
- throw new Error('Not implemented yet');
3440
-
3441
- */
3442
- console.info('createPromptbookLibraryFromDirectory', { path: path, isRecursive: isRecursive });
3443
- return [2 /*return*/, []];
3443
+ var justR_equire, fsPromises, readdir, readFile, dirents, fileNames, promptbooks, fileNames_1, fileNames_1_1, fileName, promptbookString, promptbook, e_1_1;
3444
+ var e_1, _a;
3445
+ return __generator(this, function (_b) {
3446
+ switch (_b.label) {
3447
+ case 0:
3448
+ justR_equire = require;
3449
+ fsPromises = justR_equire(just('fs/promises') /* <- Note: [2] */);
3450
+ readdir = fsPromises.readdir;
3451
+ readFile = fsPromises.readFile;
3452
+ return [4 /*yield*/, readdir(path$1, {
3453
+ withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
3454
+ })];
3455
+ case 1:
3456
+ dirents = _b.sent();
3457
+ fileNames = dirents.filter(function (dirent) { return dirent.isFile(); }).map(function (_a) {
3458
+ var name = _a.name;
3459
+ return path.join(path$1, name);
3460
+ });
3461
+ if (isVerbose) {
3462
+ console.info('createPromptbookLibraryFromDirectory', { path: path$1, isRecursive: isRecursive, dirents: dirents, fileNames: fileNames });
3463
+ }
3464
+ promptbooks = [];
3465
+ _b.label = 2;
3466
+ case 2:
3467
+ _b.trys.push([2, 8, 9, 10]);
3468
+ fileNames_1 = __values(fileNames), fileNames_1_1 = fileNames_1.next();
3469
+ _b.label = 3;
3470
+ case 3:
3471
+ if (!!fileNames_1_1.done) return [3 /*break*/, 7];
3472
+ fileName = fileNames_1_1.value;
3473
+ if (!fileName.endsWith('.ptbk.md')) return [3 /*break*/, 5];
3474
+ return [4 /*yield*/, readFile(fileName, 'utf8')];
3475
+ case 4:
3476
+ promptbookString = (_b.sent());
3477
+ promptbook = promptbookStringToJson(promptbookString);
3478
+ if (!promptbook.promptbookUrl) {
3479
+ if (isVerbose) {
3480
+ console.info("Not loading ".concat(fileName, " - missing URL"));
3481
+ }
3482
+ }
3483
+ else {
3484
+ if (isVerbose) {
3485
+ console.info("Loading ".concat(fileName));
3486
+ }
3487
+ // TODO: !!!! Chack url uniqueness and collisions
3488
+ promptbooks.push(promptbook);
3489
+ }
3490
+ return [3 /*break*/, 6];
3491
+ case 5:
3492
+ if (fileName.endsWith('.ptbk.json')) {
3493
+ if (isVerbose) {
3494
+ console.info("Loading ".concat(fileName));
3495
+ }
3496
+ // TODO: !!!! Implement JSON loading
3497
+ // TODO: !!!! Chack url uniqueness and collisions
3498
+ }
3499
+ else {
3500
+ if (isVerbose) {
3501
+ console.info("Skipping file ".concat(fileName));
3502
+ }
3503
+ }
3504
+ _b.label = 6;
3505
+ case 6:
3506
+ fileNames_1_1 = fileNames_1.next();
3507
+ return [3 /*break*/, 3];
3508
+ case 7: return [3 /*break*/, 10];
3509
+ case 8:
3510
+ e_1_1 = _b.sent();
3511
+ e_1 = { error: e_1_1 };
3512
+ return [3 /*break*/, 10];
3513
+ case 9:
3514
+ try {
3515
+ if (fileNames_1_1 && !fileNames_1_1.done && (_a = fileNames_1.return)) _a.call(fileNames_1);
3516
+ }
3517
+ finally { if (e_1) throw e_1.error; }
3518
+ return [7 /*endfinally*/];
3519
+ case 10: return [2 /*return*/, promptbooks];
3520
+ }
3444
3521
  });
3445
3522
  }); });
3446
3523
  }
3447
3524
  /***
3448
- * Note: [1] Fixing the dynamic import issue in Webpack
3525
+ * Note: [1] Fixing the dynamic import issue in Webpack (! Not working !)
3449
3526
  * > ./node_modules/@promptbook/core/esm/index.es.js
3450
3527
  * > Critical dependency: the request of a dependency is an expression
3451
3528
  *
@@ -3552,23 +3629,6 @@
3552
3629
  * TODO: [🍓][🚯] !!! Add to README and samples + maybe make `@promptbook/library` package
3553
3630
  */
3554
3631
 
3555
- /**
3556
- * Returns the same value that is passed as argument.
3557
- * No side effects.
3558
- *
3559
- * Note: It can be usefull for leveling indentation
3560
- *
3561
- * @param value any values
3562
- * @returns the same values
3563
- */
3564
- function just(value) {
3565
- if (value === undefined) {
3566
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3567
- return undefined;
3568
- }
3569
- return value;
3570
- }
3571
-
3572
3632
  /**
3573
3633
  * Just testing imports and compatibility
3574
3634
  */