@botpress/cli 0.8.3 → 0.8.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.
@@ -45,6 +45,19 @@ class ApiClient {
45
45
  token;
46
46
  workspaceId;
47
47
  static newClient = (props, logger) => new ApiClient(props, logger);
48
+ get isBotpressWorkspace() {
49
+ return [
50
+ "6a76fa10-e150-4ff6-8f59-a300feec06c1",
51
+ "95de33eb-1551-4af9-9088-e5dcb02efd09",
52
+ "11111111-1111-1111-aaaa-111111111111"
53
+ ].includes(this.workspaceId);
54
+ }
55
+ async getWorkspace() {
56
+ return this.client.getWorkspace({ id: this.workspaceId });
57
+ }
58
+ async updateWorkspace(props) {
59
+ return this.client.updateWorkspace({ id: this.workspaceId, ...props });
60
+ }
48
61
  async findIntegration(ref) {
49
62
  const formatted = (0, import_integration_ref.formatIntegrationRef)(ref);
50
63
  const privateIntegration = await this.findPrivateIntegration(ref);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/api/client.ts"],
4
- "sourcesContent": ["import * as client from '@botpress/client'\nimport _ from 'lodash'\nimport { formatIntegrationRef, ApiIntegrationRef as IntegrationRef } from '../integration-ref'\nimport type { Logger } from '../logger'\nimport * as paging from './paging'\n\nexport type PageLister<R extends object> = (t: { nextToken?: string }) => Promise<R & { meta: { nextToken?: string } }>\n\nexport type ApiClientProps = {\n apiUrl: string\n token: string\n workspaceId: string\n}\n\nexport type ApiClientFactory = {\n newClient: (props: ApiClientProps, logger: Logger) => ApiClient\n}\n\ntype PublicIntegration = client.Integration\ntype PrivateIntegration = client.Integration & { workspaceId: string }\ntype Integration = client.Integration & { workspaceId?: string }\n\n/**\n * This class is used to wrap the Botpress API and provide a more convenient way to interact with it.\n */\nexport class ApiClient {\n public readonly client: client.Client\n public readonly url: string\n public readonly token: string\n public readonly workspaceId: string\n\n public static newClient = (props: ApiClientProps, logger: Logger) => new ApiClient(props, logger)\n\n public constructor(props: ApiClientProps, private _logger: Logger) {\n const { apiUrl, token, workspaceId } = props\n this.client = new client.Client({ apiUrl, token, workspaceId })\n this.url = apiUrl\n this.token = token\n this.workspaceId = workspaceId\n }\n\n public async findIntegration(ref: IntegrationRef): Promise<Integration | undefined> {\n const formatted = formatIntegrationRef(ref)\n\n const privateIntegration = await this.findPrivateIntegration(ref)\n if (privateIntegration) {\n this._logger.debug(`Found integration \"${formatted}\" in workspace`)\n return privateIntegration\n }\n\n const publicIntegration = await this.findPublicIntegration(ref)\n if (publicIntegration) {\n this._logger.debug(`Found integration \"${formatted}\" in hub`)\n return publicIntegration\n }\n\n return\n }\n\n public async findPrivateIntegration(ref: IntegrationRef): Promise<PrivateIntegration | undefined> {\n const { workspaceId } = this\n if (ref.type === 'id') {\n return this.validateStatus(\n () => this.client.getIntegration(ref).then((r) => ({ ...r.integration, workspaceId })),\n 404\n )\n }\n return this.validateStatus(\n () => this.client.getIntegrationByName(ref).then((r) => ({ ...r.integration, workspaceId })),\n 404\n )\n }\n\n public async findPublicIntegration(ref: IntegrationRef): Promise<PublicIntegration | undefined> {\n if (ref.type === 'id') {\n return this.validateStatus(() => this.client.getPublicIntegrationById(ref).then((r) => r.integration), 404)\n }\n return this.validateStatus(() => this.client.getPublicIntegration(ref).then((r) => r.integration), 404)\n }\n\n public async testLogin(): Promise<void> {\n await this.client.listBots({})\n }\n\n public listAllPages = paging.listAllPages\n\n public async validateStatus<V>(fn: () => Promise<V>, allowedStatuses: number | number[]): Promise<V | undefined> {\n try {\n const v = await fn()\n return v\n } catch (err) {\n const allowedStatusesArray = _.isArray(allowedStatuses) ? allowedStatuses : [allowedStatuses]\n const isAllowed = client.isApiError(err) && err.code && allowedStatusesArray.includes(err.code)\n if (isAllowed) {\n return\n }\n throw err\n }\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,aAAwB;AACxB,oBAAc;AACd,6BAA0E;AAE1E,aAA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,EAAE,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,GAAG;AAAA,IAC5G;AACA,WAAO,KAAK,eAAe,MAAM,KAAK,OAAO,qBAAqB,GAAG,EAAE,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,GAAG;AAAA,EACxG;AAAA,EAEA,MAAa,YAA2B;AACtC,UAAM,KAAK,OAAO,SAAS,CAAC,CAAC;AAAA,EAC/B;AAAA,EAEO,eAAe,OAAO;AAAA,EAE7B,MAAa,eAAkB,IAAsB,iBAA4D;AAC/G,QAAI;AACF,YAAM,IAAI,MAAM,GAAG;AACnB,aAAO;AAAA,IACT,SAAS,KAAP;AACA,YAAM,uBAAuB,cAAAA,QAAE,QAAQ,eAAe,IAAI,kBAAkB,CAAC,eAAe;AAC5F,YAAM,YAAY,OAAO,WAAW,GAAG,KAAK,IAAI,QAAQ,qBAAqB,SAAS,IAAI,IAAI;AAC9F,UAAI,WAAW;AACb;AAAA,MACF;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import * as client from '@botpress/client'\nimport _ from 'lodash'\nimport { formatIntegrationRef, ApiIntegrationRef as IntegrationRef } from '../integration-ref'\nimport type { Logger } from '../logger'\nimport * as paging from './paging'\n\nexport type PageLister<R extends object> = (t: { nextToken?: string }) => Promise<R & { meta: { nextToken?: string } }>\n\nexport type ApiClientProps = {\n apiUrl: string\n token: string\n workspaceId: string\n}\n\nexport type ApiClientFactory = {\n newClient: (props: ApiClientProps, logger: Logger) => ApiClient\n}\n\ntype PublicIntegration = client.Integration\ntype PrivateIntegration = client.Integration & { workspaceId: string }\ntype Integration = client.Integration & { workspaceId?: string }\n\ntype BaseOperation = (...args: any[]) => Promise<any>\ntype Operations = {\n [K in keyof client.Client as client.Client[K] extends BaseOperation ? K : never]: client.Client[K]\n}\ntype Requests = {\n [K in keyof Operations]: Parameters<Operations[K]>[0]\n}\ntype Responses = {\n [K in keyof Operations]: ReturnType<Operations[K]>\n}\n\n/**\n * This class is used to wrap the Botpress API and provide a more convenient way to interact with it.\n */\nexport class ApiClient {\n public readonly client: client.Client\n public readonly url: string\n public readonly token: string\n public readonly workspaceId: string\n\n public static newClient = (props: ApiClientProps, logger: Logger) => new ApiClient(props, logger)\n\n public constructor(props: ApiClientProps, private _logger: Logger) {\n const { apiUrl, token, workspaceId } = props\n this.client = new client.Client({ apiUrl, token, workspaceId })\n this.url = apiUrl\n this.token = token\n this.workspaceId = workspaceId\n }\n\n public get isBotpressWorkspace(): boolean {\n return [\n '6a76fa10-e150-4ff6-8f59-a300feec06c1',\n '95de33eb-1551-4af9-9088-e5dcb02efd09',\n '11111111-1111-1111-aaaa-111111111111',\n ].includes(this.workspaceId)\n }\n\n public async getWorkspace(): Promise<Responses['getWorkspace']> {\n return this.client.getWorkspace({ id: this.workspaceId })\n }\n\n public async updateWorkspace(props: Omit<Requests['updateWorkspace'], 'id'>): Promise<Responses['updateWorkspace']> {\n return this.client.updateWorkspace({ id: this.workspaceId, ...props })\n }\n\n public async findIntegration(ref: IntegrationRef): Promise<Integration | undefined> {\n const formatted = formatIntegrationRef(ref)\n\n const privateIntegration = await this.findPrivateIntegration(ref)\n if (privateIntegration) {\n this._logger.debug(`Found integration \"${formatted}\" in workspace`)\n return privateIntegration\n }\n\n const publicIntegration = await this.findPublicIntegration(ref)\n if (publicIntegration) {\n this._logger.debug(`Found integration \"${formatted}\" in hub`)\n return publicIntegration\n }\n\n return\n }\n\n public async findPrivateIntegration(ref: IntegrationRef): Promise<PrivateIntegration | undefined> {\n const { workspaceId } = this\n if (ref.type === 'id') {\n return this.validateStatus(\n () => this.client.getIntegration(ref).then((r) => ({ ...r.integration, workspaceId })),\n 404\n )\n }\n return this.validateStatus(\n () => this.client.getIntegrationByName(ref).then((r) => ({ ...r.integration, workspaceId })),\n 404\n )\n }\n\n public async findPublicIntegration(ref: IntegrationRef): Promise<PublicIntegration | undefined> {\n if (ref.type === 'id') {\n return this.validateStatus(() => this.client.getPublicIntegrationById(ref).then((r) => r.integration), 404)\n }\n return this.validateStatus(() => this.client.getPublicIntegration(ref).then((r) => r.integration), 404)\n }\n\n public async testLogin(): Promise<void> {\n await this.client.listBots({})\n }\n\n public listAllPages = paging.listAllPages\n\n public async validateStatus<V>(fn: () => Promise<V>, allowedStatuses: number | number[]): Promise<V | undefined> {\n try {\n const v = await fn()\n return v\n } catch (err) {\n const allowedStatusesArray = _.isArray(allowedStatuses) ? allowedStatuses : [allowedStatuses]\n const isAllowed = client.isApiError(err) && err.code && allowedStatusesArray.includes(err.code)\n if (isAllowed) {\n return\n }\n throw err\n }\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,aAAwB;AACxB,oBAAc;AACd,6BAA0E;AAE1E,aAAwB;AAgCjB,MAAM,UAAU;AAAA,EAQd,YAAY,OAA+B,SAAiB;AAAjB;AAChD,UAAM,EAAE,QAAQ,OAAO,YAAY,IAAI;AACvC,SAAK,SAAS,IAAI,OAAO,OAAO,EAAE,QAAQ,OAAO,YAAY,CAAC;AAC9D,SAAK,MAAM;AACX,SAAK,QAAQ;AACb,SAAK,cAAc;AAAA,EACrB;AAAA,EAbgB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEhB,OAAc,YAAY,CAAC,OAAuB,WAAmB,IAAI,UAAU,OAAO,MAAM;AAAA,EAUhG,IAAW,sBAA+B;AACxC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,IACF,EAAE,SAAS,KAAK,WAAW;AAAA,EAC7B;AAAA,EAEA,MAAa,eAAmD;AAC9D,WAAO,KAAK,OAAO,aAAa,EAAE,IAAI,KAAK,YAAY,CAAC;AAAA,EAC1D;AAAA,EAEA,MAAa,gBAAgB,OAAuF;AAClH,WAAO,KAAK,OAAO,gBAAgB,EAAE,IAAI,KAAK,aAAa,GAAG,MAAM,CAAC;AAAA,EACvE;AAAA,EAEA,MAAa,gBAAgB,KAAuD;AAClF,U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AP;AACA,YAAM,uBAAuB,cAAAA,QAAE,QAAQ,eAAe,IAAI,kBAAkB,CAAC,eAAe;AAC5F,YAAM,YAAY,OAAO,WAAW,GAAG,KAAK,IAAI,QAAQ,qBAAqB,SAAS,IAAI,IAAI;AAC9F,UAAI,WAAW;AACb;AAAA,MACF;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACF;",
6
6
  "names": ["_"]
7
7
  }
@@ -53,6 +53,7 @@ class DeployCommand extends import_project_command.ProjectCommand {
53
53
  async _deployIntegration(api, integrationDef) {
54
54
  const outfile = this.projectPaths.abs.outFile;
55
55
  const code = await fs.promises.readFile(outfile, "utf-8");
56
+ integrationDef = await this._manageWorkspaceHandle(api, integrationDef);
56
57
  const {
57
58
  name,
58
59
  version,
@@ -70,17 +71,22 @@ class DeployCommand extends import_project_command.ProjectCommand {
70
71
  const fallbackHandlerScriptFileContent = await this._readFile(identifier?.fallbackHandlerScript);
71
72
  const identifierLinkTemplateFileContent = await this._readFile(configuration?.identifier?.linkTemplateScript);
72
73
  const integration = await api.findIntegration({ type: "name", name, version });
73
- if (integration && !integration.workspaceId) {
74
+ if (integration && integration.workspaceId !== api.workspaceId) {
74
75
  throw new errors.BotpressCLIError(
75
- `Public integration ${integrationDef.name} v${integrationDef.version} is already deployed in another workspace.`
76
+ `Public integration ${name} v${version} is already deployed in another workspace.`
77
+ );
78
+ }
79
+ if (integration && integration.public && !api.isBotpressWorkspace) {
80
+ throw new errors.BotpressCLIError(
81
+ `Integration ${name} v${version} is already deployed publicly and cannot be updated. Please bump the version.`
76
82
  );
77
83
  }
78
84
  let message;
79
85
  if (integration) {
80
86
  this.logger.warn("Integration already exists. If you decide to deploy, it will overwrite the existing one.");
81
- message = `Are you sure you want to override integration ${integrationDef.name} v${integrationDef.version}?`;
87
+ message = `Are you sure you want to override integration ${name} v${version}?`;
82
88
  } else {
83
- message = `Are you sure you want to deploy integration ${integrationDef.name} v${integrationDef.version}?`;
89
+ message = `Are you sure you want to deploy integration ${name} v${version}?`;
84
90
  }
85
91
  const confirm = await this.prompt.confirm(message);
86
92
  if (!confirm) {
@@ -106,7 +112,7 @@ class DeployCommand extends import_project_command.ProjectCommand {
106
112
  },
107
113
  public: this.argv.public
108
114
  };
109
- const startedMessage = `Deploying integration ${import_chalk.default.bold(integrationDef.name)} v${integrationDef.version}...`;
115
+ const startedMessage = `Deploying integration ${import_chalk.default.bold(name)} v${version}...`;
110
116
  const successMessage = "Integration deployed";
111
117
  if (integration) {
112
118
  const updateBody = (0, import_integration_body.prepareUpdateIntegrationBody)(
@@ -123,7 +129,7 @@ class DeployCommand extends import_project_command.ProjectCommand {
123
129
  const line = this.logger.line();
124
130
  line.started(startedMessage);
125
131
  await api.client.updateIntegration(updateBody).catch((thrown) => {
126
- throw errors.BotpressCLIError.wrap(thrown, `Could not update integration "${integrationDef.name}"`);
132
+ throw errors.BotpressCLIError.wrap(thrown, `Could not update integration "${name}"`);
127
133
  });
128
134
  line.success(successMessage);
129
135
  } else {
@@ -133,7 +139,7 @@ class DeployCommand extends import_project_command.ProjectCommand {
133
139
  const line = this.logger.line();
134
140
  line.started(startedMessage);
135
141
  await api.client.createIntegration(createBody).catch((thrown) => {
136
- throw errors.BotpressCLIError.wrap(thrown, `Could not create integration "${integrationDef.name}"`);
142
+ throw errors.BotpressCLIError.wrap(thrown, `Could not create integration "${name}"`);
137
143
  });
138
144
  line.success(successMessage);
139
145
  }
@@ -251,6 +257,88 @@ class DeployCommand extends import_project_command.ProjectCommand {
251
257
  });
252
258
  return fetchedBot;
253
259
  }
260
+ async _manageWorkspaceHandle(api, integration) {
261
+ const { name: localName, workspaceHandle: localHandle } = this._parseIntegrationName(integration.name);
262
+ if (!localHandle && api.isBotpressWorkspace) {
263
+ this.logger.debug("Botpress workspace detected; workspace handle omitted");
264
+ return integration;
265
+ }
266
+ const { handle: remoteHandle, name: workspaceName } = await api.getWorkspace().catch((thrown) => {
267
+ throw errors.BotpressCLIError.wrap(thrown, "Could not fetch workspace");
268
+ });
269
+ if (localHandle && remoteHandle) {
270
+ if (localHandle !== remoteHandle) {
271
+ throw new errors.BotpressCLIError(
272
+ `Your current workspace handle is "${remoteHandle}" but the integration handle is "${localHandle}".`
273
+ );
274
+ }
275
+ return integration;
276
+ }
277
+ const workspaceHandleIsMandatoryMsg = "Cannot deploy integration without workspace handle";
278
+ if (!localHandle && remoteHandle) {
279
+ const confirmAddHandle = await this.prompt.confirm(
280
+ `Your current workspace handle is "${remoteHandle}". Do you want to use the name "${remoteHandle}/${localName}"?`
281
+ );
282
+ if (!confirmAddHandle) {
283
+ throw new errors.BotpressCLIError(workspaceHandleIsMandatoryMsg);
284
+ }
285
+ const newName2 = `${remoteHandle}/${localName}`;
286
+ return { ...integration, name: newName2 };
287
+ }
288
+ if (localHandle && !remoteHandle) {
289
+ const { available } = await api.client.checkHandleAvailability({ handle: localHandle }).catch((thrown) => {
290
+ throw errors.BotpressCLIError.wrap(thrown, "Could not check handle availability");
291
+ });
292
+ if (!available) {
293
+ throw new errors.BotpressCLIError(`Handle "${localHandle}" is not yours and is not available`);
294
+ }
295
+ const confirmClaimHandle = await this.prompt.confirm(
296
+ `Handle "${localHandle}" is available. Do you want to claim it for your workspace ${workspaceName}?`
297
+ );
298
+ if (!confirmClaimHandle) {
299
+ throw new errors.BotpressCLIError(workspaceHandleIsMandatoryMsg);
300
+ }
301
+ await api.updateWorkspace({ handle: localHandle }).catch((thrown) => {
302
+ throw errors.BotpressCLIError.wrap(thrown, `Could not claim handle "${localHandle}"`);
303
+ });
304
+ this.logger.success(`Handle "${localHandle}" is now yours!`);
305
+ return integration;
306
+ }
307
+ this.logger.warn("It seems you don't have a workspace handle yet.");
308
+ let claimedHandle = void 0;
309
+ do {
310
+ const prompted = await this.prompt.text("Please enter a workspace handle");
311
+ if (!prompted) {
312
+ throw new errors.BotpressCLIError(workspaceHandleIsMandatoryMsg);
313
+ }
314
+ const { available, suggestions } = await api.client.checkHandleAvailability({ handle: prompted });
315
+ if (!available) {
316
+ this.logger.warn(`Handle "${prompted}" is not available. Suggestions: ${suggestions.join(", ")}`);
317
+ continue;
318
+ }
319
+ claimedHandle = prompted;
320
+ await api.updateWorkspace({ handle: claimedHandle }).catch((thrown) => {
321
+ throw errors.BotpressCLIError.wrap(thrown, `Could not claim handle "${claimedHandle}"`);
322
+ });
323
+ } while (!claimedHandle);
324
+ this.logger.success(`Handle "${claimedHandle}" is yours!`);
325
+ const newName = `${claimedHandle}/${localName}`;
326
+ return { ...integration, name: newName };
327
+ }
328
+ _parseIntegrationName = (integrationName) => {
329
+ const parts = integrationName.split("/");
330
+ if (parts.length > 2) {
331
+ throw new errors.BotpressCLIError(
332
+ `Invalid integration name "${integrationName}": a single forward slash is allowed`
333
+ );
334
+ }
335
+ if (parts.length === 2) {
336
+ const [workspaceHandle, name2] = parts;
337
+ return { name: name2, workspaceHandle };
338
+ }
339
+ const [name] = parts;
340
+ return { name };
341
+ };
254
342
  }
255
343
  // Annotate the CommonJS export names for ESM import in node:
256
344
  0 && (module.exports = {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/command-implementations/deploy-command.ts"],
4
- "sourcesContent": ["import type * as client from '@botpress/client'\nimport type * as sdk from '@botpress/sdk'\nimport chalk from 'chalk'\nimport * as fs from 'fs'\nimport { prepareCreateBotBody, prepareUpdateBotBody } from '../api/bot-body'\nimport type { ApiClient } from '../api/client'\nimport {\n prepareUpdateIntegrationBody,\n CreateIntegrationBody,\n prepareCreateIntegrationBody,\n} from '../api/integration-body'\nimport type commandDefinitions from '../command-definitions'\nimport * as errors from '../errors'\nimport * as utils from '../utils'\nimport { BuildCommand } from './build-command'\nimport { ProjectCommand } from './project-command'\n\nexport type DeployCommandDefinition = typeof commandDefinitions.deploy\nexport class DeployCommand extends ProjectCommand<DeployCommandDefinition> {\n public async run(): Promise<void> {\n const api = await this.ensureLoginAndCreateClient(this.argv)\n\n if (!this.argv.noBuild) {\n await this._runBuild() // This ensures the bundle is always synced with source code\n }\n\n const integrationDef = await this.readIntegrationDefinitionFromFS()\n if (integrationDef) {\n return this._deployIntegration(api, integrationDef)\n }\n return this._deployBot(api, this.argv.botId, this.argv.createNewBot)\n }\n\n private async _runBuild() {\n return new BuildCommand(this.api, this.prompt, this.logger, this.argv).run()\n }\n\n private async _deployIntegration(api: ApiClient, integrationDef: sdk.IntegrationDefinition) {\n const outfile = this.projectPaths.abs.outFile\n const code = await fs.promises.readFile(outfile, 'utf-8')\n\n const {\n name,\n version,\n icon: iconRelativeFilePath,\n readme: readmeRelativeFilePath,\n identifier,\n configuration,\n } = integrationDef\n\n if (iconRelativeFilePath && !iconRelativeFilePath.toLowerCase().endsWith('.svg')) {\n throw new errors.BotpressCLIError('Icon must be an SVG file')\n }\n\n const iconFileContent = await this._readMediaFile('icon', iconRelativeFilePath)\n const readmeFileContent = await this._readMediaFile('readme', readmeRelativeFilePath)\n const identifierExtractScriptFileContent = await this._readFile(identifier?.extractScript)\n const fallbackHandlerScriptFileContent = await this._readFile(identifier?.fallbackHandlerScript)\n const identifierLinkTemplateFileContent = await this._readFile(configuration?.identifier?.linkTemplateScript)\n\n const integration = await api.findIntegration({ type: 'name', name, version })\n if (integration && !integration.workspaceId) {\n throw new errors.BotpressCLIError(\n `Public integration ${integrationDef.name} v${integrationDef.version} is already deployed in another workspace.`\n )\n }\n\n let message: string\n if (integration) {\n this.logger.warn('Integration already exists. If you decide to deploy, it will overwrite the existing one.')\n message = `Are you sure you want to override integration ${integrationDef.name} v${integrationDef.version}?`\n } else {\n message = `Are you sure you want to deploy integration ${integrationDef.name} v${integrationDef.version}?`\n }\n\n const confirm = await this.prompt.confirm(message)\n if (!confirm) {\n this.logger.log('Aborted')\n return\n }\n\n let createBody: CreateIntegrationBody = prepareCreateIntegrationBody(integrationDef)\n createBody = {\n ...createBody,\n code,\n icon: iconFileContent,\n readme: readmeFileContent,\n configuration: {\n ...createBody.configuration,\n identifier: {\n ...(createBody.configuration?.identifier ?? {}),\n linkTemplateScript: identifierLinkTemplateFileContent,\n },\n },\n identifier: {\n extractScript: identifierExtractScriptFileContent,\n fallbackHandlerScript: fallbackHandlerScriptFileContent,\n },\n public: this.argv.public,\n }\n\n const startedMessage = `Deploying integration ${chalk.bold(integrationDef.name)} v${integrationDef.version}...`\n const successMessage = 'Integration deployed'\n if (integration) {\n const updateBody = prepareUpdateIntegrationBody(\n {\n id: integration.id,\n ...createBody,\n public: this.argv.public,\n },\n integration\n )\n\n const { secrets: knownSecrets } = integration\n updateBody.secrets = await this.promptSecrets(integrationDef, this.argv, { knownSecrets })\n this._detectDeprecatedFeatures(integrationDef, { allowDeprecated: true })\n\n const line = this.logger.line()\n line.started(startedMessage)\n await api.client.updateIntegration(updateBody).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not update integration \"${integrationDef.name}\"`)\n })\n line.success(successMessage)\n } else {\n const createSecrets = await this.promptSecrets(integrationDef, this.argv)\n createBody.secrets = utils.records.filterValues(createSecrets, utils.guards.is.notNull)\n this._detectDeprecatedFeatures(integrationDef, this.argv)\n\n const line = this.logger.line()\n line.started(startedMessage)\n await api.client.createIntegration(createBody).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not create integration \"${integrationDef.name}\"`)\n })\n line.success(successMessage)\n }\n }\n\n private _detectDeprecatedFeatures(\n integrationDef: sdk.IntegrationDefinition,\n opts: { allowDeprecated?: boolean } = {}\n ) {\n const deprecatedFields: string[] = []\n const { user, channels } = integrationDef\n if (user?.creation?.enabled) {\n deprecatedFields.push('user.creation')\n }\n\n for (const [channelName, channel] of Object.entries(channels ?? {})) {\n if (channel?.conversation?.creation?.enabled) {\n deprecatedFields.push(`channels.${channelName}.creation`)\n }\n }\n\n if (!deprecatedFields.length) {\n return\n }\n\n const errorMessage = `The following fields of the integration's definition are deprecated: ${deprecatedFields.join(\n ', '\n )}`\n\n if (opts.allowDeprecated) {\n this.logger.warn(errorMessage)\n } else {\n throw new errors.BotpressCLIError(errorMessage)\n }\n }\n\n private _readFile = async (filePath: string | undefined): Promise<string | undefined> => {\n if (!filePath) {\n return undefined\n }\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 _readMediaFile = async (\n filePurpose: 'icon' | 'readme',\n filePath: string | undefined\n ): Promise<string | undefined> => {\n if (!filePath) {\n return undefined\n }\n\n const absoluteFilePath = utils.path.absoluteFrom(this.projectPaths.abs.workDir, filePath)\n return fs.promises.readFile(absoluteFilePath, 'base64').catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not read ${filePurpose} file \"${absoluteFilePath}\"`)\n })\n }\n\n private async _deployBot(api: ApiClient, argvBotId: string | undefined, argvCreateNew: boolean | undefined) {\n const outfile = this.projectPaths.abs.outFile\n const code = await fs.promises.readFile(outfile, 'utf-8')\n const { default: botImpl } = utils.require.requireJsFile<{ default: sdk.Bot }>(outfile)\n\n let bot: client.Bot\n if (argvBotId && argvCreateNew) {\n throw new errors.BotpressCLIError('Cannot specify both --botId and --createNew')\n } else if (argvCreateNew) {\n const confirm = await this.prompt.confirm('Are you sure you want to create a new bot ?')\n if (!confirm) {\n this.logger.log('Aborted')\n return\n }\n\n bot = await this._createNewBot(api)\n } else {\n bot = await this._getExistingBot(api, argvBotId)\n\n const confirm = await this.prompt.confirm(`Are you sure you want to deploy the bot \"${bot.name}\"?`)\n if (!confirm) {\n this.logger.log('Aborted')\n return\n }\n }\n\n const line = this.logger.line()\n line.started(`Deploying bot ${chalk.bold(bot.name)}...`)\n\n const integrationInstances = await this.fetchBotIntegrationInstances(botImpl, api)\n const updateBotBody = prepareUpdateBotBody(\n {\n ...prepareCreateBotBody(botImpl),\n id: bot.id,\n code,\n integrations: integrationInstances,\n },\n bot\n )\n\n const { bot: updatedBot } = await api.client.updateBot(updateBotBody).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not update bot \"${bot.name}\"`)\n })\n line.success('Bot deployed')\n this.displayWebhookUrls(updatedBot)\n }\n\n private async _createNewBot(api: ApiClient): Promise<client.Bot> {\n const line = this.logger.line()\n const { bot: createdBot } = await api.client.createBot({}).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not create bot')\n })\n line.success(`Bot created with ID \"${createdBot.id}\" and name \"${createdBot.name}\"`)\n await this.projectCache.set('botId', createdBot.id)\n return createdBot\n }\n\n private async _getExistingBot(api: ApiClient, botId: string | undefined): Promise<client.Bot> {\n const promptedBotId = await this.projectCache.sync('botId', botId, async (defaultId) => {\n const userBots = await api\n .listAllPages(api.client.listBots, (r) => r.bots)\n .catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not fetch existing bots')\n })\n\n if (!userBots.length) {\n throw new errors.NoBotsFoundError()\n }\n\n const initial = userBots.find((bot) => bot.id === defaultId)\n\n const prompted = await this.prompt.select('Which bot do you want to deploy?', {\n initial: initial && { title: initial.name, value: initial.id },\n choices: userBots.map((bot) => ({ title: bot.name, value: bot.id })),\n })\n\n if (!prompted) {\n throw new errors.ParamRequiredError('Bot Id')\n }\n\n return prompted\n })\n\n const { bot: fetchedBot } = await api.client.getBot({ id: promptedBotId }).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not get bot info')\n })\n\n return fetchedBot\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAAkB;AAClB,SAAoB;AACpB,sBAA2D;AAE3D,8BAIO;AAEP,aAAwB;AACxB,YAAuB;AACvB,2BAA6B;AAC7B,6BAA+B;AAGxB,MAAM,sBAAsB,sCAAwC;AAAA,EACzE,MAAa,MAAqB;AAChC,UAAM,MAAM,MAAM,KAAK,2BAA2B,KAAK,IAAI;AAE3D,QAAI,CAAC,KAAK,KAAK,SAAS;AACtB,YAAM,KAAK,UAA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5E,SAAS,WAAW,EAAE,OAAO,QAAQ,MAAM,OAAO,QAAQ,GAAG;AAAA,QAC7D,SAAS,SAAS,IAAI,CAAC,SAAS,EAAE,OAAO,IAAI,MAAM,OAAO,IAAI,GAAG,EAAE;AAAA,MACrE,CAAC;AAED,UAAI,CAAC,UAAU;AACb,cAAM,IAAI,OAAO,mBAAmB,QAAQ;AAAA,MAC9C;AAEA,aAAO;AAAA,IACT,CAAC;AAED,UAAM,EAAE,KAAK,WAAW,IAAI,MAAM,IAAI,OAAO,OAAO,EAAE,IAAI,cAAc,CAAC,EAAE,MAAM,CAAC,WAAW;AAC3F,YAAM,OAAO,iBAAiB,KAAK,QAAQ,wBAAwB;AAAA,IACrE,CAAC;AAED,WAAO;AAAA,EACT;AACF;",
6
- "names": ["chalk"]
4
+ "sourcesContent": ["import type * as client from '@botpress/client'\nimport type * as sdk from '@botpress/sdk'\nimport chalk from 'chalk'\nimport * as fs from 'fs'\nimport { prepareCreateBotBody, prepareUpdateBotBody } from '../api/bot-body'\nimport type { ApiClient } from '../api/client'\nimport {\n prepareUpdateIntegrationBody,\n CreateIntegrationBody,\n prepareCreateIntegrationBody,\n} from '../api/integration-body'\nimport type commandDefinitions from '../command-definitions'\nimport * as errors from '../errors'\nimport * as utils from '../utils'\nimport { BuildCommand } from './build-command'\nimport { ProjectCommand } from './project-command'\n\nexport type DeployCommandDefinition = typeof commandDefinitions.deploy\nexport class DeployCommand extends ProjectCommand<DeployCommandDefinition> {\n public async run(): Promise<void> {\n const api = await this.ensureLoginAndCreateClient(this.argv)\n\n if (!this.argv.noBuild) {\n await this._runBuild() // This ensures the bundle is always synced with source code\n }\n\n const integrationDef = await this.readIntegrationDefinitionFromFS()\n if (integrationDef) {\n return this._deployIntegration(api, integrationDef)\n }\n return this._deployBot(api, this.argv.botId, this.argv.createNewBot)\n }\n\n private async _runBuild() {\n return new BuildCommand(this.api, this.prompt, this.logger, this.argv).run()\n }\n\n private async _deployIntegration(api: ApiClient, integrationDef: sdk.IntegrationDefinition) {\n const outfile = this.projectPaths.abs.outFile\n const code = await fs.promises.readFile(outfile, 'utf-8')\n\n integrationDef = await this._manageWorkspaceHandle(api, integrationDef)\n\n const {\n name,\n version,\n icon: iconRelativeFilePath,\n readme: readmeRelativeFilePath,\n identifier,\n configuration,\n } = integrationDef\n\n if (iconRelativeFilePath && !iconRelativeFilePath.toLowerCase().endsWith('.svg')) {\n throw new errors.BotpressCLIError('Icon must be an SVG file')\n }\n\n const iconFileContent = await this._readMediaFile('icon', iconRelativeFilePath)\n const readmeFileContent = await this._readMediaFile('readme', readmeRelativeFilePath)\n const identifierExtractScriptFileContent = await this._readFile(identifier?.extractScript)\n const fallbackHandlerScriptFileContent = await this._readFile(identifier?.fallbackHandlerScript)\n const identifierLinkTemplateFileContent = await this._readFile(configuration?.identifier?.linkTemplateScript)\n\n const integration = await api.findIntegration({ type: 'name', name, version })\n if (integration && integration.workspaceId !== api.workspaceId) {\n throw new errors.BotpressCLIError(\n `Public integration ${name} v${version} is already deployed in another workspace.`\n )\n }\n\n if (integration && integration.public && !api.isBotpressWorkspace) {\n throw new errors.BotpressCLIError(\n `Integration ${name} v${version} is already deployed publicly and cannot be updated. Please bump the version.`\n )\n }\n\n let message: string\n if (integration) {\n this.logger.warn('Integration already exists. If you decide to deploy, it will overwrite the existing one.')\n message = `Are you sure you want to override integration ${name} v${version}?`\n } else {\n message = `Are you sure you want to deploy integration ${name} v${version}?`\n }\n\n const confirm = await this.prompt.confirm(message)\n if (!confirm) {\n this.logger.log('Aborted')\n return\n }\n\n let createBody: CreateIntegrationBody = prepareCreateIntegrationBody(integrationDef)\n createBody = {\n ...createBody,\n code,\n icon: iconFileContent,\n readme: readmeFileContent,\n configuration: {\n ...createBody.configuration,\n identifier: {\n ...(createBody.configuration?.identifier ?? {}),\n linkTemplateScript: identifierLinkTemplateFileContent,\n },\n },\n identifier: {\n extractScript: identifierExtractScriptFileContent,\n fallbackHandlerScript: fallbackHandlerScriptFileContent,\n },\n public: this.argv.public,\n }\n\n const startedMessage = `Deploying integration ${chalk.bold(name)} v${version}...`\n const successMessage = 'Integration deployed'\n if (integration) {\n const updateBody = prepareUpdateIntegrationBody(\n {\n id: integration.id,\n ...createBody,\n public: this.argv.public,\n },\n integration\n )\n\n const { secrets: knownSecrets } = integration\n updateBody.secrets = await this.promptSecrets(integrationDef, this.argv, { knownSecrets })\n this._detectDeprecatedFeatures(integrationDef, { allowDeprecated: true })\n\n const line = this.logger.line()\n line.started(startedMessage)\n await api.client.updateIntegration(updateBody).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not update integration \"${name}\"`)\n })\n line.success(successMessage)\n } else {\n const createSecrets = await this.promptSecrets(integrationDef, this.argv)\n createBody.secrets = utils.records.filterValues(createSecrets, utils.guards.is.notNull)\n this._detectDeprecatedFeatures(integrationDef, this.argv)\n\n const line = this.logger.line()\n line.started(startedMessage)\n await api.client.createIntegration(createBody).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not create integration \"${name}\"`)\n })\n line.success(successMessage)\n }\n }\n\n private _detectDeprecatedFeatures(\n integrationDef: sdk.IntegrationDefinition,\n opts: { allowDeprecated?: boolean } = {}\n ) {\n const deprecatedFields: string[] = []\n const { user, channels } = integrationDef\n if (user?.creation?.enabled) {\n deprecatedFields.push('user.creation')\n }\n\n for (const [channelName, channel] of Object.entries(channels ?? {})) {\n if (channel?.conversation?.creation?.enabled) {\n deprecatedFields.push(`channels.${channelName}.creation`)\n }\n }\n\n if (!deprecatedFields.length) {\n return\n }\n\n const errorMessage = `The following fields of the integration's definition are deprecated: ${deprecatedFields.join(\n ', '\n )}`\n\n if (opts.allowDeprecated) {\n this.logger.warn(errorMessage)\n } else {\n throw new errors.BotpressCLIError(errorMessage)\n }\n }\n\n private _readFile = async (filePath: string | undefined): Promise<string | undefined> => {\n if (!filePath) {\n return undefined\n }\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 _readMediaFile = async (\n filePurpose: 'icon' | 'readme',\n filePath: string | undefined\n ): Promise<string | undefined> => {\n if (!filePath) {\n return undefined\n }\n\n const absoluteFilePath = utils.path.absoluteFrom(this.projectPaths.abs.workDir, filePath)\n return fs.promises.readFile(absoluteFilePath, 'base64').catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not read ${filePurpose} file \"${absoluteFilePath}\"`)\n })\n }\n\n private async _deployBot(api: ApiClient, argvBotId: string | undefined, argvCreateNew: boolean | undefined) {\n const outfile = this.projectPaths.abs.outFile\n const code = await fs.promises.readFile(outfile, 'utf-8')\n const { default: botImpl } = utils.require.requireJsFile<{ default: sdk.Bot }>(outfile)\n\n let bot: client.Bot\n if (argvBotId && argvCreateNew) {\n throw new errors.BotpressCLIError('Cannot specify both --botId and --createNew')\n } else if (argvCreateNew) {\n const confirm = await this.prompt.confirm('Are you sure you want to create a new bot ?')\n if (!confirm) {\n this.logger.log('Aborted')\n return\n }\n\n bot = await this._createNewBot(api)\n } else {\n bot = await this._getExistingBot(api, argvBotId)\n\n const confirm = await this.prompt.confirm(`Are you sure you want to deploy the bot \"${bot.name}\"?`)\n if (!confirm) {\n this.logger.log('Aborted')\n return\n }\n }\n\n const line = this.logger.line()\n line.started(`Deploying bot ${chalk.bold(bot.name)}...`)\n\n const integrationInstances = await this.fetchBotIntegrationInstances(botImpl, api)\n const updateBotBody = prepareUpdateBotBody(\n {\n ...prepareCreateBotBody(botImpl),\n id: bot.id,\n code,\n integrations: integrationInstances,\n },\n bot\n )\n\n const { bot: updatedBot } = await api.client.updateBot(updateBotBody).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not update bot \"${bot.name}\"`)\n })\n line.success('Bot deployed')\n this.displayWebhookUrls(updatedBot)\n }\n\n private async _createNewBot(api: ApiClient): Promise<client.Bot> {\n const line = this.logger.line()\n const { bot: createdBot } = await api.client.createBot({}).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not create bot')\n })\n line.success(`Bot created with ID \"${createdBot.id}\" and name \"${createdBot.name}\"`)\n await this.projectCache.set('botId', createdBot.id)\n return createdBot\n }\n\n private async _getExistingBot(api: ApiClient, botId: string | undefined): Promise<client.Bot> {\n const promptedBotId = await this.projectCache.sync('botId', botId, async (defaultId) => {\n const userBots = await api\n .listAllPages(api.client.listBots, (r) => r.bots)\n .catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not fetch existing bots')\n })\n\n if (!userBots.length) {\n throw new errors.NoBotsFoundError()\n }\n\n const initial = userBots.find((bot) => bot.id === defaultId)\n\n const prompted = await this.prompt.select('Which bot do you want to deploy?', {\n initial: initial && { title: initial.name, value: initial.id },\n choices: userBots.map((bot) => ({ title: bot.name, value: bot.id })),\n })\n\n if (!prompted) {\n throw new errors.ParamRequiredError('Bot Id')\n }\n\n return prompted\n })\n\n const { bot: fetchedBot } = await api.client.getBot({ id: promptedBotId }).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not get bot info')\n })\n\n return fetchedBot\n }\n\n private async _manageWorkspaceHandle(\n api: ApiClient,\n integration: sdk.IntegrationDefinition\n ): Promise<sdk.IntegrationDefinition> {\n const { name: localName, workspaceHandle: localHandle } = this._parseIntegrationName(integration.name)\n if (!localHandle && api.isBotpressWorkspace) {\n this.logger.debug('Botpress workspace detected; workspace handle omitted')\n return integration // botpress has the right to omit workspace handle\n }\n\n const { handle: remoteHandle, name: workspaceName } = await api.getWorkspace().catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not fetch workspace')\n })\n\n if (localHandle && remoteHandle) {\n if (localHandle !== remoteHandle) {\n throw new errors.BotpressCLIError(\n `Your current workspace handle is \"${remoteHandle}\" but the integration handle is \"${localHandle}\".`\n )\n }\n return integration\n }\n\n const workspaceHandleIsMandatoryMsg = 'Cannot deploy integration without workspace handle'\n\n if (!localHandle && remoteHandle) {\n const confirmAddHandle = await this.prompt.confirm(\n `Your current workspace handle is \"${remoteHandle}\". Do you want to use the name \"${remoteHandle}/${localName}\"?`\n )\n if (!confirmAddHandle) {\n throw new errors.BotpressCLIError(workspaceHandleIsMandatoryMsg)\n }\n const newName = `${remoteHandle}/${localName}`\n return { ...integration, name: newName }\n }\n\n if (localHandle && !remoteHandle) {\n const { available } = await api.client.checkHandleAvailability({ handle: localHandle }).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, 'Could not check handle availability')\n })\n\n if (!available) {\n throw new errors.BotpressCLIError(`Handle \"${localHandle}\" is not yours and is not available`)\n }\n\n const confirmClaimHandle = await this.prompt.confirm(\n `Handle \"${localHandle}\" is available. Do you want to claim it for your workspace ${workspaceName}?`\n )\n if (!confirmClaimHandle) {\n throw new errors.BotpressCLIError(workspaceHandleIsMandatoryMsg)\n }\n\n await api.updateWorkspace({ handle: localHandle }).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not claim handle \"${localHandle}\"`)\n })\n\n this.logger.success(`Handle \"${localHandle}\" is now yours!`)\n return integration\n }\n\n this.logger.warn(\"It seems you don't have a workspace handle yet.\")\n let claimedHandle: string | undefined = undefined\n do {\n const prompted = await this.prompt.text('Please enter a workspace handle')\n if (!prompted) {\n throw new errors.BotpressCLIError(workspaceHandleIsMandatoryMsg)\n }\n\n const { available, suggestions } = await api.client.checkHandleAvailability({ handle: prompted })\n if (!available) {\n this.logger.warn(`Handle \"${prompted}\" is not available. Suggestions: ${suggestions.join(', ')}`)\n continue\n }\n\n claimedHandle = prompted\n await api.updateWorkspace({ handle: claimedHandle }).catch((thrown) => {\n throw errors.BotpressCLIError.wrap(thrown, `Could not claim handle \"${claimedHandle}\"`)\n })\n } while (!claimedHandle)\n\n this.logger.success(`Handle \"${claimedHandle}\" is yours!`)\n const newName = `${claimedHandle}/${localName}`\n return { ...integration, name: newName }\n }\n\n private _parseIntegrationName = (integrationName: string): { name: string; workspaceHandle?: string } => {\n const parts = integrationName.split('/')\n if (parts.length > 2) {\n throw new errors.BotpressCLIError(\n `Invalid integration name \"${integrationName}\": a single forward slash is allowed`\n )\n }\n if (parts.length === 2) {\n const [workspaceHandle, name] = parts as [string, string]\n return { name, workspaceHandle }\n }\n const [name] = parts as [string]\n return { name }\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAAkB;AAClB,SAAoB;AACpB,sBAA2D;AAE3D,8BAIO;AAEP,aAAwB;AACxB,YAAuB;AACvB,2BAA6B;AAC7B,6BAA+B;AAGxB,MAAM,sBAAsB,sCAAwC;AAAA,EACzE,MAAa,MAAqB;AAChC,UAAM,MAAM,MAAM,KAAK,2BAA2B,KAAK,IAAI;AAE3D,QAAI,CAAC,KAAK,KAAK,SAAS;AACtB,YAAM,KAAK,UAAU;AAAA,IACvB;AAEA,UAAM,iBAAiB,MAAM,KAAK,gCAAgC;AAClE,QAAI,gBAAgB;AAClB,aAAO,KAAK,mBAAmB,KAAK,cAAc;AAAA,IACpD;AACA,WAAO,KAAK,WAAW,KAAK,KAAK,KAAK,OAAO,KAAK,KAAK,YAAY;AAAA,EACrE;AAAA,EAEA,MAAc,YAAY;AACxB,WAAO,IAAI,kCAAa,KAAK,KAAK,KAAK,QAAQ,KAAK,QAAQ,KAAK,IAAI,EAAE,IAAI;AAAA,EAC7E;AAAA,EAEA,MAAc,mBAAmB,KAAgB,gBAA2C;AAC1F,UAAM,UAAU,KAAK,aAAa,IAAI;AACtC,UAAM,OAAO,MAAM,GAAG,SAAS,SAAS,SAAS,OAAO;AAExD,qBAAiB,MAAM,KAAK,uBAAuB,KAAK,cAAc;AAEtE,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA,MAAM;AAAA,MACN,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,IACF,IAAI;AAEJ,QAAI,wBAAwB,CAAC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;AACF;",
6
+ "names": ["chalk", "newName", "name"]
7
7
  }
package/e2e/index.ts CHANGED
@@ -5,10 +5,18 @@ import { createDeployBot } from './tests/create-deploy-bot'
5
5
  import { createDeployIntegration } from './tests/create-deploy-integration'
6
6
  import { devBot } from './tests/dev-bot'
7
7
  import { requiredSecrets } from './tests/integration-secrets'
8
+ import { prependWorkspaceHandle, enforceWorkspaceHandle } from './tests/manage-workspace-handle'
8
9
  import { Test } from './typings'
9
10
  import { sleep, TmpDirectory } from './utils'
10
11
 
11
- const tests: Test[] = [createDeployBot, createDeployIntegration, devBot, requiredSecrets]
12
+ const tests: Test[] = [
13
+ createDeployBot,
14
+ createDeployIntegration,
15
+ devBot,
16
+ requiredSecrets,
17
+ prependWorkspaceHandle,
18
+ enforceWorkspaceHandle,
19
+ ]
12
20
 
13
21
  const timeout = (ms: number) =>
14
22
  sleep(ms).then(() => {
@@ -0,0 +1,105 @@
1
+ import { Client } from '@botpress/client'
2
+ import pathlib from 'path'
3
+ import impl from '../../src/command-implementations'
4
+ import { ApiIntegration, fetchAllIntegrations } from '../api'
5
+ import defaults from '../defaults'
6
+ import { Test } from '../typings'
7
+ import * as utils from '../utils'
8
+
9
+ const fetchIntegration = async (client: Client, integrationName: string): Promise<ApiIntegration | undefined> => {
10
+ const integrations = await fetchAllIntegrations(client)
11
+ return integrations.find(({ name }) => name === integrationName)
12
+ }
13
+
14
+ export const prependWorkspaceHandle: Test = {
15
+ name: 'cli should automatically preprend the workspace handle to the integration name when deploying',
16
+ handler: async ({ tmpDir, dependencies, workspaceHandle, logger, ...creds }) => {
17
+ const botpressHomeDir = pathlib.join(tmpDir, '.botpresshome')
18
+ const baseDir = pathlib.join(tmpDir, 'integrations')
19
+
20
+ const integrationSuffix = utils.getUUID()
21
+ const integrationName = `myintegration${integrationSuffix}`
22
+ const integrationDirName = integrationName
23
+ const integrationDir = pathlib.join(baseDir, integrationDirName)
24
+
25
+ const argv = {
26
+ ...defaults,
27
+ botpressHome: botpressHomeDir,
28
+ confirm: true,
29
+ ...creds,
30
+ }
31
+
32
+ const client = new Client({ apiUrl: creds.apiUrl, token: creds.token, workspaceId: creds.workspaceId })
33
+
34
+ await impl
35
+ .init({ ...argv, workDir: baseDir, name: integrationName, type: 'integration' })
36
+ .then(utils.handleExitCode)
37
+ await utils.fixBotpressDependencies({ workDir: integrationDir, target: dependencies })
38
+ await utils.npmInstall({ workDir: integrationDir }).then(utils.handleExitCode)
39
+ await impl.build({ ...argv, workDir: integrationDir }).then(utils.handleExitCode)
40
+ await impl.login({ ...argv }).then(utils.handleExitCode)
41
+
42
+ await impl
43
+ .deploy({ ...argv, createNewBot: undefined, botId: undefined, workDir: integrationDir })
44
+ .then(utils.handleExitCode)
45
+
46
+ logger.debug(`Fetching integration "${integrationName}"`)
47
+ let integration = await fetchIntegration(client, integrationName)
48
+ if (integration) {
49
+ throw new Error(`Integration ${integrationName} should not have been created`)
50
+ }
51
+
52
+ const expectedIntegrationName = `${workspaceHandle}/${integrationName}`
53
+ logger.debug(`Fetching integration "${expectedIntegrationName}"`)
54
+ integration = await fetchIntegration(client, expectedIntegrationName)
55
+ if (!integration) {
56
+ throw new Error(`Integration ${expectedIntegrationName} should have been created`)
57
+ }
58
+
59
+ logger.debug(`Deleting integration "${integrationName}"`)
60
+ await impl.integrations.subcommands.delete({ ...argv, integrationRef: integration.id }).then(({ exitCode }) => {
61
+ exitCode !== 0 && logger.warn(`Failed to delete integration "${integrationName}"`) // not enough to fail the test
62
+ })
63
+ },
64
+ }
65
+
66
+ export const enforceWorkspaceHandle: Test = {
67
+ name: 'cli should fail when attempting to deploy an integration with incorrect workspace handle',
68
+ handler: async ({ tmpDir, dependencies, ...creds }) => {
69
+ const botpressHomeDir = pathlib.join(tmpDir, '.botpresshome')
70
+ const baseDir = pathlib.join(tmpDir, 'integrations')
71
+
72
+ const randomSuffix = utils.getUUID().slice(0, 8)
73
+
74
+ const name = 'myintegration'
75
+ const handle = `myhandle${randomSuffix}`
76
+ const integrationName = `${handle}/${name}`
77
+ const integrationDirName = `${handle}-${name}`
78
+ const integrationDir = pathlib.join(baseDir, integrationDirName)
79
+
80
+ const argv = {
81
+ ...defaults,
82
+ botpressHome: botpressHomeDir,
83
+ confirm: true,
84
+ ...creds,
85
+ }
86
+
87
+ await impl
88
+ .init({ ...argv, workDir: baseDir, name: integrationName, type: 'integration' })
89
+ .then(utils.handleExitCode)
90
+ await utils.fixBotpressDependencies({ workDir: integrationDir, target: dependencies })
91
+ await utils.npmInstall({ workDir: integrationDir }).then(utils.handleExitCode)
92
+ await impl.login({ ...argv }).then(utils.handleExitCode)
93
+
94
+ const { exitCode } = await impl.deploy({
95
+ ...argv,
96
+ createNewBot: undefined,
97
+ botId: undefined,
98
+ workDir: integrationDir,
99
+ })
100
+
101
+ if (exitCode === 0) {
102
+ throw new Error(`Integration ${integrationName} should not have been deployed`)
103
+ }
104
+ },
105
+ }
package/e2e/utils.ts CHANGED
@@ -3,6 +3,7 @@ import fs from 'fs'
3
3
  import _ from 'lodash'
4
4
  import pathlib from 'path'
5
5
  import tmp from 'tmp'
6
+ import * as uuid from 'uuid'
6
7
 
7
8
  type PackageJson = {
8
9
  name: string
@@ -71,3 +72,5 @@ export const handleExitCode = ({ exitCode }: { exitCode: number }) => {
71
72
  throw new Error(`Command exited with code ${exitCode}`)
72
73
  }
73
74
  }
75
+
76
+ export const getUUID = () => uuid.v4().replace(/-/g, '')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botpress/cli",
3
- "version": "0.8.3",
3
+ "version": "0.8.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.14.2",
24
- "@botpress/sdk": "0.8.2",
23
+ "@botpress/client": "0.15.0",
24
+ "@botpress/sdk": "0.8.4",
25
25
  "@bpinternal/const": "^0.0.20",
26
26
  "@bpinternal/tunnel": "^0.1.1",
27
27
  "@bpinternal/yargs-extra": "^0.0.3",
@@ -8,8 +8,8 @@
8
8
  "author": "",
9
9
  "license": "MIT",
10
10
  "dependencies": {
11
- "@botpress/client": "0.14.2",
12
- "@botpress/sdk": "0.8.2"
11
+ "@botpress/client": "0.15.0",
12
+ "@botpress/sdk": "0.8.4"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/node": "^18.11.17",
@@ -0,0 +1,57 @@
1
+ /* tslint:disable */
2
+ /**
3
+ * This file was automatically generated by json-schema-to-typescript.
4
+ * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
5
+ * and run json-schema-to-typescript to regenerate this file.
6
+ */
7
+
8
+ export interface Bloc {
9
+ items: (
10
+ | {
11
+ type: "text";
12
+ payload: {
13
+ text: string;
14
+ };
15
+ }
16
+ | {
17
+ type: "markdown";
18
+ payload: {
19
+ markdown: string;
20
+ };
21
+ }
22
+ | {
23
+ type: "image";
24
+ payload: {
25
+ imageUrl: string;
26
+ };
27
+ }
28
+ | {
29
+ type: "audio";
30
+ payload: {
31
+ audioUrl: string;
32
+ };
33
+ }
34
+ | {
35
+ type: "video";
36
+ payload: {
37
+ videoUrl: string;
38
+ };
39
+ }
40
+ | {
41
+ type: "file";
42
+ payload: {
43
+ fileUrl: string;
44
+ title?: string;
45
+ };
46
+ }
47
+ | {
48
+ type: "location";
49
+ payload: {
50
+ latitude: number;
51
+ longitude: number;
52
+ address?: string;
53
+ title?: string;
54
+ };
55
+ }
56
+ )[];
57
+ }
@@ -23,6 +23,8 @@ import * as dropdown from "./dropdown";
23
23
  export * as dropdown from "./dropdown";
24
24
  import * as choice from "./choice";
25
25
  export * as choice from "./choice";
26
+ import * as bloc from "./bloc";
27
+ export * as bloc from "./bloc";
26
28
 
27
29
  export type Messages = {
28
30
  text: text.Text;
@@ -36,4 +38,5 @@ export type Messages = {
36
38
  card: card.Card;
37
39
  dropdown: dropdown.Dropdown;
38
40
  choice: choice.Choice;
41
+ bloc: bloc.Bloc;
39
42
  }
@@ -9,8 +9,8 @@
9
9
  "author": "",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
- "@botpress/client": "0.14.2",
13
- "@botpress/sdk": "0.8.2"
12
+ "@botpress/client": "0.15.0",
13
+ "@botpress/sdk": "0.8.4"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@types/node": "^18.11.17",
@@ -58,6 +58,9 @@ export default new botpress.Integration({
58
58
  dropdown: async () => {
59
59
  throw new NotImplementedError()
60
60
  },
61
+ bloc: async () => {
62
+ throw new NotImplementedError()
63
+ },
61
64
  },
62
65
  },
63
66
  },