@canva/cli 1.19.0 → 1.20.0

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.
Files changed (231) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +1 -9
  3. package/cli.js +408 -409
  4. package/package.json +1 -2
  5. package/templates/base/backend/base_backend/create.ts +0 -114
  6. package/templates/base/backend/database/database.ts +0 -42
  7. package/templates/base/backend/routers/auth.ts +0 -288
  8. package/templates/base/declarations/declarations.d.ts +0 -29
  9. package/templates/base/eslint.config.mjs +0 -14
  10. package/templates/base/package.json +0 -91
  11. package/templates/base/scripts/copy_env.ts +0 -13
  12. package/templates/base/scripts/ssl/ssl.ts +0 -131
  13. package/templates/base/scripts/start/app_runner.ts +0 -223
  14. package/templates/base/scripts/start/context.ts +0 -171
  15. package/templates/base/scripts/start/start.ts +0 -46
  16. package/templates/base/scripts/start.tests.ts +0 -61
  17. package/templates/base/styles/components.css +0 -38
  18. package/templates/base/tsconfig.json +0 -56
  19. package/templates/base/webpack.config.ts +0 -247
  20. package/templates/common/.env.template +0 -6
  21. package/templates/common/.gitignore.template +0 -8
  22. package/templates/common/.nvmrc +0 -1
  23. package/templates/common/.prettierrc +0 -21
  24. package/templates/common/LICENSE.md +0 -48
  25. package/templates/common/README.md +0 -179
  26. package/templates/common/jest.config.mjs +0 -35
  27. package/templates/common/jest.setup.ts +0 -35
  28. package/templates/content_publisher/README.md +0 -58
  29. package/templates/content_publisher/canva-app.json +0 -17
  30. package/templates/content_publisher/declarations/declarations.d.ts +0 -29
  31. package/templates/content_publisher/eslint.config.mjs +0 -14
  32. package/templates/content_publisher/package.json +0 -90
  33. package/templates/content_publisher/scripts/copy_env.ts +0 -13
  34. package/templates/content_publisher/scripts/ssl/ssl.ts +0 -131
  35. package/templates/content_publisher/scripts/start/app_runner.ts +0 -223
  36. package/templates/content_publisher/scripts/start/context.ts +0 -171
  37. package/templates/content_publisher/scripts/start/start.ts +0 -46
  38. package/templates/content_publisher/src/index.tsx +0 -4
  39. package/templates/content_publisher/src/intents/content_publisher/index.tsx +0 -107
  40. package/templates/content_publisher/src/intents/content_publisher/post_preview.tsx +0 -240
  41. package/templates/content_publisher/src/intents/content_publisher/preview_ui.tsx +0 -62
  42. package/templates/content_publisher/src/intents/content_publisher/settings_ui.tsx +0 -81
  43. package/templates/content_publisher/src/intents/content_publisher/types.ts +0 -29
  44. package/templates/content_publisher/styles/components.css +0 -38
  45. package/templates/content_publisher/styles/preview_ui.css +0 -49
  46. package/templates/content_publisher/tsconfig.json +0 -56
  47. package/templates/content_publisher/webpack.config.ts +0 -247
  48. package/templates/dam/backend/routers/dam.ts +0 -108
  49. package/templates/dam/backend/server.ts +0 -65
  50. package/templates/dam/canva-app.json +0 -25
  51. package/templates/dam/declarations/declarations.d.ts +0 -29
  52. package/templates/dam/eslint.config.mjs +0 -14
  53. package/templates/dam/package.json +0 -97
  54. package/templates/dam/scripts/copy_env.ts +0 -13
  55. package/templates/dam/scripts/ssl/ssl.ts +0 -131
  56. package/templates/dam/scripts/start/app_runner.ts +0 -223
  57. package/templates/dam/scripts/start/context.ts +0 -171
  58. package/templates/dam/scripts/start/start.ts +0 -46
  59. package/templates/dam/src/index.tsx +0 -4
  60. package/templates/dam/src/intents/design_editor/adapter.ts +0 -44
  61. package/templates/dam/src/intents/design_editor/app.tsx +0 -35
  62. package/templates/dam/src/intents/design_editor/config.ts +0 -220
  63. package/templates/dam/src/intents/design_editor/index.css +0 -3
  64. package/templates/dam/src/intents/design_editor/index.tsx +0 -25
  65. package/templates/dam/tsconfig.json +0 -56
  66. package/templates/dam/utils/backend/base_backend/create.ts +0 -114
  67. package/templates/dam/webpack.config.ts +0 -247
  68. package/templates/data_connector/README.md +0 -84
  69. package/templates/data_connector/canva-app.json +0 -21
  70. package/templates/data_connector/declarations/declarations.d.ts +0 -29
  71. package/templates/data_connector/eslint.config.mjs +0 -14
  72. package/templates/data_connector/package.json +0 -92
  73. package/templates/data_connector/scripts/copy_env.ts +0 -13
  74. package/templates/data_connector/scripts/ssl/ssl.ts +0 -131
  75. package/templates/data_connector/scripts/start/app_runner.ts +0 -223
  76. package/templates/data_connector/scripts/start/context.ts +0 -171
  77. package/templates/data_connector/scripts/start/start.ts +0 -46
  78. package/templates/data_connector/src/api/connect_client.ts +0 -6
  79. package/templates/data_connector/src/api/data_source.ts +0 -97
  80. package/templates/data_connector/src/api/data_sources/designs.tsx +0 -296
  81. package/templates/data_connector/src/api/data_sources/index.ts +0 -4
  82. package/templates/data_connector/src/api/data_sources/templates.tsx +0 -328
  83. package/templates/data_connector/src/api/fetch_data_table.ts +0 -55
  84. package/templates/data_connector/src/api/index.ts +0 -4
  85. package/templates/data_connector/src/api/oauth.ts +0 -8
  86. package/templates/data_connector/src/api/tests/data_source.test.tsx +0 -99
  87. package/templates/data_connector/src/components/app_error.tsx +0 -15
  88. package/templates/data_connector/src/components/footer.tsx +0 -26
  89. package/templates/data_connector/src/components/header.tsx +0 -40
  90. package/templates/data_connector/src/components/index.ts +0 -3
  91. package/templates/data_connector/src/components/inputs/messages.tsx +0 -95
  92. package/templates/data_connector/src/components/inputs/search_filter.tsx +0 -109
  93. package/templates/data_connector/src/components/inputs/select_field.tsx +0 -26
  94. package/templates/data_connector/src/context/app_context.tsx +0 -125
  95. package/templates/data_connector/src/context/index.ts +0 -2
  96. package/templates/data_connector/src/context/use_app_context.ts +0 -17
  97. package/templates/data_connector/src/index.tsx +0 -4
  98. package/templates/data_connector/src/intents/data_connector/app.tsx +0 -20
  99. package/templates/data_connector/src/intents/data_connector/entrypoint.tsx +0 -70
  100. package/templates/data_connector/src/intents/data_connector/home.tsx +0 -21
  101. package/templates/data_connector/src/intents/data_connector/index.tsx +0 -56
  102. package/templates/data_connector/src/pages/data_source_config.tsx +0 -9
  103. package/templates/data_connector/src/pages/error.tsx +0 -37
  104. package/templates/data_connector/src/pages/index.ts +0 -4
  105. package/templates/data_connector/src/pages/login.tsx +0 -145
  106. package/templates/data_connector/src/pages/select_source.tsx +0 -24
  107. package/templates/data_connector/src/routes/index.ts +0 -2
  108. package/templates/data_connector/src/routes/paths.ts +0 -7
  109. package/templates/data_connector/src/routes/protected_route.tsx +0 -26
  110. package/templates/data_connector/src/routes/routes.tsx +0 -42
  111. package/templates/data_connector/src/utils/data_params.ts +0 -17
  112. package/templates/data_connector/src/utils/data_table.ts +0 -116
  113. package/templates/data_connector/src/utils/fetch_result.ts +0 -36
  114. package/templates/data_connector/src/utils/index.ts +0 -2
  115. package/templates/data_connector/src/utils/tests/data_table.test.ts +0 -133
  116. package/templates/data_connector/styles/components.css +0 -38
  117. package/templates/data_connector/tsconfig.json +0 -56
  118. package/templates/data_connector/webpack.config.ts +0 -247
  119. package/templates/gen_ai/README.md +0 -27
  120. package/templates/gen_ai/backend/routers/image.ts +0 -232
  121. package/templates/gen_ai/backend/server.ts +0 -65
  122. package/templates/gen_ai/canva-app.json +0 -25
  123. package/templates/gen_ai/declarations/declarations.d.ts +0 -29
  124. package/templates/gen_ai/eslint.config.mjs +0 -14
  125. package/templates/gen_ai/package.json +0 -101
  126. package/templates/gen_ai/scripts/copy_env.ts +0 -13
  127. package/templates/gen_ai/scripts/ssl/ssl.ts +0 -131
  128. package/templates/gen_ai/scripts/start/app_runner.ts +0 -223
  129. package/templates/gen_ai/scripts/start/context.ts +0 -171
  130. package/templates/gen_ai/scripts/start/start.ts +0 -46
  131. package/templates/gen_ai/src/api/api.ts +0 -194
  132. package/templates/gen_ai/src/api/index.ts +0 -1
  133. package/templates/gen_ai/src/components/app_error.tsx +0 -18
  134. package/templates/gen_ai/src/components/footer.messages.ts +0 -48
  135. package/templates/gen_ai/src/components/footer.tsx +0 -156
  136. package/templates/gen_ai/src/components/image_grid.tsx +0 -103
  137. package/templates/gen_ai/src/components/index.ts +0 -7
  138. package/templates/gen_ai/src/components/loading_results.tsx +0 -169
  139. package/templates/gen_ai/src/components/prompt_input.messages.ts +0 -14
  140. package/templates/gen_ai/src/components/prompt_input.tsx +0 -154
  141. package/templates/gen_ai/src/components/remaining_credits.tsx +0 -84
  142. package/templates/gen_ai/src/components/report_box.tsx +0 -54
  143. package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +0 -47
  144. package/templates/gen_ai/src/config.ts +0 -21
  145. package/templates/gen_ai/src/context/app_context.tsx +0 -153
  146. package/templates/gen_ai/src/context/context.messages.ts +0 -30
  147. package/templates/gen_ai/src/context/index.ts +0 -2
  148. package/templates/gen_ai/src/context/use_app_context.ts +0 -17
  149. package/templates/gen_ai/src/index.tsx +0 -4
  150. package/templates/gen_ai/src/intents/design_editor/app.tsx +0 -19
  151. package/templates/gen_ai/src/intents/design_editor/home.tsx +0 -13
  152. package/templates/gen_ai/src/intents/design_editor/index.tsx +0 -17
  153. package/templates/gen_ai/src/pages/error.tsx +0 -41
  154. package/templates/gen_ai/src/pages/generate.tsx +0 -9
  155. package/templates/gen_ai/src/pages/index.ts +0 -3
  156. package/templates/gen_ai/src/pages/results.tsx +0 -31
  157. package/templates/gen_ai/src/routes/index.ts +0 -1
  158. package/templates/gen_ai/src/routes/paths.ts +0 -4
  159. package/templates/gen_ai/src/routes/routes.tsx +0 -24
  160. package/templates/gen_ai/src/utils/index.ts +0 -1
  161. package/templates/gen_ai/src/utils/obscenity_filter.ts +0 -33
  162. package/templates/gen_ai/styles/components.css +0 -38
  163. package/templates/gen_ai/styles/utils.css +0 -3
  164. package/templates/gen_ai/tsconfig.json +0 -56
  165. package/templates/gen_ai/utils/backend/base_backend/create.ts +0 -114
  166. package/templates/gen_ai/webpack.config.ts +0 -247
  167. package/templates/hello_world/canva-app.json +0 -21
  168. package/templates/hello_world/declarations/declarations.d.ts +0 -29
  169. package/templates/hello_world/eslint.config.mjs +0 -14
  170. package/templates/hello_world/package.json +0 -90
  171. package/templates/hello_world/scripts/copy_env.ts +0 -13
  172. package/templates/hello_world/scripts/ssl/ssl.ts +0 -131
  173. package/templates/hello_world/scripts/start/app_runner.ts +0 -223
  174. package/templates/hello_world/scripts/start/context.ts +0 -171
  175. package/templates/hello_world/scripts/start/start.ts +0 -46
  176. package/templates/hello_world/src/index.tsx +0 -4
  177. package/templates/hello_world/src/intents/design_editor/app.tsx +0 -86
  178. package/templates/hello_world/src/intents/design_editor/index.tsx +0 -25
  179. package/templates/hello_world/src/intents/design_editor/tests/__snapshots__/app.tests.tsx.snap +0 -45
  180. package/templates/hello_world/src/intents/design_editor/tests/app.tests.tsx +0 -92
  181. package/templates/hello_world/styles/components.css +0 -38
  182. package/templates/hello_world/tsconfig.json +0 -56
  183. package/templates/hello_world/webpack.config.ts +0 -247
  184. package/templates/mls/README.md +0 -81
  185. package/templates/mls/canva-app.json +0 -25
  186. package/templates/mls/declarations/declarations.d.ts +0 -29
  187. package/templates/mls/eslint.config.mjs +0 -14
  188. package/templates/mls/jest.config.mjs +0 -36
  189. package/templates/mls/jest.setup.ts +0 -39
  190. package/templates/mls/package.json +0 -117
  191. package/templates/mls/scripts/copy_env.ts +0 -13
  192. package/templates/mls/scripts/ssl/ssl.ts +0 -131
  193. package/templates/mls/scripts/start/app_runner.ts +0 -223
  194. package/templates/mls/scripts/start/context.ts +0 -171
  195. package/templates/mls/scripts/start/start.ts +0 -46
  196. package/templates/mls/src/__tests__/app.tests.tsx +0 -11
  197. package/templates/mls/src/__tests__/office_selection_page.tests.tsx +0 -72
  198. package/templates/mls/src/__tests__/utils.tsx +0 -19
  199. package/templates/mls/src/adapter.ts +0 -126
  200. package/templates/mls/src/components/agent/agent_card.tsx +0 -57
  201. package/templates/mls/src/components/agent/agent_grid.tsx +0 -37
  202. package/templates/mls/src/components/agent/agent_list.tsx +0 -17
  203. package/templates/mls/src/components/agent/agent_search_filters.tsx +0 -88
  204. package/templates/mls/src/components/breadcrumb/breadcrumb.tsx +0 -40
  205. package/templates/mls/src/components/listing/listing_card.tsx +0 -64
  206. package/templates/mls/src/components/listing/listing_grid.tsx +0 -37
  207. package/templates/mls/src/components/listing/listing_list.tsx +0 -21
  208. package/templates/mls/src/components/listing/listing_search_filters.tsx +0 -145
  209. package/templates/mls/src/components/placeholders/placeholders.tsx +0 -65
  210. package/templates/mls/src/data.ts +0 -359
  211. package/templates/mls/src/index.tsx +0 -4
  212. package/templates/mls/src/intents/design_editor/app.tsx +0 -44
  213. package/templates/mls/src/intents/design_editor/index.tsx +0 -25
  214. package/templates/mls/src/pages/agent_details_page/agent_details_page.tsx +0 -175
  215. package/templates/mls/src/pages/list_page/agent_tab_panel.tsx +0 -126
  216. package/templates/mls/src/pages/list_page/list_page.tsx +0 -67
  217. package/templates/mls/src/pages/list_page/listing_tab_panel.tsx +0 -135
  218. package/templates/mls/src/pages/listing_details_page/listing_details_page.tsx +0 -418
  219. package/templates/mls/src/pages/loading_page/loading_page.tsx +0 -152
  220. package/templates/mls/src/pages/office_selection_page/office_selection_page.tsx +0 -144
  221. package/templates/mls/src/real_estate.type.ts +0 -44
  222. package/templates/mls/src/util/use_add_element.tsx +0 -62
  223. package/templates/mls/src/util/use_drag_element.tsx +0 -68
  224. package/templates/mls/styles/components.css +0 -38
  225. package/templates/mls/tsconfig.json +0 -54
  226. package/templates/mls/webpack.config.ts +0 -248
  227. package/templates/optional/.cursor/mcp.json +0 -8
  228. package/templates/optional/.vscode/extensions.json +0 -6
  229. package/templates/optional/.vscode/mcp.json +0 -9
  230. package/templates/optional/AGENTS.md +0 -154
  231. package/templates/optional/CLAUDE.md +0 -154
