wrangler 0.0.0-e1c615f → 0.0.0-e22f9d7

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/src/publish.ts CHANGED
@@ -11,7 +11,7 @@ import type { Config } from "./config";
11
11
  import makeModuleCollector from "./module-collection";
12
12
  import { syncAssets } from "./sites";
13
13
 
14
- type CfScriptFormat = void | "modules" | "service-worker";
14
+ type CfScriptFormat = undefined | "modules" | "service-worker";
15
15
 
16
16
  type Props = {
17
17
  config: Config;
@@ -19,13 +19,15 @@ type Props = {
19
19
  script?: string;
20
20
  name?: string;
21
21
  env?: string;
22
+ compatibilityDate?: string;
23
+ compatibilityFlags?: string[];
22
24
  public?: string;
23
25
  site?: string;
24
26
  triggers?: (string | number)[];
25
27
  routes?: (string | number)[];
26
28
  legacyEnv?: boolean;
27
- jsxFactory: void | string;
28
- jsxFragment: void | string;
29
+ jsxFactory: undefined | string;
30
+ jsxFragment: undefined | string;
29
31
  };
30
32
 
31
33
  function sleep(ms: number) {
@@ -48,6 +50,18 @@ export default async function publish(props: Props): Promise<void> {
48
50
  __path__,
49
51
  } = config;
50
52
 
53
+ const envRootObj =
54
+ props.env && config.env ? config.env[props.env] || {} : config;
55
+
56
+ assert(
57
+ envRootObj.compatibility_date || props.compatibilityDate,
58
+ "A compatibility_date is required when publishing. Add one to your wrangler.toml file, or pass it in your terminal as --compatibility_date. See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
59
+ );
60
+
61
+ if (accountId === undefined) {
62
+ throw new Error("No account_id provided.");
63
+ }
64
+
51
65
  const triggers = props.triggers || config.triggers?.crons;
52
66
  const routes = props.routes || config.routes;
53
67
 
@@ -120,18 +134,24 @@ export default async function publish(props: Props): Promise<void> {
120
134
  ...(jsxFragment && { jsxFragment }),
121
135
  });
122
136
 
123
- const chunks = Object.entries(result.metafile.outputs).find(
124
- ([_path, { entryPoint }]) =>
125
- entryPoint ===
126
- (props.public
127
- ? path.join(path.dirname(file), "static-asset-facade.js")
128
- : file)
137
+ // result.metafile is defined because of the `metafile: true` option above.
138
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
139
+ const metafile = result.metafile!;
140
+ const expectedEntryPoint = props.public
141
+ ? path.join(path.dirname(file), "static-asset-facade.js")
142
+ : file;
143
+ const outputEntry = Object.entries(metafile.outputs).find(
144
+ ([, { entryPoint }]) => entryPoint === expectedEntryPoint
129
145
  );
130
-
146
+ if (outputEntry === undefined) {
147
+ throw new Error(
148
+ `Cannot find entry-point "${expectedEntryPoint}" in generated bundle.`
149
+ );
150
+ }
131
151
  const { format } = props;
132
152
  const bundle = {
133
- type: chunks[1].exports.length > 0 ? "esm" : "commonjs",
134
- exports: chunks[1].exports,
153
+ type: outputEntry[1].exports.length > 0 ? "esm" : "commonjs",
154
+ exports: outputEntry[1].exports,
135
155
  };
136
156
 
137
157
  // TODO: instead of bundling the facade with the worker, we should just bundle the worker and expose it as a module.
@@ -148,13 +168,13 @@ export default async function publish(props: Props): Promise<void> {
148
168
  return;
149
169
  }
150
170
 
151
- const content = await readFile(chunks[0], { encoding: "utf-8" });
171
+ const content = await readFile(outputEntry[0], { encoding: "utf-8" });
152
172
  await destination.cleanup();
153
173
 
154
174
  // if config.migrations
155
175
  // get current migration tag
156
176
  let migrations;
157
- if ("migrations" in config) {
177
+ if (config.migrations !== undefined) {
158
178
  const scripts = await fetchResult<{ id: string; migration_tag: string }[]>(
159
179
  `/accounts/${accountId}/workers/scripts`
160
180
  );
@@ -190,17 +210,11 @@ export default async function publish(props: Props): Promise<void> {
190
210
  }
191
211
  }
192
212
 
193
- const assets =
194
- props.public || props.site || props.config.site?.bucket // TODO: allow both
195
- ? await syncAssets(
196
- accountId,
197
- scriptName,
198
- props.public || props.site || props.config.site?.bucket,
199
- false
200
- )
201
- : { manifest: undefined, namespace: undefined };
202
-
203
- const envRootObj = props.env ? config.env[props.env] || {} : config;
213
+ const assetPath = props.public || props.site || props.config.site?.bucket; // TODO: allow both
214
+ const assets = assetPath
215
+ ? await syncAssets(accountId, scriptName, assetPath, false)
216
+ : { manifest: undefined, namespace: undefined };
217
+
204
218
  const bindings: CfWorkerInit["bindings"] = {
205
219
  kv_namespaces: envRootObj.kv_namespaces?.concat(
206
220
  assets.namespace
@@ -215,7 +229,7 @@ export default async function publish(props: Props): Promise<void> {
215
229
  const worker: CfWorkerInit = {
216
230
  name: scriptName,
217
231
  main: {
218
- name: path.basename(chunks[0]),
232
+ name: path.basename(outputEntry[0]),
219
233
  content: content,
220
234
  type: bundle.type === "esm" ? "esm" : "commonjs",
221
235
  },
package/src/sites.tsx CHANGED
@@ -89,7 +89,11 @@ export async function syncAssets(
89
89
  );
90
90
 
91
91
  const manifest = {};
92
- const upload = [];
92
+ const upload: {
93
+ key: string;
94
+ value: string;
95
+ base64: boolean;
96
+ }[] = [];
93
97
  // TODO: this can be more efficient by parallelising
94
98
  for await (const file of getFilesInFolder(dirPath)) {
95
99
  // TODO: "exclude:" config
package/src/user.tsx CHANGED
@@ -206,7 +206,7 @@
206
206
  */
207
207
 
208
208
  import React from "react";
209
- import { render } from "ink";
209
+ import { render, Text } from "ink";
210
210
  import Table from "ink-table";
211
211
  import SelectInput from "ink-select-input";
212
212
  import fetch from "node-fetch";
@@ -298,9 +298,9 @@ let initialised = false;
298
298
 
299
299
  // we do this because we have some async stuff
300
300
  // TODO: this should just happen in the top level
301
- // abd we should fiure out how to do top level await
301
+ // and we should figure out how to do top level await
302
302
  export async function initialise(): Promise<void> {
303
- // get refreshtoken/accesstoken from fs if exists
303
+ // get refreshToken/accessToken from fs if exists
304
304
  try {
305
305
  // if CF_API_TOKEN available, use that
306
306
  if (process.env.CF_API_TOKEN) {
@@ -350,7 +350,9 @@ export function getAPIToken(): string {
350
350
  }
351
351
 
352
352
  throwIfNotInitialised();
353
- return LocalState.accessToken?.value;
353
+ // `throwIfNotInitialised()` ensures that the accessToken is guaranteed to be defined.
354
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
355
+ return LocalState.accessToken!.value;
354
356
  }
355
357
 
356
358
  interface AccessContext {
@@ -971,14 +973,14 @@ export async function getAccountId() {
971
973
  });
972
974
  } catch (err) {
973
975
  // probably offline
976
+ return;
974
977
  }
975
- if (!response) return;
976
- let accountId: string;
977
- // @ts-expect-error need to type this response
978
- const responseJSON: {
978
+
979
+ let accountId: string | undefined;
980
+ const responseJSON = (await response.json()) as {
979
981
  success: boolean;
980
982
  result: { id: string; account: { id: string; name: string } }[];
981
- } = await response.json();
983
+ };
982
984
 
983
985
  if (responseJSON.success === true) {
984
986
  if (responseJSON.result.length === 1) {
@@ -1010,13 +1012,16 @@ export function ChooseAccount(props: {
1010
1012
  onSelect: (item) => void;
1011
1013
  }) {
1012
1014
  return (
1013
- <SelectInput
1014
- items={props.accounts.map((item) => ({
1015
- key: item.id,
1016
- label: item.name,
1017
- value: item,
1018
- }))}
1019
- onSelect={props.onSelect}
1020
- />
1015
+ <>
1016
+ <Text bold>Select an account from below:</Text>
1017
+ <SelectInput
1018
+ items={props.accounts.map((item) => ({
1019
+ key: item.id,
1020
+ label: item.name,
1021
+ value: item,
1022
+ }))}
1023
+ onSelect={props.onSelect}
1024
+ />
1025
+ </>
1021
1026
  );
1022
1027
  }