@botpress/cli 0.9.4 ā 0.9.6
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/.turbo/turbo-build.log +8 -8
- package/dist/command-implementations/project-command.js +14 -9
- package/dist/command-implementations/project-command.js.map +2 -2
- package/dist/utils/require-utils.js +38 -2
- package/dist/utils/require-utils.js.map +2 -2
- package/dist/utils/require-utils.test.js +19 -0
- package/dist/utils/require-utils.test.js.map +2 -2
- package/package.json +3 -3
- package/templates/echo-bot/package.json +2 -2
- package/templates/empty-integration/package.json +2 -2
- package/templates/hello-world/package.json +2 -2
- package/templates/webhook-message/package.json +2 -2
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
|
|
2
|
-
> @botpress/cli@0.9.
|
|
2
|
+
> @botpress/cli@0.9.6 build /home/runner/work/botpress/botpress/packages/cli
|
|
3
3
|
> pnpm run bundle && pnpm run template:gen
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
> @botpress/cli@0.9.
|
|
6
|
+
> @botpress/cli@0.9.6 bundle /home/runner/work/botpress/botpress/packages/cli
|
|
7
7
|
> ts-node -T build.ts
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
> @botpress/cli@0.9.
|
|
10
|
+
> @botpress/cli@0.9.6 template:gen /home/runner/work/botpress/botpress/packages/cli
|
|
11
11
|
> pnpm -r --stream -F @bp-templates/* exec bp gen
|
|
12
12
|
|
|
13
|
-
š¤ Botpress CLI v0.9.
|
|
14
|
-
š¤ Botpress CLI v0.9.
|
|
15
|
-
š¤ Botpress CLI v0.9.
|
|
16
|
-
š¤ Botpress CLI v0.9.
|
|
13
|
+
š¤ Botpress CLI v0.9.6
|
|
14
|
+
š¤ Botpress CLI v0.9.6
|
|
15
|
+
š¤ Botpress CLI v0.9.6
|
|
16
|
+
š¤ Botpress CLI v0.9.6
|
|
17
17
|
ā No typings to generate for bot projects
|
|
18
|
-
[2K[1Gā Generating typings for integration empty-integration...[2K[1Gā Generating typings for integration
|
|
18
|
+
[2K[1Gā Generating typings for integration empty-integration...[2K[1Gā Generating typings for integration webhook-message...[2K[1Gā Generating typings for integration hello-world...[2K[1Gā Typings available at .botpress
|
|
19
19
|
[2K[1Gā Typings available at .botpress
|
|
20
20
|
[2K[1Gā Typings available at .botpress
|
|
@@ -96,15 +96,19 @@ class ProjectCommand extends import_global_command.GlobalCommand {
|
|
|
96
96
|
return { remoteInstances, localInstances };
|
|
97
97
|
}
|
|
98
98
|
async readProjectDefinitionFromFS(projectPaths = this.projectPaths) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
99
|
+
try {
|
|
100
|
+
const integrationDefinition = await this._readIntegrationDefinitionFromFS(projectPaths);
|
|
101
|
+
if (integrationDefinition) {
|
|
102
|
+
return { type: "integration", definition: integrationDefinition };
|
|
103
|
+
}
|
|
104
|
+
const interfaceDefinition = await this._readInterfaceDefinitionFromFS(projectPaths);
|
|
105
|
+
if (interfaceDefinition) {
|
|
106
|
+
return { type: "interface", definition: interfaceDefinition };
|
|
107
|
+
}
|
|
108
|
+
return { type: "bot", definition: null };
|
|
109
|
+
} catch (thrown) {
|
|
110
|
+
throw errors.BotpressCLIError.wrap(thrown, "Error while reading project definition");
|
|
106
111
|
}
|
|
107
|
-
return { type: "bot", definition: null };
|
|
108
112
|
}
|
|
109
113
|
async _readIntegrationDefinitionFromFS(projectPaths = this.projectPaths) {
|
|
110
114
|
const abs = projectPaths.abs;
|
|
@@ -116,7 +120,8 @@ class ProjectCommand extends import_global_command.GlobalCommand {
|
|
|
116
120
|
cwd: abs.workDir,
|
|
117
121
|
outfile: "",
|
|
118
122
|
entrypoint: rel.integrationDefinition,
|
|
119
|
-
write: false
|
|
123
|
+
write: false,
|
|
124
|
+
minify: false
|
|
120
125
|
});
|
|
121
126
|
const artifact = outputFiles[0];
|
|
122
127
|
if (!artifact) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/command-implementations/project-command.ts"],
|
|
4
|
-
"sourcesContent": ["import type * as client from '@botpress/client'\nimport type * as sdk from '@botpress/sdk'\nimport type { YargsConfig } from '@bpinternal/yargs-extra'\nimport bluebird from 'bluebird'\nimport chalk from 'chalk'\nimport fs from 'fs'\nimport _ from 'lodash'\nimport pathlib from 'path'\nimport semver from 'semver'\nimport { ApiClient } from '../api/client'\nimport * as codegen from '../code-generation'\nimport type * as config from '../config'\nimport * as consts from '../consts'\nimport * as errors from '../errors'\nimport { formatIntegrationRef, IntegrationRef } from '../integration-ref'\nimport { validateIntegrationDefinition } from '../sdk/validate-integration'\nimport type { CommandArgv, CommandDefinition } from '../typings'\nimport * as utils from '../utils'\nimport { GlobalCommand } from './global-command'\n\nexport type ProjectCommandDefinition = CommandDefinition<typeof config.schemas.project>\nexport type ProjectCache = { botId: string; devId: string }\n\ntype ConfigurableProjectPaths = { entryPoint: string; outDir: string; workDir: string }\ntype ConstantProjectPaths = typeof consts.fromOutDir & typeof consts.fromWorkDir\ntype AllProjectPaths = ConfigurableProjectPaths & ConstantProjectPaths\n\ntype RemoteIntegrationInstance = utils.types.Merge<sdk.IntegrationInstance<any>, { id: string }>\ntype LocalIntegrationInstance = utils.types.Merge<sdk.IntegrationInstance<any>, { id: null }>\n\nexport type ProjectType = ProjectDefinition['type']\nexport type ProjectDefinition =\n | { type: 'integration'; definition: sdk.IntegrationDefinition }\n | { type: 'interface'; definition: sdk.InterfaceDeclaration }\n | { type: 'bot'; definition: null }\n\nclass ProjectPaths extends utils.path.PathStore<keyof AllProjectPaths> {\n public constructor(argv: CommandArgv<ProjectCommandDefinition>) {\n const absWorkDir = utils.path.absoluteFrom(utils.path.cwd(), argv.workDir)\n const absEntrypoint = utils.path.absoluteFrom(absWorkDir, argv.entryPoint)\n const absOutDir = utils.path.absoluteFrom(absWorkDir, argv.outDir)\n super({\n workDir: absWorkDir,\n entryPoint: absEntrypoint,\n outDir: absOutDir,\n ..._.mapValues(consts.fromOutDir, (p) => utils.path.absoluteFrom(absOutDir, p)),\n ..._.mapValues(consts.fromWorkDir, (p) => utils.path.absoluteFrom(absWorkDir, p)),\n })\n }\n}\n\nexport abstract class ProjectCommand<C extends ProjectCommandDefinition> extends GlobalCommand<C> {\n protected override async bootstrap() {\n await super.bootstrap()\n await this._notifyUpdateSdk()\n }\n\n protected get projectPaths() {\n return new ProjectPaths(this.argv)\n }\n\n protected get projectCache() {\n return new utils.cache.FSKeyValueCache<ProjectCache>(this.projectPaths.abs.projectCacheFile)\n }\n\n protected async fetchBotIntegrationInstances(bot: sdk.Bot, api: ApiClient) {\n const integrationList = _(bot.props.integrations).values().filter(utils.guards.is.defined).value()\n\n const { remoteInstances, localInstances } = this._splitApiAndLocalIntegrationInstances(integrationList)\n\n const fetchedInstances: RemoteIntegrationInstance[] = await bluebird.map(localInstances, async (instance) => {\n const ref: IntegrationRef = { type: 'name', name: instance.name, version: instance.version }\n const integration = await api.findIntegration(ref)\n if (!integration) {\n const formattedRef = formatIntegrationRef(ref)\n throw new errors.BotpressCLIError(`Integration \"${formattedRef}\" not found`)\n }\n return { ...instance, id: integration.id }\n })\n\n return _([...fetchedInstances, ...remoteInstances])\n .keyBy((i) => i.id)\n .mapValues(({ enabled, configurationType, configuration }) => ({\n enabled,\n configurationType: configurationType ?? null,\n configuration,\n }))\n .value()\n }\n\n private _splitApiAndLocalIntegrationInstances(instances: sdk.IntegrationInstance<any>[]): {\n remoteInstances: RemoteIntegrationInstance[]\n localInstances: LocalIntegrationInstance[]\n } {\n const remoteInstances: RemoteIntegrationInstance[] = []\n const localInstances: LocalIntegrationInstance[] = []\n for (const { id, ...instance } of instances) {\n if (id) {\n remoteInstances.push({ ...instance, id })\n } else {\n localInstances.push({ ...instance, id: null })\n }\n }\n\n return { remoteInstances, localInstances }\n }\n\n protected async readProjectDefinitionFromFS(\n projectPaths: utils.path.PathStore<'workDir' | 'integrationDefinition' | 'interfaceDefinition'> = this.projectPaths\n ): Promise<ProjectDefinition> {\n const integrationDefinition = await this._readIntegrationDefinitionFromFS(projectPaths)\n if (integrationDefinition) {\n return { type: 'integration', definition: integrationDefinition }\n }\n const interfaceDefinition = await this._readInterfaceDefinitionFromFS(projectPaths)\n if (interfaceDefinition) {\n return { type: 'interface', definition: interfaceDefinition }\n }\n return { type: 'bot', definition: null }\n }\n\n private async _readIntegrationDefinitionFromFS(\n projectPaths: utils.path.PathStore<'workDir' | 'integrationDefinition'> = this.projectPaths\n ): Promise<sdk.IntegrationDefinition | undefined> {\n const abs = projectPaths.abs\n const rel = projectPaths.rel('workDir')\n\n if (!fs.existsSync(abs.integrationDefinition)) {\n return\n }\n\n const { outputFiles } = await utils.esbuild.buildEntrypoint({\n cwd: abs.workDir,\n outfile: '',\n entrypoint: rel.integrationDefinition,\n write: false,\n })\n\n const artifact = outputFiles[0]\n if (!artifact) {\n throw new errors.BotpressCLIError('Could not read integration definition')\n }\n\n const { default: definition } = utils.require.requireJsCode<{ default: sdk.IntegrationDefinition }>(artifact.text)\n\n validateIntegrationDefinition(definition)\n\n return definition\n }\n\n private async _readInterfaceDefinitionFromFS(\n projectPaths: utils.path.PathStore<'workDir' | 'interfaceDefinition'> = this.projectPaths\n ): Promise<sdk.InterfaceDeclaration | undefined> {\n const abs = projectPaths.abs\n const rel = projectPaths.rel('workDir')\n\n if (!fs.existsSync(abs.interfaceDefinition)) {\n return\n }\n\n const { outputFiles } = await utils.esbuild.buildEntrypoint({\n cwd: abs.workDir,\n outfile: '',\n entrypoint: rel.interfaceDefinition,\n write: false,\n minify: false,\n })\n\n const artifact = outputFiles[0]\n if (!artifact) {\n throw new errors.BotpressCLIError('Could not read interface definition')\n }\n\n const { default: definition } = utils.require.requireJsCode<{ default: sdk.InterfaceDeclaration }>(artifact.text)\n\n return definition\n }\n\n protected async writeGeneratedFilesToOutFolder(files: codegen.File[]) {\n for (const file of files) {\n const filePath = utils.path.absoluteFrom(this.projectPaths.abs.outDir, file.path)\n const dirPath = pathlib.dirname(filePath)\n await fs.promises.mkdir(dirPath, { recursive: true })\n await fs.promises.writeFile(filePath, file.content)\n }\n }\n\n protected displayWebhookUrls(bot: client.Bot) {\n if (!_.keys(bot.integrations).length) {\n this.logger.debug('No integrations in bot')\n return\n }\n\n this.logger.log('Integrations:')\n for (const integration of Object.values(bot.integrations).filter(utils.guards.is.defined)) {\n if (!integration.enabled) {\n this.logger.log(`${chalk.grey(integration.name)} ${chalk.italic('(disabled)')}: ${integration.webhookUrl}`, {\n prefix: { symbol: '\u25CB', indent: 2 },\n })\n } else {\n this.logger.log(`${chalk.bold(integration.name)} : ${integration.webhookUrl}`, {\n prefix: { symbol: '\u25CF', indent: 2 },\n })\n }\n }\n }\n\n protected async promptSecrets(\n integrationDef: sdk.IntegrationDefinition,\n argv: YargsConfig<typeof config.schemas.secrets>,\n opts: { formatEnv?: boolean; knownSecrets?: string[] } = {}\n ): Promise<Record<string, string | null>> {\n const formatEnv = opts.formatEnv ?? false\n const knownSecrets = opts.knownSecrets ?? []\n\n const { secrets: secretDefinitions } = integrationDef\n if (!secretDefinitions) {\n return {}\n }\n\n const secretArgv = this._parseArgvSecrets(argv.secrets)\n const invalidSecret = Object.keys(secretArgv).find((s) => !secretDefinitions[s])\n if (invalidSecret) {\n throw new errors.BotpressCLIError(`Secret ${invalidSecret} is not defined in integration definition`)\n }\n\n const values: Record<string, string | null> = {}\n for (const [secretName, { optional }] of Object.entries(secretDefinitions)) {\n const argvSecret = secretArgv[secretName]\n if (argvSecret) {\n this.logger.debug(`Using secret \"${secretName}\" from argv`)\n values[secretName] = argvSecret\n continue\n }\n\n const alreadyKnown = knownSecrets.includes(secretName)\n let mode: string\n if (alreadyKnown) {\n mode = 'already set'\n } else if (optional) {\n mode = 'optional'\n } else {\n mode = 'required'\n }\n\n const prompted = await this.prompt.text(`Enter value for secret \"${secretName}\" (${mode})`)\n if (prompted) {\n values[secretName] = prompted\n continue\n }\n\n if (alreadyKnown) {\n this.logger.log(`Secret \"${secretName}\" is unchanged`)\n } else if (optional) {\n this.logger.warn(`Secret \"${secretName}\" is unassigned`)\n } else {\n throw new errors.BotpressCLIError(`Secret \"${secretName}\" is required`)\n }\n }\n\n for (const secretName of knownSecrets) {\n const isDefined = secretName in secretDefinitions\n if (isDefined) {\n continue\n }\n const prompted = await this.prompt.confirm(`Secret \"${secretName}\" was removed. Do you wish to delete it?`)\n if (prompted) {\n this.logger.log(`Deleting secret \"${secretName}\"`, { prefix: { symbol: '\u00D7', fg: 'red' } })\n values[secretName] = null\n }\n }\n\n if (!formatEnv) {\n return values\n }\n\n const envVariables = _.mapKeys(values, (_v, k) => codegen.secretEnvVariableName(k))\n return envVariables\n }\n\n protected async readIntegrationConfigDefinition<C extends client.ClientInputs['createIntegration']['configuration']>(\n config: C\n ): Promise<C> {\n if (!config?.identifier) {\n return config\n }\n return {\n ...config,\n identifier: {\n ...config.identifier,\n linkTemplateScript: await this.readProjectFile(config.identifier.linkTemplateScript),\n },\n }\n }\n\n protected readProjectFile = async (filePath: string | undefined): Promise<string | undefined> => {\n if (!filePath) {\n return undefined\n }\n const absoluteFilePath = utils.path.absoluteFrom(this.projectPaths.abs.workDir, filePath)\n return fs.promises.readFile(absoluteFilePath, 'utf-8').catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not read file \"${absoluteFilePath}\"`)\n })\n }\n\n private _parseArgvSecrets(argvSecrets: string[]): Record<string, string> {\n const parsed: Record<string, string> = {}\n for (const secret of argvSecrets) {\n const [key, value] = utils.string.splitOnce(secret, '=')\n if (!value) {\n throw new errors.BotpressCLIError(\n `Secret \"${key}\" is missing a value. Expected format: \"SECRET_NAME=secretValue\"`\n )\n }\n parsed[key!] = value\n }\n\n return parsed\n }\n\n private _notifyUpdateSdk = async (): Promise<void> => {\n try {\n this.logger.debug('Checking if sdk is up to date')\n\n const { workDir } = this.projectPaths.abs\n const projectPkgJson = await utils.pkgJson.readPackageJson(workDir)\n if (!projectPkgJson) {\n this.logger.debug(`Could not find package.json at \"${workDir}\"`)\n return\n }\n\n const sdkPackageName = '@botpress/sdk'\n const actualSdkVersion = utils.pkgJson.findDependency(projectPkgJson, sdkPackageName)\n if (!actualSdkVersion) {\n this.logger.debug(`Could not find dependency \"${sdkPackageName}\" in project package.json`)\n return\n }\n\n if (actualSdkVersion.startsWith('workspace:')) {\n return\n }\n\n const actualCleanedSdkVersion = semver.valid(semver.coerce(actualSdkVersion))\n if (!actualCleanedSdkVersion) {\n this.logger.debug(`Invalid sdk version \"${actualSdkVersion}\" in project package.json`)\n return\n }\n\n const cliPkgJson = await this.readPkgJson()\n const expectedSdkVersion = utils.pkgJson.findDependency(cliPkgJson, sdkPackageName)\n if (!expectedSdkVersion) {\n this.logger.debug(`Could not find dependency \"${sdkPackageName}\" in cli package.json`)\n return\n }\n\n const expectedCleanedSdkVersion = semver.valid(semver.coerce(expectedSdkVersion))\n if (!expectedCleanedSdkVersion) {\n this.logger.debug(`Invalid sdk version \"${expectedSdkVersion}\" in cli package.json`)\n return\n }\n\n if (semver.eq(actualCleanedSdkVersion, expectedCleanedSdkVersion)) {\n return\n }\n\n const diff = semver.diff(actualCleanedSdkVersion, expectedCleanedSdkVersion)\n if (!diff) {\n this.logger.debug(`Could not compare versions \"${actualCleanedSdkVersion}\" and \"${expectedCleanedSdkVersion}\"`)\n return\n }\n\n const errorMsg = `Project SDK version is \"${actualCleanedSdkVersion}\", but expected \"${expectedCleanedSdkVersion}\"`\n if (utils.semver.releases.lt(diff, 'minor')) {\n this.logger.debug(`${errorMsg}. This may cause compatibility issues.`)\n return\n }\n\n this.logger.warn(chalk.bold(`${errorMsg}. This will cause compatibility issues.`))\n } catch (thrown) {\n const err = errors.BotpressCLIError.map(thrown)\n this.logger.debug(`Failed to check if sdk is up to date: ${err.message}`)\n }\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;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AsC,eAAe;AAEtG,UAAM,mBAAgD,MAAM,gBAAAC,QAAS,IAAI,gBAAgB,OAAO,aAAa;AAC3G,YAAM,MAAsB,EAAE,MAAM,QAAQ,MAAM,SAAS,MAAM,SAAS,SAAS,QAAQ;AAC3F,YAAM,cAAc,MAAM,IAAI,gBAAgB,GAAG;AACjD,UAAI,CAAC,aAAa;AAChB,cAAM,mBAAe,6CAAqB,GAAG;AAC7C,cAAM,IAAI,OAAO,iBAAiB,gBAAgB,yBAAyB;AAAA,MAC7E;AACA,aAAO,EAAE,GAAG,UAAU,IAAI,YAAY,GAAG;AAAA,IAC3C,CAAC;AAED,eAAO,cAAAD,SAAE,CAAC,GAAG,kBAAkB,GAAG,eAAe,CAAC,EAC/C,MAAM,CAAC,MAAM,EAAE,EAAE,EACjB,UAAU,CAAC,EAAE,SAAS,mBAAmB,cAAc,OAAO;AAAA,MAC7D;AAAA,MACA,mBAAmB,qBAAqB;AAAA,MACxC;AAAA,IACF,EAAE,EACD,MAAM;AAAA,EACX;AAAA,EAEQ,sCAAsC,WAG5C;AACA,UAAM,kBAA+C,CAAC;AACtD,UAAM,iBAA6C,CAAC;AACpD,eAAW,EAAE,OAAO,SAAS,KAAK,WAAW;AAC3C,UAAI,IAAI;AACN,wBAAgB,KAAK,EAAE,GAAG,UAAU,GAAG,CAAC;AAAA,MAC1C,OAAO;AACL,uBAAe,KAAK,EAAE,GAAG,UAAU,IAAI,KAAK,CAAC;AAAA,MAC/C;AAAA,IACF;AAEA,WAAO,EAAE,iBAAiB,eAAe;AAAA,EAC3C;AAAA,EAEA,MAAgB,4BACd,eAAkG,KAAK,cAC3E;AAC5B,
|
|
4
|
+
"sourcesContent": ["import type * as client from '@botpress/client'\nimport type * as sdk from '@botpress/sdk'\nimport type { YargsConfig } from '@bpinternal/yargs-extra'\nimport bluebird from 'bluebird'\nimport chalk from 'chalk'\nimport fs from 'fs'\nimport _ from 'lodash'\nimport pathlib from 'path'\nimport semver from 'semver'\nimport { ApiClient } from '../api/client'\nimport * as codegen from '../code-generation'\nimport type * as config from '../config'\nimport * as consts from '../consts'\nimport * as errors from '../errors'\nimport { formatIntegrationRef, IntegrationRef } from '../integration-ref'\nimport { validateIntegrationDefinition } from '../sdk/validate-integration'\nimport type { CommandArgv, CommandDefinition } from '../typings'\nimport * as utils from '../utils'\nimport { GlobalCommand } from './global-command'\n\nexport type ProjectCommandDefinition = CommandDefinition<typeof config.schemas.project>\nexport type ProjectCache = { botId: string; devId: string }\n\ntype ConfigurableProjectPaths = { entryPoint: string; outDir: string; workDir: string }\ntype ConstantProjectPaths = typeof consts.fromOutDir & typeof consts.fromWorkDir\ntype AllProjectPaths = ConfigurableProjectPaths & ConstantProjectPaths\n\ntype RemoteIntegrationInstance = utils.types.Merge<sdk.IntegrationInstance<any>, { id: string }>\ntype LocalIntegrationInstance = utils.types.Merge<sdk.IntegrationInstance<any>, { id: null }>\n\nexport type ProjectType = ProjectDefinition['type']\nexport type ProjectDefinition =\n | { type: 'integration'; definition: sdk.IntegrationDefinition }\n | { type: 'interface'; definition: sdk.InterfaceDeclaration }\n | { type: 'bot'; definition: null }\n\nclass ProjectPaths extends utils.path.PathStore<keyof AllProjectPaths> {\n public constructor(argv: CommandArgv<ProjectCommandDefinition>) {\n const absWorkDir = utils.path.absoluteFrom(utils.path.cwd(), argv.workDir)\n const absEntrypoint = utils.path.absoluteFrom(absWorkDir, argv.entryPoint)\n const absOutDir = utils.path.absoluteFrom(absWorkDir, argv.outDir)\n super({\n workDir: absWorkDir,\n entryPoint: absEntrypoint,\n outDir: absOutDir,\n ..._.mapValues(consts.fromOutDir, (p) => utils.path.absoluteFrom(absOutDir, p)),\n ..._.mapValues(consts.fromWorkDir, (p) => utils.path.absoluteFrom(absWorkDir, p)),\n })\n }\n}\n\nexport abstract class ProjectCommand<C extends ProjectCommandDefinition> extends GlobalCommand<C> {\n protected override async bootstrap() {\n await super.bootstrap()\n await this._notifyUpdateSdk()\n }\n\n protected get projectPaths() {\n return new ProjectPaths(this.argv)\n }\n\n protected get projectCache() {\n return new utils.cache.FSKeyValueCache<ProjectCache>(this.projectPaths.abs.projectCacheFile)\n }\n\n protected async fetchBotIntegrationInstances(bot: sdk.Bot, api: ApiClient) {\n const integrationList = _(bot.props.integrations).values().filter(utils.guards.is.defined).value()\n\n const { remoteInstances, localInstances } = this._splitApiAndLocalIntegrationInstances(integrationList)\n\n const fetchedInstances: RemoteIntegrationInstance[] = await bluebird.map(localInstances, async (instance) => {\n const ref: IntegrationRef = { type: 'name', name: instance.name, version: instance.version }\n const integration = await api.findIntegration(ref)\n if (!integration) {\n const formattedRef = formatIntegrationRef(ref)\n throw new errors.BotpressCLIError(`Integration \"${formattedRef}\" not found`)\n }\n return { ...instance, id: integration.id }\n })\n\n return _([...fetchedInstances, ...remoteInstances])\n .keyBy((i) => i.id)\n .mapValues(({ enabled, configurationType, configuration }) => ({\n enabled,\n configurationType: configurationType ?? null,\n configuration,\n }))\n .value()\n }\n\n private _splitApiAndLocalIntegrationInstances(instances: sdk.IntegrationInstance<any>[]): {\n remoteInstances: RemoteIntegrationInstance[]\n localInstances: LocalIntegrationInstance[]\n } {\n const remoteInstances: RemoteIntegrationInstance[] = []\n const localInstances: LocalIntegrationInstance[] = []\n for (const { id, ...instance } of instances) {\n if (id) {\n remoteInstances.push({ ...instance, id })\n } else {\n localInstances.push({ ...instance, id: null })\n }\n }\n\n return { remoteInstances, localInstances }\n }\n\n protected async readProjectDefinitionFromFS(\n projectPaths: utils.path.PathStore<'workDir' | 'integrationDefinition' | 'interfaceDefinition'> = this.projectPaths\n ): Promise<ProjectDefinition> {\n try {\n const integrationDefinition = await this._readIntegrationDefinitionFromFS(projectPaths)\n if (integrationDefinition) {\n return { type: 'integration', definition: integrationDefinition }\n }\n const interfaceDefinition = await this._readInterfaceDefinitionFromFS(projectPaths)\n if (interfaceDefinition) {\n return { type: 'interface', definition: interfaceDefinition }\n }\n return { type: 'bot', definition: null }\n } catch (thrown: unknown) {\n throw errors.BotpressCLIError.wrap(thrown, 'Error while reading project definition')\n }\n }\n\n private async _readIntegrationDefinitionFromFS(\n projectPaths: utils.path.PathStore<'workDir' | 'integrationDefinition'> = this.projectPaths\n ): Promise<sdk.IntegrationDefinition | undefined> {\n const abs = projectPaths.abs\n const rel = projectPaths.rel('workDir')\n\n if (!fs.existsSync(abs.integrationDefinition)) {\n return\n }\n\n const { outputFiles } = await utils.esbuild.buildEntrypoint({\n cwd: abs.workDir,\n outfile: '',\n entrypoint: rel.integrationDefinition,\n write: false,\n minify: false,\n })\n\n const artifact = outputFiles[0]\n if (!artifact) {\n throw new errors.BotpressCLIError('Could not read integration definition')\n }\n\n const { default: definition } = utils.require.requireJsCode<{ default: sdk.IntegrationDefinition }>(artifact.text)\n\n validateIntegrationDefinition(definition)\n\n return definition\n }\n\n private async _readInterfaceDefinitionFromFS(\n projectPaths: utils.path.PathStore<'workDir' | 'interfaceDefinition'> = this.projectPaths\n ): Promise<sdk.InterfaceDeclaration | undefined> {\n const abs = projectPaths.abs\n const rel = projectPaths.rel('workDir')\n\n if (!fs.existsSync(abs.interfaceDefinition)) {\n return\n }\n\n const { outputFiles } = await utils.esbuild.buildEntrypoint({\n cwd: abs.workDir,\n outfile: '',\n entrypoint: rel.interfaceDefinition,\n write: false,\n minify: false,\n })\n\n const artifact = outputFiles[0]\n if (!artifact) {\n throw new errors.BotpressCLIError('Could not read interface definition')\n }\n\n const { default: definition } = utils.require.requireJsCode<{ default: sdk.InterfaceDeclaration }>(artifact.text)\n\n return definition\n }\n\n protected async writeGeneratedFilesToOutFolder(files: codegen.File[]) {\n for (const file of files) {\n const filePath = utils.path.absoluteFrom(this.projectPaths.abs.outDir, file.path)\n const dirPath = pathlib.dirname(filePath)\n await fs.promises.mkdir(dirPath, { recursive: true })\n await fs.promises.writeFile(filePath, file.content)\n }\n }\n\n protected displayWebhookUrls(bot: client.Bot) {\n if (!_.keys(bot.integrations).length) {\n this.logger.debug('No integrations in bot')\n return\n }\n\n this.logger.log('Integrations:')\n for (const integration of Object.values(bot.integrations).filter(utils.guards.is.defined)) {\n if (!integration.enabled) {\n this.logger.log(`${chalk.grey(integration.name)} ${chalk.italic('(disabled)')}: ${integration.webhookUrl}`, {\n prefix: { symbol: '\u25CB', indent: 2 },\n })\n } else {\n this.logger.log(`${chalk.bold(integration.name)} : ${integration.webhookUrl}`, {\n prefix: { symbol: '\u25CF', indent: 2 },\n })\n }\n }\n }\n\n protected async promptSecrets(\n integrationDef: sdk.IntegrationDefinition,\n argv: YargsConfig<typeof config.schemas.secrets>,\n opts: { formatEnv?: boolean; knownSecrets?: string[] } = {}\n ): Promise<Record<string, string | null>> {\n const formatEnv = opts.formatEnv ?? false\n const knownSecrets = opts.knownSecrets ?? []\n\n const { secrets: secretDefinitions } = integrationDef\n if (!secretDefinitions) {\n return {}\n }\n\n const secretArgv = this._parseArgvSecrets(argv.secrets)\n const invalidSecret = Object.keys(secretArgv).find((s) => !secretDefinitions[s])\n if (invalidSecret) {\n throw new errors.BotpressCLIError(`Secret ${invalidSecret} is not defined in integration definition`)\n }\n\n const values: Record<string, string | null> = {}\n for (const [secretName, { optional }] of Object.entries(secretDefinitions)) {\n const argvSecret = secretArgv[secretName]\n if (argvSecret) {\n this.logger.debug(`Using secret \"${secretName}\" from argv`)\n values[secretName] = argvSecret\n continue\n }\n\n const alreadyKnown = knownSecrets.includes(secretName)\n let mode: string\n if (alreadyKnown) {\n mode = 'already set'\n } else if (optional) {\n mode = 'optional'\n } else {\n mode = 'required'\n }\n\n const prompted = await this.prompt.text(`Enter value for secret \"${secretName}\" (${mode})`)\n if (prompted) {\n values[secretName] = prompted\n continue\n }\n\n if (alreadyKnown) {\n this.logger.log(`Secret \"${secretName}\" is unchanged`)\n } else if (optional) {\n this.logger.warn(`Secret \"${secretName}\" is unassigned`)\n } else {\n throw new errors.BotpressCLIError(`Secret \"${secretName}\" is required`)\n }\n }\n\n for (const secretName of knownSecrets) {\n const isDefined = secretName in secretDefinitions\n if (isDefined) {\n continue\n }\n const prompted = await this.prompt.confirm(`Secret \"${secretName}\" was removed. Do you wish to delete it?`)\n if (prompted) {\n this.logger.log(`Deleting secret \"${secretName}\"`, { prefix: { symbol: '\u00D7', fg: 'red' } })\n values[secretName] = null\n }\n }\n\n if (!formatEnv) {\n return values\n }\n\n const envVariables = _.mapKeys(values, (_v, k) => codegen.secretEnvVariableName(k))\n return envVariables\n }\n\n protected async readIntegrationConfigDefinition<C extends client.ClientInputs['createIntegration']['configuration']>(\n config: C\n ): Promise<C> {\n if (!config?.identifier) {\n return config\n }\n return {\n ...config,\n identifier: {\n ...config.identifier,\n linkTemplateScript: await this.readProjectFile(config.identifier.linkTemplateScript),\n },\n }\n }\n\n protected readProjectFile = async (filePath: string | undefined): Promise<string | undefined> => {\n if (!filePath) {\n return undefined\n }\n const absoluteFilePath = utils.path.absoluteFrom(this.projectPaths.abs.workDir, filePath)\n return fs.promises.readFile(absoluteFilePath, 'utf-8').catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not read file \"${absoluteFilePath}\"`)\n })\n }\n\n private _parseArgvSecrets(argvSecrets: string[]): Record<string, string> {\n const parsed: Record<string, string> = {}\n for (const secret of argvSecrets) {\n const [key, value] = utils.string.splitOnce(secret, '=')\n if (!value) {\n throw new errors.BotpressCLIError(\n `Secret \"${key}\" is missing a value. Expected format: \"SECRET_NAME=secretValue\"`\n )\n }\n parsed[key!] = value\n }\n\n return parsed\n }\n\n private _notifyUpdateSdk = async (): Promise<void> => {\n try {\n this.logger.debug('Checking if sdk is up to date')\n\n const { workDir } = this.projectPaths.abs\n const projectPkgJson = await utils.pkgJson.readPackageJson(workDir)\n if (!projectPkgJson) {\n this.logger.debug(`Could not find package.json at \"${workDir}\"`)\n return\n }\n\n const sdkPackageName = '@botpress/sdk'\n const actualSdkVersion = utils.pkgJson.findDependency(projectPkgJson, sdkPackageName)\n if (!actualSdkVersion) {\n this.logger.debug(`Could not find dependency \"${sdkPackageName}\" in project package.json`)\n return\n }\n\n if (actualSdkVersion.startsWith('workspace:')) {\n return\n }\n\n const actualCleanedSdkVersion = semver.valid(semver.coerce(actualSdkVersion))\n if (!actualCleanedSdkVersion) {\n this.logger.debug(`Invalid sdk version \"${actualSdkVersion}\" in project package.json`)\n return\n }\n\n const cliPkgJson = await this.readPkgJson()\n const expectedSdkVersion = utils.pkgJson.findDependency(cliPkgJson, sdkPackageName)\n if (!expectedSdkVersion) {\n this.logger.debug(`Could not find dependency \"${sdkPackageName}\" in cli package.json`)\n return\n }\n\n const expectedCleanedSdkVersion = semver.valid(semver.coerce(expectedSdkVersion))\n if (!expectedCleanedSdkVersion) {\n this.logger.debug(`Invalid sdk version \"${expectedSdkVersion}\" in cli package.json`)\n return\n }\n\n if (semver.eq(actualCleanedSdkVersion, expectedCleanedSdkVersion)) {\n return\n }\n\n const diff = semver.diff(actualCleanedSdkVersion, expectedCleanedSdkVersion)\n if (!diff) {\n this.logger.debug(`Could not compare versions \"${actualCleanedSdkVersion}\" and \"${expectedCleanedSdkVersion}\"`)\n return\n }\n\n const errorMsg = `Project SDK version is \"${actualCleanedSdkVersion}\", but expected \"${expectedCleanedSdkVersion}\"`\n if (utils.semver.releases.lt(diff, 'minor')) {\n this.logger.debug(`${errorMsg}. This may cause compatibility issues.`)\n return\n }\n\n this.logger.warn(chalk.bold(`${errorMsg}. This will cause compatibility issues.`))\n } catch (thrown) {\n const err = errors.BotpressCLIError.map(thrown)\n this.logger.debug(`Failed to check if sdk is up to date: ${err.message}`)\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,sBAAqB;AACrB,mBAAkB;AAClB,gBAAe;AACf,oBAAc;AACd,kBAAoB;AACpB,oBAAmB;AAEnB,cAAyB;AAEzB,aAAwB;AACxB,aAAwB;AACxB,6BAAqD;AACrD,kCAA8C;AAE9C,YAAuB;AACvB,4BAA8B;AAkB9B,MAAM,qBAAqB,MAAM,KAAK,UAAiC;AAAA,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C7C;AAAA,MACF;AAEA,YAAM,0BAA0B,cAAAG,QAAO,MAAM,cAAAA,QAAO,OAAO,gBAAgB,CAAC;AAC5E,UAAI,CAAC,yBAAyB;AAC5B,aAAK,OAAO,MAAM,wBAAwB,2CAA2C;AACrF;AAAA,MACF;AAEA,YAAM,aAAa,MAAM,KAAK,YAAY;AAC1C,YAAM,qBAAqB,MAAM,QAAQ,eAAe,YAAY,cAAc;AAClF,UAAI,CAAC,oBAAoB;AACvB,aAAK,OAAO,MAAM,8BAA8B,qCAAqC;AACrF;AAAA,MACF;AAEA,YAAM,4BAA4B,cAAAA,QAAO,MAAM,cAAAA,QAAO,OAAO,kBAAkB,CAAC;AAChF,UAAI,CAAC,2BAA2B;AAC9B,aAAK,OAAO,MAAM,wBAAwB,yCAAyC;AACnF;AAAA,MACF;AAEA,UAAI,cAAAA,QAAO,GAAG,yBAAyB,yBAAyB,GAAG;AACjE;AAAA,MACF;AAEA,YAAM,OAAO,cAAAA,QAAO,KAAK,yBAAyB,yBAAyB;AAC3E,UAAI,CAAC,MAAM;AACT,aAAK,OAAO,MAAM,+BAA+B,iCAAiC,4BAA4B;AAC9G;AAAA,MACF;AAEA,YAAM,WAAW,2BAA2B,2CAA2C;AACvF,UAAI,MAAM,OAAO,SAAS,GAAG,MAAM,OAAO,GAAG;AAC3C,aAAK,OAAO,MAAM,GAAG,gDAAgD;AACrE;AAAA,MACF;AAEA,WAAK,OAAO,KAAK,aAAAD,QAAM,KAAK,GAAG,iDAAiD,CAAC;AAAA,IACnF,SAAS,QAAP;AACA,YAAM,MAAM,OAAO,iBAAiB,IAAI,MAAM;AAC9C,WAAK,OAAO,MAAM,yCAAyC,IAAI,SAAS;AAAA,IAC1E;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["_", "bluebird", "fs", "pathlib", "chalk", "semver"]
|
|
7
7
|
}
|
|
@@ -39,8 +39,44 @@ const requireJsCode = (code) => {
|
|
|
39
39
|
const fileid = import_path.default.join(filedir, filename);
|
|
40
40
|
const m = new import_module.default(fileid);
|
|
41
41
|
m.filename = filename;
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
try {
|
|
43
|
+
m._compile(code, filename);
|
|
44
|
+
return m.exports;
|
|
45
|
+
} catch (thrown) {
|
|
46
|
+
const error = thrown instanceof Error ? thrown : new Error(`${thrown}`);
|
|
47
|
+
throw _injectStackTrace(error, code, filename);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const STACK_TRACE_SURROUNDING_LINES = 3;
|
|
51
|
+
const _injectStackTrace = (compileError, code, filename) => {
|
|
52
|
+
if (!compileError.stack || !compileError.stack.includes(`${filename}:`)) {
|
|
53
|
+
return compileError;
|
|
54
|
+
}
|
|
55
|
+
const [, locationInfo] = compileError.stack.split(`${filename}:`, 2);
|
|
56
|
+
if (!locationInfo) {
|
|
57
|
+
return compileError;
|
|
58
|
+
}
|
|
59
|
+
const [lineNoStr, _rest] = locationInfo.split(":", 2);
|
|
60
|
+
if (!lineNoStr || !_rest) {
|
|
61
|
+
return compileError;
|
|
62
|
+
}
|
|
63
|
+
const [columnStr] = _rest.split(")", 1);
|
|
64
|
+
if (!columnStr) {
|
|
65
|
+
return compileError;
|
|
66
|
+
}
|
|
67
|
+
const lineNo = parseInt(lineNoStr);
|
|
68
|
+
const column = parseInt(columnStr);
|
|
69
|
+
const allLines = code.split("\n");
|
|
70
|
+
const linesBefore = allLines.slice(Math.max(0, lineNo - 1 - STACK_TRACE_SURROUNDING_LINES), lineNo - 1);
|
|
71
|
+
const offendingLine = allLines[lineNo - 1];
|
|
72
|
+
const caretLine = " ".repeat(column - 1) + "^";
|
|
73
|
+
const linesAfter = allLines.slice(lineNo, Math.min(allLines.length, lineNo + STACK_TRACE_SURROUNDING_LINES));
|
|
74
|
+
const stackTrace = [...linesBefore, offendingLine, caretLine, ...linesAfter].join("\n");
|
|
75
|
+
return new Error(`${compileError.message}
|
|
76
|
+
|
|
77
|
+
Offending code:
|
|
78
|
+
|
|
79
|
+
${stackTrace}`, { cause: compileError });
|
|
44
80
|
};
|
|
45
81
|
// Annotate the CommonJS export names for ESM import in node:
|
|
46
82
|
0 && (module.exports = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils/require-utils.ts"],
|
|
4
|
-
"sourcesContent": ["import Module from 'module'\nimport pathlib from 'path'\n\nexport const requireJsFile = <T>(path: string): T => {\n return require(path)\n}\n\nexport const requireJsCode = <T>(code: string): T => {\n const filedir = 'tmp'\n const filename = `${Date.now()}.js`\n\n const fileid = pathlib.join(filedir, filename)\n\n const m = new Module(fileid)\n m.filename = filename\n // @ts-ignore\n
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAmB;AACnB,kBAAoB;AAEb,MAAM,gBAAgB,CAAI,SAAoB;AACnD,SAAO,QAAQ,IAAI;AACrB;AAEO,MAAM,gBAAgB,CAAI,SAAoB;AACnD,QAAM,UAAU;AAChB,QAAM,WAAW,GAAG,KAAK,IAAI;AAE7B,QAAM,SAAS,YAAAA,QAAQ,KAAK,SAAS,QAAQ;AAE7C,QAAM,IAAI,IAAI,cAAAC,QAAO,MAAM;AAC3B,IAAE,WAAW;AAEb,
|
|
4
|
+
"sourcesContent": ["import Module from 'module'\nimport pathlib from 'path'\n\nexport const requireJsFile = <T>(path: string): T => {\n return require(path)\n}\n\nexport const requireJsCode = <T>(code: string): T => {\n const filedir = 'tmp'\n const filename = `${Date.now()}.js`\n\n const fileid = pathlib.join(filedir, filename)\n\n const m = new Module(fileid)\n m.filename = filename\n\n try {\n // @ts-ignore\n m._compile(code, filename)\n return m.exports\n } catch (thrown: unknown) {\n const error = thrown instanceof Error ? thrown : new Error(`${thrown}`)\n throw _injectStackTrace(error, code, filename)\n }\n}\n\nconst STACK_TRACE_SURROUNDING_LINES = 3\n\nconst _injectStackTrace = (compileError: Error, code: string, filename: string): Error => {\n if (!compileError.stack || !compileError.stack.includes(`${filename}:`)) {\n return compileError\n }\n\n // Extract line and column from the stack trace:\n const [, locationInfo] = compileError.stack.split(`${filename}:`, 2)\n if (!locationInfo) {\n return compileError\n }\n\n const [lineNoStr, _rest] = locationInfo.split(':', 2)\n if (!lineNoStr || !_rest) {\n return compileError\n }\n\n const [columnStr] = _rest.split(')', 1)\n if (!columnStr) {\n return compileError\n }\n\n const lineNo = parseInt(lineNoStr)\n const column = parseInt(columnStr)\n\n // Build the stack trace:\n const allLines = code.split('\\n')\n const linesBefore = allLines.slice(Math.max(0, lineNo - 1 - STACK_TRACE_SURROUNDING_LINES), lineNo - 1)\n const offendingLine = allLines[lineNo - 1]\n const caretLine = ' '.repeat(column - 1) + '^'\n const linesAfter = allLines.slice(lineNo, Math.min(allLines.length, lineNo + STACK_TRACE_SURROUNDING_LINES))\n const stackTrace = [...linesBefore, offendingLine, caretLine, ...linesAfter].join('\\n')\n\n return new Error(`${compileError.message}\\n\\nOffending code:\\n\\n${stackTrace}`, { cause: compileError })\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAmB;AACnB,kBAAoB;AAEb,MAAM,gBAAgB,CAAI,SAAoB;AACnD,SAAO,QAAQ,IAAI;AACrB;AAEO,MAAM,gBAAgB,CAAI,SAAoB;AACnD,QAAM,UAAU;AAChB,QAAM,WAAW,GAAG,KAAK,IAAI;AAE7B,QAAM,SAAS,YAAAA,QAAQ,KAAK,SAAS,QAAQ;AAE7C,QAAM,IAAI,IAAI,cAAAC,QAAO,MAAM;AAC3B,IAAE,WAAW;AAEb,MAAI;AAEF,MAAE,SAAS,MAAM,QAAQ;AACzB,WAAO,EAAE;AAAA,EACX,SAAS,QAAP;AACA,UAAM,QAAQ,kBAAkB,QAAQ,SAAS,IAAI,MAAM,GAAG,QAAQ;AACtE,UAAM,kBAAkB,OAAO,MAAM,QAAQ;AAAA,EAC/C;AACF;AAEA,MAAM,gCAAgC;AAEtC,MAAM,oBAAoB,CAAC,cAAqB,MAAc,aAA4B;AACxF,MAAI,CAAC,aAAa,SAAS,CAAC,aAAa,MAAM,SAAS,GAAG,WAAW,GAAG;AACvE,WAAO;AAAA,EACT;AAGA,QAAM,CAAC,EAAE,YAAY,IAAI,aAAa,MAAM,MAAM,GAAG,aAAa,CAAC;AACnE,MAAI,CAAC,cAAc;AACjB,WAAO;AAAA,EACT;AAEA,QAAM,CAAC,WAAW,KAAK,IAAI,aAAa,MAAM,KAAK,CAAC;AACpD,MAAI,CAAC,aAAa,CAAC,OAAO;AACxB,WAAO;AAAA,EACT;AAEA,QAAM,CAAC,SAAS,IAAI,MAAM,MAAM,KAAK,CAAC;AACtC,MAAI,CAAC,WAAW;AACd,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,SAAS,SAAS;AACjC,QAAM,SAAS,SAAS,SAAS;AAGjC,QAAM,WAAW,KAAK,MAAM,IAAI;AAChC,QAAM,cAAc,SAAS,MAAM,KAAK,IAAI,GAAG,SAAS,IAAI,6BAA6B,GAAG,SAAS,CAAC;AACtG,QAAM,gBAAgB,SAAS,SAAS;AACxC,QAAM,YAAY,IAAI,OAAO,SAAS,CAAC,IAAI;AAC3C,QAAM,aAAa,SAAS,MAAM,QAAQ,KAAK,IAAI,SAAS,QAAQ,SAAS,6BAA6B,CAAC;AAC3G,QAAM,aAAa,CAAC,GAAG,aAAa,eAAe,WAAW,GAAG,UAAU,EAAE,KAAK,IAAI;AAEtF,SAAO,IAAI,MAAM,GAAG,aAAa;AAAA;AAAA;AAAA;AAAA,EAAiC,cAAc,EAAE,OAAO,aAAa,CAAC;AACzG;",
|
|
6
6
|
"names": ["pathlib", "Module"]
|
|
7
7
|
}
|
|
@@ -28,4 +28,23 @@ var requireUtils = __toESM(require("./require-utils"));
|
|
|
28
28
|
const result = requireUtils.requireJsCode(code);
|
|
29
29
|
(0, import_vitest.expect)(result.foo).toBe("bar");
|
|
30
30
|
});
|
|
31
|
+
const getError = (fn) => {
|
|
32
|
+
try {
|
|
33
|
+
fn();
|
|
34
|
+
return;
|
|
35
|
+
} catch (thrown) {
|
|
36
|
+
return thrown instanceof Error ? thrown : new Error(`${thrown}`);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
(0, import_vitest.test)("require js code should indicate issue in stack trace", () => {
|
|
40
|
+
const code = `
|
|
41
|
+
var foo = undefined
|
|
42
|
+
module.exports = {
|
|
43
|
+
bar: foo.bar
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
const error = getError(() => requireUtils.requireJsCode(code));
|
|
47
|
+
(0, import_vitest.expect)(error).toBeDefined();
|
|
48
|
+
(0, import_vitest.expect)(error?.message).toContain("bar: foo.bar");
|
|
49
|
+
});
|
|
31
50
|
//# sourceMappingURL=require-utils.test.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils/require-utils.test.ts"],
|
|
4
|
-
"sourcesContent": ["import { test, expect } from 'vitest'\nimport * as requireUtils from './require-utils'\n\ntest('require js code should do as its name suggests', () => {\n const code = `\n module.exports = {\n foo: 'bar'\n }\n `\n\n const result = requireUtils.requireJsCode<{ foo: string }>(code)\n\n expect(result.foo).toBe('bar')\n})\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA,oBAA6B;AAC7B,mBAA8B;AAAA,IAE9B,oBAAK,kDAAkD,MAAM;AAC3D,QAAM,OAAO;AAAA;AAAA;AAAA;AAAA;AAMb,QAAM,SAAS,aAAa,cAA+B,IAAI;AAE/D,4BAAO,OAAO,GAAG,EAAE,KAAK,KAAK;AAC/B,CAAC;",
|
|
4
|
+
"sourcesContent": ["import { test, expect } from 'vitest'\nimport * as requireUtils from './require-utils'\n\ntest('require js code should do as its name suggests', () => {\n const code = `\n module.exports = {\n foo: 'bar'\n }\n `\n\n const result = requireUtils.requireJsCode<{ foo: string }>(code)\n\n expect(result.foo).toBe('bar')\n})\n\nconst getError = (fn: () => void): Error | undefined => {\n try {\n fn()\n return\n } catch (thrown: unknown) {\n return thrown instanceof Error ? thrown : new Error(`${thrown}`)\n }\n}\n\ntest('require js code should indicate issue in stack trace', () => {\n const code = `\n var foo = undefined\n module.exports = {\n bar: foo.bar\n }\n `\n\n const error = getError(() => requireUtils.requireJsCode(code))\n\n expect(error).toBeDefined()\n expect(error?.message).toContain('bar: foo.bar')\n})\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA,oBAA6B;AAC7B,mBAA8B;AAAA,IAE9B,oBAAK,kDAAkD,MAAM;AAC3D,QAAM,OAAO;AAAA;AAAA;AAAA;AAAA;AAMb,QAAM,SAAS,aAAa,cAA+B,IAAI;AAE/D,4BAAO,OAAO,GAAG,EAAE,KAAK,KAAK;AAC/B,CAAC;AAED,MAAM,WAAW,CAAC,OAAsC;AACtD,MAAI;AACF,OAAG;AACH;AAAA,EACF,SAAS,QAAP;AACA,WAAO,kBAAkB,QAAQ,SAAS,IAAI,MAAM,GAAG,QAAQ;AAAA,EACjE;AACF;AAAA,IAEA,oBAAK,wDAAwD,MAAM;AACjE,QAAM,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAOb,QAAM,QAAQ,SAAS,MAAM,aAAa,cAAc,IAAI,CAAC;AAE7D,4BAAO,KAAK,EAAE,YAAY;AAC1B,4BAAO,OAAO,OAAO,EAAE,UAAU,cAAc;AACjD,CAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@botpress/cli",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.6",
|
|
4
4
|
"description": "Botpress CLI",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "pnpm run bundle && pnpm run template:gen",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
},
|
|
21
21
|
"main": "dist/index.js",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@botpress/client": "0.30.
|
|
24
|
-
"@botpress/sdk": "0.11.
|
|
23
|
+
"@botpress/client": "0.30.2",
|
|
24
|
+
"@botpress/sdk": "0.11.4",
|
|
25
25
|
"@bpinternal/const": "^0.0.20",
|
|
26
26
|
"@bpinternal/tunnel": "^0.1.1",
|
|
27
27
|
"@bpinternal/yargs-extra": "^0.0.3",
|