@@ -1,223 +0,0 @@
1
- /* eslint-disable no-console */
2
- import { generatePreviewUrl } from "@canva/cli";
3
- import ngrok from "@ngrok/ngrok";
4
- import chalk from "chalk";
5
- import Table from "cli-table3";
6
- import nodemon from "nodemon";
7
- import open from "open";
8
- import os from "os";
9
- import webpack from "webpack";
10
- import WebpackDevServer from "webpack-dev-server";
11
- import { buildConfig } from "../../webpack.config";
12
- import type { Certificate } from "../ssl/ssl";
13
- import { createOrRetrieveCertificate } from "../ssl/ssl";
14
- import type { Context } from "./context";
15
-
16
- export const infoChalk = chalk.blue.bold;
17
- export const warnChalk = chalk.bgYellow.bold;
18
- export const errorChalk = chalk.bgRed.bold;
19
- export const highlightChalk = chalk.greenBright.bold;
20
- export const linkChalk = chalk.cyan;
21
-
22
- /**
23
- * Returns the appropriate modifier key text based on the user's operating system.
24
- * @returns "cmd" for macOS, "ctrl" for Windows and Linux
25
- */
26
- export function getModifierKey(): string {
27
- const platform = os.platform();
28
- switch (platform) {
29
- case "darwin": // macOS
30
- return "cmd";
31
- case "win32": // Windows
32
- return "ctrl";
33
- default: // Linux and others
34
- return "ctrl";
35
- }
36
- }
37
-
38
- export class AppRunner {
39
- async run(ctx: Context) {
40
- console.log(
41
- infoChalk("Info:"),
42
- `Starting development server for ${highlightChalk(ctx.entryDir)}\n`,
43
- );
44
-
45
- if (!ctx.hmrEnabled) {
46
- console.log(
47
- `${infoChalk(
48
- "Note:",
49
- )} Hot Module Replacement (HMR) not enabled. To enable it, please refer to the instructions in the ${highlightChalk(
50
- "README.md",
51
- )}\n`,
52
- );
53
- }
54
-
55
- let cert: Certificate | undefined;
56
- if (ctx.httpsEnabled) {
57
- try {
58
- cert = await createOrRetrieveCertificate();
59
- } catch (err) {
60
- console.log(
61
- errorChalk("Error:"),
62
- "Unable to generate SSL certificate.",
63
- );
64
- throw err;
65
- }
66
- }
67
-
68
- const table = new Table({
69
- colWidths: [30, 80],
70
- wordWrap: true,
71
- wrapOnWordBoundary: true,
72
- });
73
-
74
- const server = await this.runWebpackDevServer(ctx, table, cert);
75
-
76
- await this.maybeRunBackendServer(ctx, table, cert, server);
77
-
78
- await this.generateAndOpenPreviewUrl(ctx.openPreview, table);
79
-
80
- console.log(table.toString(), "\n");
81
-
82
- console.log(
83
- `${infoChalk(
84
- "Note:",
85
- )} For instructions on how to set up the app via the Developer Portal, see the ${highlightChalk(
86
- "README.md",
87
- )}.\n`,
88
- );
89
- }
90
-
91
- private readonly maybeRunBackendServer = async (
92
- ctx: Context,
93
- table: Table.Table,
94
- cert: Certificate | undefined,
95
- webpackDevServer: WebpackDevServer,
96
- ) => {
97
- if (!ctx.developerBackendEntryPath) {
98
- return;
99
- }
100
-
101
- // App ID must be set when running a backend example
102
- if (!ctx.appId) {
103
- console.log(
104
- errorChalk("Error:"),
105
- `'CANVA_APP_ID' environment variable is undefined. Refer to the instructions in the ${highlightChalk(
106
- "README.md",
107
- )} on starting a backend example.`,
108
- );
109
- throw new Error("'CANVA_APP_ID' env variable not set.");
110
- }
111
-
112
- await new Promise((resolve) => {
113
- const nodemonServer = nodemon({
114
- script: ctx.developerBackendEntryPath,
115
- ext: "ts",
116
- env: {
117
- SHOULD_ENABLE_HTTPS: ctx.httpsEnabled,
118
- HTTPS_CERT_FILE: cert?.certFile || "",
119
- HTTPS_KEY_FILE: cert?.keyFile || "",
120
- },
121
- });
122
-
123
- nodemonServer.on("start", resolve);
124
-
125
- nodemonServer.on("crash", async () => {
126
- console.log(errorChalk("Shutting down local server.\n"));
127
-
128
- await webpackDevServer.stop();
129
- process.exit(1);
130
- });
131
- });
132
-
133
- if (ctx.ngrokEnabled) {
134
- console.log(
135
- warnChalk("Warning:"),
136
- `ngrok exposes a local port via a public URL. Be mindful of what's exposed and shut down the server when it's not in use.\n`,
137
- );
138
- }
139
-
140
- let url = ctx.backendUrl;
141
- if (ctx.ngrokEnabled) {
142
- try {
143
- const ngrokListener = await ngrok.forward({
144
- addr: ctx.backendPort,
145
- // requires an `NGROK_AUTHTOKEN` env var to be set
146
- authtoken_from_env: true,
147
- });
148
- url = ngrokListener.url() ?? url;
149
- } catch (err) {
150
- console.log(
151
- errorChalk("Error:"),
152
- `Unable to start ngrok server: ${err}`,
153
- );
154
- }
155
- }
156
-
157
- table.push(["Base URL (Backend)", linkChalk(url)]);
158
- };
159
-
160
- private readonly runWebpackDevServer = async (
161
- ctx: Context,
162
- table: Table.Table,
163
- cert: Certificate | undefined,
164
- ): Promise<WebpackDevServer> => {
165
- const runtimeWebpackConfig = buildConfig({
166
- appEntry: ctx.frontendEntryPath,
167
- backendHost: ctx.backendHost,
168
- devConfig: {
169
- port: ctx.frontendPort,
170
- enableHmr: ctx.hmrEnabled,
171
- appId: ctx.appId,
172
- appOrigin: ctx.appOrigin,
173
- enableHttps: ctx.httpsEnabled,
174
- ...cert,
175
- },
176
- });
177
-
178
- const compiler = webpack(runtimeWebpackConfig);
179
- const server = new WebpackDevServer(
180
- runtimeWebpackConfig.devServer,
181
- compiler,
182
- );
183
- await server.start();
184
-
185
- table.push(["Development URL (Frontend)", linkChalk(ctx.frontendUrl)]);
186
-
187
- return server;
188
- };
189
-
190
- /**
191
- * Calls the Canva CLI to generate a preview URL for the app
192
- */
193
- private readonly generateAndOpenPreviewUrl = async (
194
- openPreview: boolean,
195
- table: Table.Table,
196
- ) => {
197
- const previewCellHeader = { content: "Preview your app in Canva" };
198
-
199
- const generatePreviewResult = await generatePreviewUrl();
200
-
201
- if (!generatePreviewResult.success) {
202
- table.push([
203
- previewCellHeader,
204
- { content: warnChalk(generatePreviewResult.message) },
205
- ]);
206
- return;
207
- }
208
-
209
- const modifierKey = getModifierKey();
210
-
211
- table.push([
212
- previewCellHeader,
213
- {
214
- content: `Preview URL (${modifierKey} + click)`,
215
- href: generatePreviewResult.data,
216
- },
217
- ]);
218
-
219
- if (openPreview) {
220
- open(generatePreviewResult.data);
221
- }
222
- };
223
- }
@@ -1,171 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
-
4
- interface CliArgs {
5
- example?: string;
6
- useHttps: boolean;
7
- ngrok: boolean;
8
- preview: boolean;
9
- overrideFrontendPort?: number;
10
- }
11
-
12
- interface EnvVars {
13
- frontendPort: number;
14
- backendPort: number;
15
- hmrEnabled: boolean;
16
- appId?: string;
17
- appOrigin?: string;
18
- backendHost?: string;
19
- }
20
-
21
- export class Context {
22
- private readonly envVars: EnvVars;
23
-
24
- constructor(
25
- private env: NodeJS.ProcessEnv = process.env,
26
- private readonly args: CliArgs,
27
- ) {
28
- this.envVars = this.parseAndValidateEnvironmentVariables();
29
- }
30
-
31
- static get srcDir() {
32
- const src = path.join(Context.rootDir, "src");
33
-
34
- if (!fs.existsSync(src)) {
35
- throw new Error(`Directory does not exist: ${src}`);
36
- }
37
-
38
- return src;
39
- }
40
-
41
- static get readmeDir() {
42
- return path.join(Context.rootDir, "README.md");
43
- }
44
-
45
- get entryDir() {
46
- return Context.srcDir;
47
- }
48
-
49
- get ngrokEnabled() {
50
- return this.args.ngrok;
51
- }
52
-
53
- get hmrEnabled() {
54
- return this.envVars.hmrEnabled;
55
- }
56
-
57
- get httpsEnabled() {
58
- return this.args.useHttps;
59
- }
60
-
61
- get frontendEntryPath() {
62
- const frontendEntryPath = path.join(this.entryDir, "index.tsx");
63
-
64
- if (!fs.existsSync(frontendEntryPath)) {
65
- throw new Error(
66
- `Entry point for frontend does not exist: ${frontendEntryPath}`,
67
- );
68
- }
69
-
70
- return frontendEntryPath;
71
- }
72
-
73
- get frontendUrl() {
74
- return `${this.protocol}://localhost:${this.frontendPort}`;
75
- }
76
-
77
- get frontendPort() {
78
- return this.args.overrideFrontendPort || this.envVars.frontendPort;
79
- }
80
-
81
- get developerBackendEntryPath(): string | undefined {
82
- const developerBackendEntryPath = path.join(
83
- Context.rootDir,
84
- "backend",
85
- "server.ts",
86
- );
87
-
88
- if (!fs.existsSync(developerBackendEntryPath)) {
89
- return undefined;
90
- }
91
-
92
- return developerBackendEntryPath;
93
- }
94
-
95
- get backendUrl() {
96
- return `${this.protocol}://localhost:${this.envVars.backendPort}`;
97
- }
98
-
99
- get backendHost() {
100
- let backendHost = this.envVars.backendHost;
101
-
102
- // if there's no custom URL provided by the developer, we fallback to our localhost backend
103
- if (!backendHost || backendHost.trim() === "") {
104
- backendHost = this.backendUrl;
105
- }
106
-
107
- return backendHost;
108
- }
109
-
110
- get backendPort() {
111
- return this.envVars.backendPort;
112
- }
113
-
114
- get appOrigin(): string | undefined {
115
- return this.envVars.appOrigin;
116
- }
117
-
118
- get appId(): string | undefined {
119
- return this.envVars.appId;
120
- }
121
-
122
- get openPreview(): boolean {
123
- return this.args.preview;
124
- }
125
-
126
- private get protocol(): "https" | "http" {
127
- return this.httpsEnabled ? "https" : "http";
128
- }
129
-
130
- private static get rootDir() {
131
- return path.join(process.cwd());
132
- }
133
-
134
- private parseAndValidateEnvironmentVariables(): EnvVars {
135
- const {
136
- CANVA_FRONTEND_PORT,
137
- CANVA_BACKEND_PORT,
138
- CANVA_BACKEND_HOST,
139
- CANVA_APP_ID,
140
- CANVA_APP_ORIGIN,
141
- CANVA_HMR_ENABLED,
142
- } = this.env;
143
-
144
- if (!CANVA_FRONTEND_PORT) {
145
- throw new Error(
146
- "CANVA_FRONTEND_PORT environment variable is not defined",
147
- );
148
- }
149
-
150
- if (!CANVA_BACKEND_PORT) {
151
- throw new Error("CANVA_BACKEND_PORT environment variable is not defined");
152
- }
153
-
154
- const envVars: EnvVars = {
155
- frontendPort: parseInt(CANVA_FRONTEND_PORT, 10),
156
- backendPort: parseInt(CANVA_BACKEND_PORT, 10),
157
- hmrEnabled: CANVA_HMR_ENABLED?.toLowerCase().trim() === "true",
158
- appId: CANVA_APP_ID,
159
- appOrigin: CANVA_APP_ORIGIN,
160
- backendHost: CANVA_BACKEND_HOST,
161
- };
162
-
163
- if (envVars.hmrEnabled && envVars.appOrigin == null) {
164
- throw new Error(
165
- "CANVA_HMR_ENABLED environment variable is TRUE, but CANVA_APP_ORIGIN is not set. Refer to the instructions in the README.md on configuring HMR.",
166
- );
167
- }
168
-
169
- return envVars;
170
- }
171
- }
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env node
2
- import yargs from "yargs";
3
- import { hideBin } from "yargs/helpers";
4
- import { AppRunner } from "./app_runner";
5
- import { Context } from "./context";
6
-
7
- const appRunner = new AppRunner();
8
-
9
- yargs(hideBin(process.argv))
10
- .version(false)
11
- .help()
12
- .option("ngrok", {
13
- description: "Run backend server via ngrok.",
14
- type: "boolean",
15
- // npm swallows command line args instead of forwarding to the script
16
- default:
17
- process.env.npm_config_ngrok?.toLocaleLowerCase().trim() === "true",
18
- })
19
- .option("use-https", {
20
- description: "Start local development server on HTTPS.",
21
- type: "boolean",
22
- // npm swallows commands line args instead of forwarding to the script
23
- default:
24
- process.env.npm_config_use_https?.toLocaleLowerCase().trim() === "true",
25
- })
26
- .option("override-frontend-port", {
27
- description:
28
- "Port to run the local development server on. Overrides the frontend port set in the .env file.",
29
- type: "number",
30
- alias: "p",
31
- })
32
- .option("preview", {
33
- description: "Open the app in Canva.",
34
- type: "boolean",
35
- default: false,
36
- })
37
- .command(
38
- "$0",
39
- "Starts local development",
40
- () => {},
41
- (args) => {
42
- const ctx = new Context(process.env, args);
43
- appRunner.run(ctx);
44
- },
45
- )
46
- .parse();
@@ -1,4 +0,0 @@
1
- import { prepareDesignEditor } from "@canva/intents/design";
2
- import designEditor from "./intents/design_editor";
3
-
4
- prepareDesignEditor(designEditor);
@@ -1,86 +0,0 @@
1
- import { useFeatureSupport } from "@canva/app-hooks";
2
- import { Button, Rows, Text } from "@canva/app-ui-kit";
3
- import { addElementAtCursor, addElementAtPoint } from "@canva/design";
4
- import { requestOpenExternalUrl } from "@canva/platform";
5
- import { FormattedMessage, useIntl } from "react-intl";
6
- import * as styles from "styles/components.css";
7
-
8
- export const DOCS_URL = "https://www.canva.dev/docs/apps/";
9
-
10
- export const App = () => {
11
- const isSupported = useFeatureSupport();
12
- const addElement = [addElementAtPoint, addElementAtCursor].find((fn) =>
13
- isSupported(fn),
14
- );
15
-
16
- const onClick = () => {
17
- if (!addElement) {
18
- return;
19
- }
20
-
21
- addElement({
22
- type: "text",
23
- children: ["Hello world!"],
24
- });
25
- };
26
-
27
- const openExternalUrl = async (url: string) => {
28
- const response = await requestOpenExternalUrl({
29
- url,
30
- });
31
-
32
- if (response.status === "aborted") {
33
- // user decided not to navigate to the link
34
- }
35
- };
36
-
37
- const intl = useIntl();
38
-
39
- return (
40
- <div className={styles.scrollContainer}>
41
- <Rows spacing="2u">
42
- <Text>
43
- <FormattedMessage
44
- defaultMessage="
45
- To make changes to this app, edit the <code>src/app.tsx</code> file,
46
- then close and reopen the app in the editor to preview the changes.
47
- "
48
- description="Instructions for how to make changes to the app. Do not translate <code>src/app.tsx</code>."
49
- values={{
50
- code: (chunks) => <code>{chunks}</code>,
51
- }}
52
- />
53
- </Text>
54
- <Button
55
- variant="primary"
56
- onClick={onClick}
57
- disabled={!addElement}
58
- tooltipLabel={
59
- !addElement
60
- ? intl.formatMessage({
61
- defaultMessage:
62
- "This feature is not supported in the current page",
63
- description:
64
- "Tooltip label for when a feature is not supported in the current design",
65
- })
66
- : undefined
67
- }
68
- stretch
69
- >
70
- {intl.formatMessage({
71
- defaultMessage: "Do something cool",
72
- description:
73
- "Button text to do something cool. Creates a new text element when pressed.",
74
- })}
75
- </Button>
76
- <Button variant="secondary" onClick={() => openExternalUrl(DOCS_URL)}>
77
- {intl.formatMessage({
78
- defaultMessage: "Open Canva Apps SDK docs",
79
- description:
80
- "Button text to open Canva Apps SDK docs. Opens an external URL when pressed.",
81
- })}
82
- </Button>
83
- </Rows>
84
- </div>
85
- );
86
- };
@@ -1,25 +0,0 @@
1
- import "@canva/app-ui-kit/styles.css";
2
- import { AppI18nProvider } from "@canva/app-i18n-kit";
3
- import { AppUiProvider } from "@canva/app-ui-kit";
4
- import type { DesignEditorIntent } from "@canva/intents/design";
5
- import { createRoot } from "react-dom/client";
6
- import { App } from "./app";
7
-
8
- async function render() {
9
- const root = createRoot(document.getElementById("root") as Element);
10
-
11
- root.render(
12
- <AppI18nProvider>
13
- <AppUiProvider>
14
- <App />
15
- </AppUiProvider>
16
- </AppI18nProvider>,
17
- );
18
- }
19
-
20
- const designEditor: DesignEditorIntent = { render };
21
- export default designEditor;
22
-
23
- if (module.hot) {
24
- module.hot.accept("./app", render);
25
- }
@@ -1,45 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`Hello World Tests should have a consistent snapshot 1`] = `
4
- <div>
5
- <div
6
- class="scrollContainer"
7
- >
8
- <div
9
- class="jv_R6g"
10
- style="--NZu1Zw: 16px;"
11
- >
12
- <p
13
- class="aWBg0w aZskFA u16U_g"
14
- >
15
- To make changes to this app, edit the
16
- <code>
17
- src/app.tsx
18
- </code>
19
- file, then close and reopen the app in the editor to preview the changes.
20
- </p>
21
- <button
22
- class="_5KtATA LQzFZw VgvqkQ _8ERLTg rV61VQ LQzFZw VgvqkQ _4_iekA _3oHQrw j34Zww aqbYPg _3FvZZg"
23
- type="button"
24
- >
25
- <span
26
- class="khPe7Q"
27
- >
28
- Do something cool
29
- </span>
30
- </button>
31
- <button
32
- class="_5KtATA LQzFZw VgvqkQ _8ERLTg Z3nT2A LQzFZw VgvqkQ _4_iekA j34Zww aqbYPg _3FvZZg"
33
- type="button"
34
- >
35
- <span
36
- class="khPe7Q"
37
- >
38
- Open Canva Apps SDK docs
39
- </span>
40
- </button>
41
- </div>
42
- </div>
43
- ,
44
- </div>
45
- `;
@@ -1,92 +0,0 @@
1
- /* eslint-disable formatjs/no-literal-string-in-jsx */
2
- import { useFeatureSupport } from "@canva/app-hooks";
3
- import { TestAppI18nProvider } from "@canva/app-i18n-kit";
4
- import { TestAppUiProvider } from "@canva/app-ui-kit";
5
- import { addElementAtCursor, addElementAtPoint } from "@canva/design";
6
- import type { Feature } from "@canva/platform";
7
- import { requestOpenExternalUrl } from "@canva/platform";
8
- import { fireEvent, render } from "@testing-library/react";
9
- import type { RenderResult } from "@testing-library/react";
10
- import type { ReactNode } from "react";
11
- import { App, DOCS_URL } from "../app";
12
-
13
- function renderInTestProvider(node: ReactNode): RenderResult {
14
- return render(
15
- // In a test environment, you should wrap your apps in `TestAppI18nProvider` and `TestAppUiProvider`, rather than `AppI18nProvider` and `AppUiProvider`
16
- <TestAppI18nProvider>
17
- <TestAppUiProvider>{node}</TestAppUiProvider>,
18
- </TestAppI18nProvider>,
19
- );
20
- }
21
-
22
- jest.mock("@canva/app-hooks");
23
-
24
- // This test demonstrates how to test code that uses functions from the Canva Apps SDK
25
- // For more information on testing with the Canva Apps SDK, see https://www.canva.dev/docs/apps/testing/
26
- describe("Hello World Tests", () => {
27
- const mockIsSupported = jest.fn();
28
- const mockUseFeatureSupport = jest.mocked(useFeatureSupport);
29
- const mockRequestOpenExternalUrl = jest.mocked(requestOpenExternalUrl);
30
-
31
- beforeEach(() => {
32
- jest.resetAllMocks();
33
- mockIsSupported.mockImplementation(
34
- (fn: Feature) => fn === addElementAtPoint,
35
- );
36
- mockUseFeatureSupport.mockReturnValue(mockIsSupported);
37
- mockRequestOpenExternalUrl.mockResolvedValue({ status: "completed" });
38
- });
39
-
40
- // this test uses a mock in place of the useFeatureSupport hook
41
- it("should add a text element when the button is clicked", () => {
42
- // assert that the mocks are in the expected clean state
43
- expect(mockUseFeatureSupport).not.toHaveBeenCalled();
44
- expect(addElementAtPoint).not.toHaveBeenCalled();
45
-
46
- const result = renderInTestProvider(<App />);
47
-
48
- // the hook should have been called in the render process but not the callback
49
- expect(mockUseFeatureSupport).toHaveBeenCalled();
50
- expect(addElementAtPoint).not.toHaveBeenCalled();
51
-
52
- // get a reference to the do something cool button element
53
- const doSomethingCoolBtn = result.getByRole("button", {
54
- name: "Do something cool",
55
- });
56
-
57
- // programmatically simulate clicking the button
58
- fireEvent.click(doSomethingCoolBtn);
59
-
60
- // we expect that addElementAtPoint has been called by the button's click handler
61
- expect(mockIsSupported).toHaveBeenCalledWith(addElementAtPoint);
62
- expect(mockIsSupported).not.toHaveBeenCalledWith(addElementAtCursor);
63
- expect(addElementAtPoint).toHaveBeenCalled();
64
- });
65
-
66
- // this test uses a mock in place of the @canva/platform requestOpenExternalUrl function
67
- it("should call `requestOpenExternalUrl` when the button is clicked", () => {
68
- expect(mockRequestOpenExternalUrl).not.toHaveBeenCalled();
69
-
70
- const result = renderInTestProvider(<App />);
71
-
72
- // get a reference to the Apps SDK button by name
73
- const sdkButton = result.getByRole("button", {
74
- name: "Open Canva Apps SDK docs",
75
- });
76
-
77
- expect(mockRequestOpenExternalUrl).not.toHaveBeenCalled();
78
- fireEvent.click(sdkButton);
79
- expect(mockRequestOpenExternalUrl).toHaveBeenCalled();
80
-
81
- // assert that the requestOpenExternalUrl function was called with the expected arguments
82
- expect(mockRequestOpenExternalUrl.mock.calls[0]?.[0]).toEqual({
83
- url: DOCS_URL,
84
- });
85
- });
86
-
87
- // this test demonstrates the use of a snapshot test
88
- it("should have a consistent snapshot", () => {
89
- const result = renderInTestProvider(<App />);
90
- expect(result.container).toMatchSnapshot();
91
- });
92
- });