@promptbook/node 0.84.0-0 → 0.84.0-10
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/README.md +1 -0
- package/esm/index.es.js +193 -201
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/cli.index.d.ts +8 -0
- package/esm/typings/src/_packages/core.index.d.ts +4 -0
- package/esm/typings/src/_packages/markitdown.index.d.ts +8 -0
- package/esm/typings/src/_packages/pdf.index.d.ts +6 -0
- package/esm/typings/src/_packages/utils.index.d.ts +2 -0
- package/esm/typings/src/_packages/wizzard.index.d.ts +8 -0
- package/esm/typings/src/constants.d.ts +1 -1
- package/esm/typings/src/executables/platforms/locateAppOnLinux.d.ts +1 -1
- package/esm/typings/src/executables/platforms/locateAppOnMacOs.d.ts +1 -1
- package/esm/typings/src/pipeline/book-notation.d.ts +4 -0
- package/esm/typings/src/pipeline/prompt-notation.d.ts +18 -0
- package/esm/typings/src/pipeline/prompt-notation.test.d.ts +4 -0
- package/esm/typings/src/scrapers/_boilerplate/BoilerplateScraper.d.ts +43 -0
- package/esm/typings/src/scrapers/_boilerplate/createBoilerplateScraper.d.ts +20 -0
- package/esm/typings/src/scrapers/_boilerplate/playground/boilerplate-scraper-playground.d.ts +5 -0
- package/esm/typings/src/scrapers/_boilerplate/register-constructor.d.ts +15 -0
- package/esm/typings/src/scrapers/_boilerplate/register-metadata.d.ts +28 -0
- package/esm/typings/src/scrapers/markitdown/MarkitdownScraper.d.ts +50 -0
- package/esm/typings/src/scrapers/markitdown/createMarkitdownScraper.d.ts +22 -0
- package/esm/typings/src/scrapers/markitdown/playground/markitdown-scraper-playground.d.ts +5 -0
- package/esm/typings/src/scrapers/markitdown/register-constructor.d.ts +17 -0
- package/esm/typings/src/scrapers/markitdown/register-metadata.d.ts +28 -0
- package/esm/typings/src/types/typeAliases.d.ts +1 -1
- package/package.json +3 -3
- package/umd/index.umd.js +196 -203
- package/umd/index.umd.js.map +1 -1
package/README.md
CHANGED
|
@@ -273,6 +273,7 @@ Or you can install them separately:
|
|
|
273
273
|
- **[@promptbook/remote-client](https://www.npmjs.com/package/@promptbook/remote-client)** - Remote client for remote execution of promptbooks
|
|
274
274
|
- **[@promptbook/remote-server](https://www.npmjs.com/package/@promptbook/remote-server)** - Remote server for remote execution of promptbooks
|
|
275
275
|
- **[@promptbook/pdf](https://www.npmjs.com/package/@promptbook/pdf)** - Read knowledge from `.pdf` documents
|
|
276
|
+
- **[@promptbook/documents](https://www.npmjs.com/package/@promptbook/markitdown)** - Integration of [Markitdown by Microsoft](https://github.com/microsoft/markitdown)
|
|
276
277
|
- **[@promptbook/documents](https://www.npmjs.com/package/@promptbook/documents)** - Read knowledge from documents like `.docx`, `.odt`,…
|
|
277
278
|
- **[@promptbook/legacy-documents](https://www.npmjs.com/package/@promptbook/legacy-documents)** - Read knowledge from legacy documents like `.doc`, `.rtf`,…
|
|
278
279
|
- **[@promptbook/website-crawler](https://www.npmjs.com/package/@promptbook/website-crawler)** - Crawl knowledge from the web
|
package/esm/index.es.js
CHANGED
|
@@ -9,8 +9,7 @@ import { unparse, parse } from 'papaparse';
|
|
|
9
9
|
import { SHA256 } from 'crypto-js';
|
|
10
10
|
import hexEncoder from 'crypto-js/enc-hex';
|
|
11
11
|
import { lookup } from 'mime-types';
|
|
12
|
-
import {
|
|
13
|
-
import { promisify } from 'util';
|
|
12
|
+
import { spawn } from 'child_process';
|
|
14
13
|
import * as dotenv from 'dotenv';
|
|
15
14
|
import sha256 from 'crypto-js/sha256';
|
|
16
15
|
|
|
@@ -28,7 +27,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
28
27
|
* @generated
|
|
29
28
|
* @see https://github.com/webgptorg/promptbook
|
|
30
29
|
*/
|
|
31
|
-
var PROMPTBOOK_ENGINE_VERSION = '0.
|
|
30
|
+
var PROMPTBOOK_ENGINE_VERSION = '0.84.0-9';
|
|
32
31
|
/**
|
|
33
32
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
34
33
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -186,7 +185,7 @@ var NAME = "Promptbook";
|
|
|
186
185
|
*
|
|
187
186
|
* @public exported from `@promptbook/core`
|
|
188
187
|
*/
|
|
189
|
-
var ADMIN_EMAIL = '
|
|
188
|
+
var ADMIN_EMAIL = 'pavol@ptbk.io';
|
|
190
189
|
/**
|
|
191
190
|
* Name of the responsible person for the Promptbook on GitHub
|
|
192
191
|
*
|
|
@@ -917,7 +916,7 @@ var ORDER_OF_PIPELINE_JSON = [
|
|
|
917
916
|
*
|
|
918
917
|
* @private within the repository
|
|
919
918
|
*/
|
|
920
|
-
var REPLACING_NONCE = '
|
|
919
|
+
var REPLACING_NONCE = 'ptbkauk42kV2dzao34faw7FudQUHYPtW';
|
|
921
920
|
/**
|
|
922
921
|
* @@@
|
|
923
922
|
*
|
|
@@ -1760,7 +1759,7 @@ var LimitReachedError = /** @class */ (function (_super) {
|
|
|
1760
1759
|
var NotYetImplementedError = /** @class */ (function (_super) {
|
|
1761
1760
|
__extends(NotYetImplementedError, _super);
|
|
1762
1761
|
function NotYetImplementedError(message) {
|
|
1763
|
-
var _this = _super.call(this, spaceTrim$1(function (block) { return "\n ".concat(block(message), "\n\n Note: This feature is not implemented yet but it will be soon.\n\n If you want speed up the implementation or just read more, look here:\n https://github.com/webgptorg/promptbook\n\n Or contact us on
|
|
1762
|
+
var _this = _super.call(this, spaceTrim$1(function (block) { return "\n ".concat(block(message), "\n\n Note: This feature is not implemented yet but it will be soon.\n\n If you want speed up the implementation or just read more, look here:\n https://github.com/webgptorg/promptbook\n\n Or contact us on pavol@ptbk.io\n\n "); })) || this;
|
|
1764
1763
|
_this.name = 'NotYetImplementedError';
|
|
1765
1764
|
Object.setPrototypeOf(_this, NotYetImplementedError.prototype);
|
|
1766
1765
|
return _this;
|
|
@@ -9427,30 +9426,187 @@ function compilePipeline(pipelineString, tools, options) {
|
|
|
9427
9426
|
*/
|
|
9428
9427
|
var $isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
|
|
9429
9428
|
|
|
9430
|
-
|
|
9431
|
-
|
|
9429
|
+
/**
|
|
9430
|
+
* Normalize options for `execCommand` and `execCommands`
|
|
9431
|
+
*
|
|
9432
|
+
* Note: `$` is used to indicate that this function behaves differently according to `process.platform`
|
|
9433
|
+
*
|
|
9434
|
+
* @private internal utility of `execCommand` and `execCommands`
|
|
9435
|
+
*/
|
|
9436
|
+
function $execCommandNormalizeOptions(options) {
|
|
9437
|
+
var _a;
|
|
9438
|
+
var _b, _c, _d, _e;
|
|
9439
|
+
var command;
|
|
9440
|
+
var cwd;
|
|
9441
|
+
var crashOnError;
|
|
9442
|
+
var args = [];
|
|
9443
|
+
var timeout;
|
|
9444
|
+
var isVerbose;
|
|
9445
|
+
if (typeof options === 'string') {
|
|
9446
|
+
// TODO: [1] DRY default values
|
|
9447
|
+
command = options;
|
|
9448
|
+
cwd = process.cwd();
|
|
9449
|
+
crashOnError = true;
|
|
9450
|
+
timeout = Infinity; // <- TODO: [⏳]
|
|
9451
|
+
isVerbose = DEFAULT_IS_VERBOSE;
|
|
9452
|
+
}
|
|
9453
|
+
else {
|
|
9454
|
+
/*
|
|
9455
|
+
TODO:
|
|
9456
|
+
if ((options as any).commands !== undefined) {
|
|
9457
|
+
commands = (options as any).commands;
|
|
9458
|
+
} else {
|
|
9459
|
+
commands = [(options as any).command];
|
|
9460
|
+
}
|
|
9461
|
+
*/
|
|
9462
|
+
// TODO: [1] DRY default values
|
|
9463
|
+
command = options.command;
|
|
9464
|
+
cwd = (_b = options.cwd) !== null && _b !== void 0 ? _b : process.cwd();
|
|
9465
|
+
crashOnError = (_c = options.crashOnError) !== null && _c !== void 0 ? _c : true;
|
|
9466
|
+
timeout = (_d = options.timeout) !== null && _d !== void 0 ? _d : Infinity;
|
|
9467
|
+
isVerbose = (_e = options.isVerbose) !== null && _e !== void 0 ? _e : DEFAULT_IS_VERBOSE;
|
|
9468
|
+
}
|
|
9469
|
+
// TODO: /(-[a-zA-Z0-9-]+\s+[^\s]*)|[^\s]*/g
|
|
9470
|
+
var _ = Array.from(command.matchAll(/(".*")|([^\s]*)/g))
|
|
9471
|
+
.map(function (_a) {
|
|
9472
|
+
var _b = __read(_a, 1), match = _b[0];
|
|
9473
|
+
return match;
|
|
9474
|
+
})
|
|
9475
|
+
.filter(function (arg) { return arg !== ''; });
|
|
9476
|
+
if (_.length > 1) {
|
|
9477
|
+
_a = __read(_), command = _a[0], args = _a.slice(1);
|
|
9478
|
+
}
|
|
9479
|
+
if (options.args) {
|
|
9480
|
+
args = __spreadArray(__spreadArray([], __read(args), false), __read(options.args), false);
|
|
9481
|
+
}
|
|
9482
|
+
var humanReadableCommand = !['npx', 'npm'].includes(command) ? command : args[0];
|
|
9483
|
+
if (['ts-node'].includes(humanReadableCommand)) {
|
|
9484
|
+
humanReadableCommand += " ".concat(args[1]);
|
|
9485
|
+
}
|
|
9486
|
+
if (/^win/.test(process.platform) && ['npm', 'npx'].includes(command)) {
|
|
9487
|
+
command = "".concat(command, ".cmd");
|
|
9488
|
+
}
|
|
9489
|
+
return { command: command, humanReadableCommand: humanReadableCommand, args: args, cwd: cwd, crashOnError: crashOnError, timeout: timeout, isVerbose: isVerbose };
|
|
9490
|
+
}
|
|
9491
|
+
// TODO: This should show type error> execCommandNormalizeOptions({ command: '', commands: [''] });
|
|
9492
|
+
|
|
9493
|
+
/**
|
|
9494
|
+
* Run one command in a shell
|
|
9495
|
+
*
|
|
9496
|
+
*
|
|
9497
|
+
* Note: There are 2 similar functions in the codebase:
|
|
9498
|
+
* - `$execCommand` which runs a single command
|
|
9499
|
+
* - `$execCommands` which runs multiple commands
|
|
9500
|
+
* Note: `$` is used to indicate that this function is not a pure function - it runs a command in a shell
|
|
9501
|
+
*
|
|
9502
|
+
* @public exported from `@promptbook/node`
|
|
9503
|
+
*/
|
|
9504
|
+
function $execCommand(options) {
|
|
9505
|
+
if (!$isRunningInNode()) {
|
|
9506
|
+
throw new EnvironmentMismatchError('Function `$execCommand` can run only in Node environment.js');
|
|
9507
|
+
}
|
|
9508
|
+
return new Promise(function (resolve, reject) {
|
|
9509
|
+
// eslint-disable-next-line prefer-const
|
|
9510
|
+
var _a = $execCommandNormalizeOptions(options), command = _a.command, humanReadableCommand = _a.humanReadableCommand, args = _a.args, cwd = _a.cwd, crashOnError = _a.crashOnError, timeout = _a.timeout, _b = _a.isVerbose, isVerbose = _b === void 0 ? DEFAULT_IS_VERBOSE : _b;
|
|
9511
|
+
if (timeout !== Infinity) {
|
|
9512
|
+
// TODO: In waitasecond forTime(Infinity) should be equivalent to forEver()
|
|
9513
|
+
forTime(timeout).then(function () {
|
|
9514
|
+
if (crashOnError) {
|
|
9515
|
+
reject(new Error("Command \"".concat(humanReadableCommand, "\" exceeded time limit of ").concat(timeout, "ms")));
|
|
9516
|
+
}
|
|
9517
|
+
else {
|
|
9518
|
+
console.warn("Command \"".concat(humanReadableCommand, "\" exceeded time limit of ").concat(timeout, "ms but continues running"));
|
|
9519
|
+
resolve('Command exceeded time limit');
|
|
9520
|
+
}
|
|
9521
|
+
});
|
|
9522
|
+
}
|
|
9523
|
+
if (isVerbose) {
|
|
9524
|
+
console.info(colors.yellow(cwd) + ' ' + colors.green(command) + ' ' + colors.blue(args.join(' ')));
|
|
9525
|
+
}
|
|
9526
|
+
try {
|
|
9527
|
+
var commandProcess = spawn(command, args, { cwd: cwd, shell: true });
|
|
9528
|
+
if (isVerbose) {
|
|
9529
|
+
commandProcess.on('message', function (message) {
|
|
9530
|
+
console.info({ message: message });
|
|
9531
|
+
});
|
|
9532
|
+
}
|
|
9533
|
+
var output_1 = [];
|
|
9534
|
+
commandProcess.stdout.on('data', function (stdout) {
|
|
9535
|
+
output_1.push(stdout.toString());
|
|
9536
|
+
if (isVerbose) {
|
|
9537
|
+
console.info(stdout.toString());
|
|
9538
|
+
}
|
|
9539
|
+
});
|
|
9540
|
+
commandProcess.stderr.on('data', function (stderr) {
|
|
9541
|
+
output_1.push(stderr.toString());
|
|
9542
|
+
if (isVerbose && stderr.toString().trim()) {
|
|
9543
|
+
console.warn(stderr.toString());
|
|
9544
|
+
}
|
|
9545
|
+
});
|
|
9546
|
+
var finishWithCode = function (code) {
|
|
9547
|
+
if (code !== 0) {
|
|
9548
|
+
if (crashOnError) {
|
|
9549
|
+
reject(new Error(output_1.join('\n').trim() ||
|
|
9550
|
+
"Command \"".concat(humanReadableCommand, "\" exited with code ").concat(code)));
|
|
9551
|
+
}
|
|
9552
|
+
else {
|
|
9553
|
+
if (isVerbose) {
|
|
9554
|
+
console.warn("Command \"".concat(humanReadableCommand, "\" exited with code ").concat(code));
|
|
9555
|
+
}
|
|
9556
|
+
resolve(spaceTrim$1(output_1.join('\n')));
|
|
9557
|
+
}
|
|
9558
|
+
}
|
|
9559
|
+
else {
|
|
9560
|
+
resolve(spaceTrim$1(output_1.join('\n')));
|
|
9561
|
+
}
|
|
9562
|
+
};
|
|
9563
|
+
commandProcess.on('close', finishWithCode);
|
|
9564
|
+
commandProcess.on('exit', finishWithCode);
|
|
9565
|
+
commandProcess.on('disconnect', function () {
|
|
9566
|
+
// Note: Unexpected disconnection should always result in rejection
|
|
9567
|
+
reject(new Error("Command \"".concat(humanReadableCommand, "\" disconnected")));
|
|
9568
|
+
});
|
|
9569
|
+
commandProcess.on('error', function (error) {
|
|
9570
|
+
if (crashOnError) {
|
|
9571
|
+
reject(new Error("Command \"".concat(humanReadableCommand, "\" failed: \n").concat(error.message)));
|
|
9572
|
+
}
|
|
9573
|
+
else {
|
|
9574
|
+
if (isVerbose) {
|
|
9575
|
+
console.warn(error);
|
|
9576
|
+
}
|
|
9577
|
+
resolve(spaceTrim$1(output_1.join('\n')));
|
|
9578
|
+
}
|
|
9579
|
+
});
|
|
9580
|
+
}
|
|
9581
|
+
catch (error) {
|
|
9582
|
+
// Note: Unexpected error in sync code should always result in rejection
|
|
9583
|
+
reject(error);
|
|
9584
|
+
}
|
|
9585
|
+
});
|
|
9586
|
+
}
|
|
9587
|
+
/**
|
|
9588
|
+
* Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
|
|
9589
|
+
*/
|
|
9590
|
+
|
|
9432
9591
|
/**
|
|
9433
9592
|
* @@@
|
|
9434
9593
|
*
|
|
9435
9594
|
* @private within the repository
|
|
9436
9595
|
*/
|
|
9437
9596
|
function locateAppOnLinux(_a) {
|
|
9438
|
-
var
|
|
9597
|
+
var linuxWhich = _a.linuxWhich;
|
|
9439
9598
|
return __awaiter(this, void 0, void 0, function () {
|
|
9440
|
-
var
|
|
9441
|
-
return __generator(this, function (
|
|
9442
|
-
switch (
|
|
9599
|
+
var result, error_1;
|
|
9600
|
+
return __generator(this, function (_b) {
|
|
9601
|
+
switch (_b.label) {
|
|
9443
9602
|
case 0:
|
|
9444
|
-
|
|
9445
|
-
return [4 /*yield*/,
|
|
9603
|
+
_b.trys.push([0, 2, , 3]);
|
|
9604
|
+
return [4 /*yield*/, $execCommand({ crashOnError: true, command: "which ".concat(linuxWhich) })];
|
|
9446
9605
|
case 1:
|
|
9447
|
-
|
|
9448
|
-
|
|
9449
|
-
return [2 /*return*/, stdout.trim()];
|
|
9450
|
-
}
|
|
9451
|
-
throw new Error("Can not locate app ".concat(appName, " on Linux.\n ").concat(stderr));
|
|
9606
|
+
result = _b.sent();
|
|
9607
|
+
return [2 /*return*/, result.trim()];
|
|
9452
9608
|
case 2:
|
|
9453
|
-
error_1 =
|
|
9609
|
+
error_1 = _b.sent();
|
|
9454
9610
|
if (!(error_1 instanceof Error)) {
|
|
9455
9611
|
throw error_1;
|
|
9456
9612
|
}
|
|
@@ -9520,43 +9676,41 @@ function isExecutable(path, fs) {
|
|
|
9520
9676
|
// @see https://stackoverflow.com/questions/37000981/how-to-import-node-module-in-typescript-without-type-definitions
|
|
9521
9677
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
9522
9678
|
var userhome = require('userhome');
|
|
9523
|
-
// Note: We want to use the `exec` as async function
|
|
9524
|
-
var exec = promisify(exec$2);
|
|
9525
9679
|
/**
|
|
9526
9680
|
* @@@
|
|
9527
9681
|
*
|
|
9528
9682
|
* @private within the repository
|
|
9529
9683
|
*/
|
|
9530
9684
|
function locateAppOnMacOs(_a) {
|
|
9531
|
-
var
|
|
9685
|
+
var macOsName = _a.macOsName;
|
|
9532
9686
|
return __awaiter(this, void 0, void 0, function () {
|
|
9533
|
-
var toExec, regPath, altPath,
|
|
9534
|
-
return __generator(this, function (
|
|
9535
|
-
switch (
|
|
9687
|
+
var toExec, regPath, altPath, result, error_1;
|
|
9688
|
+
return __generator(this, function (_b) {
|
|
9689
|
+
switch (_b.label) {
|
|
9536
9690
|
case 0:
|
|
9537
|
-
|
|
9691
|
+
_b.trys.push([0, 6, , 7]);
|
|
9538
9692
|
toExec = "/Contents/MacOS/".concat(macOsName);
|
|
9539
9693
|
regPath = "/Applications/".concat(macOsName, ".app") + toExec;
|
|
9540
9694
|
altPath = userhome(regPath.slice(1));
|
|
9541
9695
|
return [4 /*yield*/, isExecutable(regPath, $provideFilesystemForNode())];
|
|
9542
9696
|
case 1:
|
|
9543
|
-
if (!
|
|
9697
|
+
if (!_b.sent()) return [3 /*break*/, 2];
|
|
9544
9698
|
return [2 /*return*/, regPath];
|
|
9545
9699
|
case 2: return [4 /*yield*/, isExecutable(altPath, $provideFilesystemForNode())];
|
|
9546
9700
|
case 3:
|
|
9547
|
-
if (
|
|
9701
|
+
if (_b.sent()) {
|
|
9548
9702
|
return [2 /*return*/, altPath];
|
|
9549
9703
|
}
|
|
9550
|
-
|
|
9551
|
-
case 4: return [4 /*yield*/,
|
|
9704
|
+
_b.label = 4;
|
|
9705
|
+
case 4: return [4 /*yield*/, $execCommand({
|
|
9706
|
+
crashOnError: true,
|
|
9707
|
+
command: "mdfind 'kMDItemDisplayName == \"".concat(macOsName, "\" && kMDItemKind == Application'"),
|
|
9708
|
+
})];
|
|
9552
9709
|
case 5:
|
|
9553
|
-
|
|
9554
|
-
|
|
9555
|
-
return [2 /*return*/, stdout.trim() + toExec];
|
|
9556
|
-
}
|
|
9557
|
-
throw new Error("Can not locate app ".concat(appName, " on macOS.\n ").concat(stderr));
|
|
9710
|
+
result = _b.sent();
|
|
9711
|
+
return [2 /*return*/, result.trim() + toExec];
|
|
9558
9712
|
case 6:
|
|
9559
|
-
error_1 =
|
|
9713
|
+
error_1 = _b.sent();
|
|
9560
9714
|
if (!(error_1 instanceof Error)) {
|
|
9561
9715
|
throw error_1;
|
|
9562
9716
|
}
|
|
@@ -9657,7 +9811,7 @@ function locateApp(options) {
|
|
|
9657
9811
|
}
|
|
9658
9812
|
else if (process.platform === 'darwin') {
|
|
9659
9813
|
if (macOsName) {
|
|
9660
|
-
return locateAppOnMacOs({
|
|
9814
|
+
return locateAppOnMacOs({ macOsName: macOsName });
|
|
9661
9815
|
}
|
|
9662
9816
|
else {
|
|
9663
9817
|
throw new Error("".concat(appName, " is not available on macOS."));
|
|
@@ -9665,7 +9819,7 @@ function locateApp(options) {
|
|
|
9665
9819
|
}
|
|
9666
9820
|
else {
|
|
9667
9821
|
if (linuxWhich) {
|
|
9668
|
-
return locateAppOnLinux({
|
|
9822
|
+
return locateAppOnLinux({ linuxWhich: linuxWhich });
|
|
9669
9823
|
}
|
|
9670
9824
|
else {
|
|
9671
9825
|
throw new Error("".concat(appName, " is not available on Linux."));
|
|
@@ -11211,168 +11365,6 @@ var FileCacheStorage = /** @class */ (function () {
|
|
|
11211
11365
|
* Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
|
|
11212
11366
|
*/
|
|
11213
11367
|
|
|
11214
|
-
/**
|
|
11215
|
-
* Normalize options for `execCommand` and `execCommands`
|
|
11216
|
-
*
|
|
11217
|
-
* Note: `$` is used to indicate that this function behaves differently according to `process.platform`
|
|
11218
|
-
*
|
|
11219
|
-
* @private internal utility of `execCommand` and `execCommands`
|
|
11220
|
-
*/
|
|
11221
|
-
function $execCommandNormalizeOptions(options) {
|
|
11222
|
-
var _a;
|
|
11223
|
-
var _b, _c, _d, _e;
|
|
11224
|
-
var command;
|
|
11225
|
-
var cwd;
|
|
11226
|
-
var crashOnError;
|
|
11227
|
-
var args = [];
|
|
11228
|
-
var timeout;
|
|
11229
|
-
var isVerbose;
|
|
11230
|
-
if (typeof options === 'string') {
|
|
11231
|
-
// TODO: [1] DRY default values
|
|
11232
|
-
command = options;
|
|
11233
|
-
cwd = process.cwd();
|
|
11234
|
-
crashOnError = true;
|
|
11235
|
-
timeout = Infinity; // <- TODO: [⏳]
|
|
11236
|
-
isVerbose = DEFAULT_IS_VERBOSE;
|
|
11237
|
-
}
|
|
11238
|
-
else {
|
|
11239
|
-
/*
|
|
11240
|
-
TODO:
|
|
11241
|
-
if ((options as any).commands !== undefined) {
|
|
11242
|
-
commands = (options as any).commands;
|
|
11243
|
-
} else {
|
|
11244
|
-
commands = [(options as any).command];
|
|
11245
|
-
}
|
|
11246
|
-
*/
|
|
11247
|
-
// TODO: [1] DRY default values
|
|
11248
|
-
command = options.command;
|
|
11249
|
-
cwd = (_b = options.cwd) !== null && _b !== void 0 ? _b : process.cwd();
|
|
11250
|
-
crashOnError = (_c = options.crashOnError) !== null && _c !== void 0 ? _c : true;
|
|
11251
|
-
timeout = (_d = options.timeout) !== null && _d !== void 0 ? _d : Infinity;
|
|
11252
|
-
isVerbose = (_e = options.isVerbose) !== null && _e !== void 0 ? _e : DEFAULT_IS_VERBOSE;
|
|
11253
|
-
}
|
|
11254
|
-
// TODO: /(-[a-zA-Z0-9-]+\s+[^\s]*)|[^\s]*/g
|
|
11255
|
-
var _ = Array.from(command.matchAll(/(".*")|([^\s]*)/g))
|
|
11256
|
-
.map(function (_a) {
|
|
11257
|
-
var _b = __read(_a, 1), match = _b[0];
|
|
11258
|
-
return match;
|
|
11259
|
-
})
|
|
11260
|
-
.filter(function (arg) { return arg !== ''; });
|
|
11261
|
-
if (_.length > 1) {
|
|
11262
|
-
_a = __read(_), command = _a[0], args = _a.slice(1);
|
|
11263
|
-
}
|
|
11264
|
-
if (options.args) {
|
|
11265
|
-
args = __spreadArray(__spreadArray([], __read(args), false), __read(options.args), false);
|
|
11266
|
-
}
|
|
11267
|
-
var humanReadableCommand = !['npx', 'npm'].includes(command) ? command : args[0];
|
|
11268
|
-
if (['ts-node'].includes(humanReadableCommand)) {
|
|
11269
|
-
humanReadableCommand += " ".concat(args[1]);
|
|
11270
|
-
}
|
|
11271
|
-
if (/^win/.test(process.platform) && ['npm', 'npx'].includes(command)) {
|
|
11272
|
-
command = "".concat(command, ".cmd");
|
|
11273
|
-
}
|
|
11274
|
-
return { command: command, humanReadableCommand: humanReadableCommand, args: args, cwd: cwd, crashOnError: crashOnError, timeout: timeout, isVerbose: isVerbose };
|
|
11275
|
-
}
|
|
11276
|
-
// TODO: This should show type error> execCommandNormalizeOptions({ command: '', commands: [''] });
|
|
11277
|
-
|
|
11278
|
-
/**
|
|
11279
|
-
* Run one command in a shell
|
|
11280
|
-
*
|
|
11281
|
-
*
|
|
11282
|
-
* Note: There are 2 similar functions in the codebase:
|
|
11283
|
-
* - `$execCommand` which runs a single command
|
|
11284
|
-
* - `$execCommands` which runs multiple commands
|
|
11285
|
-
* Note: `$` is used to indicate that this function is not a pure function - it runs a command in a shell
|
|
11286
|
-
*
|
|
11287
|
-
* @public exported from `@promptbook/node`
|
|
11288
|
-
*/
|
|
11289
|
-
function $execCommand(options) {
|
|
11290
|
-
if (!$isRunningInNode()) {
|
|
11291
|
-
throw new EnvironmentMismatchError('Function `$execCommand` can run only in Node environment.js');
|
|
11292
|
-
}
|
|
11293
|
-
return new Promise(function (resolve, reject) {
|
|
11294
|
-
// eslint-disable-next-line prefer-const
|
|
11295
|
-
var _a = $execCommandNormalizeOptions(options), command = _a.command, humanReadableCommand = _a.humanReadableCommand, args = _a.args, cwd = _a.cwd, crashOnError = _a.crashOnError, timeout = _a.timeout, _b = _a.isVerbose, isVerbose = _b === void 0 ? DEFAULT_IS_VERBOSE : _b;
|
|
11296
|
-
if (timeout !== Infinity) {
|
|
11297
|
-
// TODO: In waitasecond forTime(Infinity) should be equivalent to forEver()
|
|
11298
|
-
forTime(timeout).then(function () {
|
|
11299
|
-
if (crashOnError) {
|
|
11300
|
-
reject(new Error("Command \"".concat(humanReadableCommand, "\" exceeded time limit of ").concat(timeout, "ms")));
|
|
11301
|
-
}
|
|
11302
|
-
else {
|
|
11303
|
-
console.warn("Command \"".concat(humanReadableCommand, "\" exceeded time limit of ").concat(timeout, "ms but continues running"));
|
|
11304
|
-
resolve('Command exceeded time limit');
|
|
11305
|
-
}
|
|
11306
|
-
});
|
|
11307
|
-
}
|
|
11308
|
-
if (isVerbose) {
|
|
11309
|
-
console.info(colors.yellow(cwd) + ' ' + colors.green(command) + ' ' + colors.blue(args.join(' ')));
|
|
11310
|
-
}
|
|
11311
|
-
try {
|
|
11312
|
-
var commandProcess = spawn(command, args, { cwd: cwd, shell: true });
|
|
11313
|
-
if (isVerbose) {
|
|
11314
|
-
commandProcess.on('message', function (message) {
|
|
11315
|
-
console.info({ message: message });
|
|
11316
|
-
});
|
|
11317
|
-
}
|
|
11318
|
-
var output_1 = [];
|
|
11319
|
-
commandProcess.stdout.on('data', function (stdout) {
|
|
11320
|
-
output_1.push(stdout.toString());
|
|
11321
|
-
if (isVerbose) {
|
|
11322
|
-
console.info(stdout.toString());
|
|
11323
|
-
}
|
|
11324
|
-
});
|
|
11325
|
-
commandProcess.stderr.on('data', function (stderr) {
|
|
11326
|
-
output_1.push(stderr.toString());
|
|
11327
|
-
if (isVerbose && stderr.toString().trim()) {
|
|
11328
|
-
console.warn(stderr.toString());
|
|
11329
|
-
}
|
|
11330
|
-
});
|
|
11331
|
-
var finishWithCode = function (code) {
|
|
11332
|
-
if (code !== 0) {
|
|
11333
|
-
if (crashOnError) {
|
|
11334
|
-
reject(new Error(output_1.join('\n').trim() ||
|
|
11335
|
-
"Command \"".concat(humanReadableCommand, "\" exited with code ").concat(code)));
|
|
11336
|
-
}
|
|
11337
|
-
else {
|
|
11338
|
-
if (isVerbose) {
|
|
11339
|
-
console.warn("Command \"".concat(humanReadableCommand, "\" exited with code ").concat(code));
|
|
11340
|
-
}
|
|
11341
|
-
resolve(spaceTrim$1(output_1.join('\n')));
|
|
11342
|
-
}
|
|
11343
|
-
}
|
|
11344
|
-
else {
|
|
11345
|
-
resolve(spaceTrim$1(output_1.join('\n')));
|
|
11346
|
-
}
|
|
11347
|
-
};
|
|
11348
|
-
commandProcess.on('close', finishWithCode);
|
|
11349
|
-
commandProcess.on('exit', finishWithCode);
|
|
11350
|
-
commandProcess.on('disconnect', function () {
|
|
11351
|
-
// Note: Unexpected disconnection should always result in rejection
|
|
11352
|
-
reject(new Error("Command \"".concat(humanReadableCommand, "\" disconnected")));
|
|
11353
|
-
});
|
|
11354
|
-
commandProcess.on('error', function (error) {
|
|
11355
|
-
if (crashOnError) {
|
|
11356
|
-
reject(new Error("Command \"".concat(humanReadableCommand, "\" failed: \n").concat(error.message)));
|
|
11357
|
-
}
|
|
11358
|
-
else {
|
|
11359
|
-
if (isVerbose) {
|
|
11360
|
-
console.warn(error);
|
|
11361
|
-
}
|
|
11362
|
-
resolve(spaceTrim$1(output_1.join('\n')));
|
|
11363
|
-
}
|
|
11364
|
-
});
|
|
11365
|
-
}
|
|
11366
|
-
catch (error) {
|
|
11367
|
-
// Note: Unexpected error in sync code should always result in rejection
|
|
11368
|
-
reject(error);
|
|
11369
|
-
}
|
|
11370
|
-
});
|
|
11371
|
-
}
|
|
11372
|
-
/**
|
|
11373
|
-
* Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
|
|
11374
|
-
*/
|
|
11375
|
-
|
|
11376
11368
|
/**
|
|
11377
11369
|
* Run multiple commands in a shell in sequence
|
|
11378
11370
|
*
|