@sanity/cli 3.84.1-next.2 → 3.85.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.
@@ -3911,7 +3911,7 @@ function ensureNpx() {
3911
3911
  );
3912
3912
  }
3913
3913
  }
3914
- const helpText$c = `
3914
+ const helpText$d = `
3915
3915
  Runs a given code modification script on the current studio folder.
3916
3916
  Running the command without a specified codemod name will list available transformations.
3917
3917
 
@@ -3933,7 +3933,7 @@ Examples
3933
3933
  name: "codemod",
3934
3934
  signature: "[CODEMOD_NAME]",
3935
3935
  description: "Updates Sanity Studio codebase with a code modification script",
3936
- helpText: helpText$c,
3936
+ helpText: helpText$d,
3937
3937
  action: codemodAction
3938
3938
  };
3939
3939
  var lodash_isplainobject, hasRequiredLodash_isplainobject;
@@ -7471,7 +7471,7 @@ const docsCommand = {
7471
7471
  const { output } = context, { print } = output, url = "https://www.sanity.io/docs";
7472
7472
  print(`Opening ${url}`), await open(url);
7473
7473
  }
7474
- }, helpText$b = `
7474
+ }, helpText$c = `
7475
7475
  Options
7476
7476
  --port <port> Port to start emulator on
7477
7477
 
@@ -7481,20 +7481,92 @@ Examples
7481
7481
 
7482
7482
  # Start dev server on specific port
7483
7483
  sanity functions dev --port 3333
7484
- `, defaultFlags$3 = {
7484
+ `, defaultFlags$4 = {
7485
7485
  port: 8080
7486
7486
  }, devFunctionsCommand = {
7487
7487
  name: "dev",
7488
7488
  group: "functions",
7489
- helpText: helpText$b,
7489
+ helpText: helpText$c,
7490
7490
  signature: "",
7491
7491
  description: "Start the Sanity Function emulator",
7492
7492
  hideFromHelp: !0,
7493
7493
  async action(args, context) {
7494
- const { output } = context, { print } = output, flags = { ...defaultFlags$3, ...args.extOptions }, { devAction } = await import("@sanity/runtime-cli");
7495
- devAction(flags.port), print(`Server is running on port ${flags.port}
7494
+ const { output } = context, { print } = output, flags = { ...defaultFlags$4, ...args.extOptions }, { functionsActions } = await import("@sanity/runtime-cli");
7495
+ functionsActions.dev.dev(flags.port), print(`Server is running on port ${flags.port}
7496
7496
  `), open(`http://localhost:${flags.port}`);
7497
7497
  }
7498
+ }, helpText$b = `
7499
+ Options
7500
+ --name <name> The name of the function
7501
+ --add Add or update an environment variable
7502
+ --remove Remove an environment variable
7503
+ --key <key> The name of the environment variable
7504
+ --value <value> The value of the environment variable
7505
+
7506
+ Examples
7507
+ # Add or update an environment variable
7508
+ sanity functions env --name echo --add --key API_URL --value https://api.example.com/
7509
+
7510
+ # Remove an environment variable
7511
+ sanity functions env --name echo --remove --key API_URL
7512
+ `, defaultFlags$3 = {
7513
+ name: "",
7514
+ add: !1,
7515
+ remove: !1,
7516
+ key: "",
7517
+ value: ""
7518
+ }, envFunctionsCommand = {
7519
+ name: "env",
7520
+ group: "functions",
7521
+ helpText: helpText$b,
7522
+ signature: "",
7523
+ description: "Add or remove an environment variable for a Sanity function",
7524
+ hideFromHelp: !0,
7525
+ async action(args, context) {
7526
+ const { apiClient, output } = context, { print } = output, flags = { ...defaultFlags$3, ...args.extOptions }, client2 = apiClient({
7527
+ requireUser: !0,
7528
+ requireProject: !1
7529
+ });
7530
+ if (flags.name === "") {
7531
+ print("You must provide a function name");
7532
+ return;
7533
+ }
7534
+ const token2 = client2.config().token, { blueprintsActions, functionsActions, utils: utils2 } = await import("@sanity/runtime-cli"), { deployedStack } = await blueprintsActions.blueprint.readBlueprintOnDisk({
7535
+ getStack: !0,
7536
+ token: token2
7537
+ });
7538
+ if (!deployedStack) {
7539
+ print("Stack not found");
7540
+ return;
7541
+ }
7542
+ const projectId = blueprintsActions.blueprint.readConfigFile()?.projectId, { externalId } = utils2.findFunctions.findFunctionByName(
7543
+ deployedStack,
7544
+ flags.name
7545
+ );
7546
+ if (token2 && projectId) {
7547
+ if (flags.add) {
7548
+ print(`Updating "${flags.key}" environment variable in "${flags.name}"`);
7549
+ const result = await functionsActions.env.update.update(
7550
+ externalId,
7551
+ flags.key,
7552
+ flags.value,
7553
+ {
7554
+ token: token2,
7555
+ projectId
7556
+ }
7557
+ );
7558
+ result.ok ? print(`Update of ${flags.key} succeeded`) : (print(`Failed to update ${flags.key}`), print(`Error: ${result.error || "Unknown error"}`));
7559
+ } else if (flags.remove) {
7560
+ print(`Removing "${flags.key}" environment variable in "${flags.name}"`);
7561
+ const result = await functionsActions.env.remove.remove(externalId, flags.key, {
7562
+ token: token2,
7563
+ projectId
7564
+ });
7565
+ result.ok ? print(`Remove of ${flags.key} succeeded`) : (print(`Failed to remove ${flags.key}`), print(`Error: ${result.error || "Unknown error"}`));
7566
+ }
7567
+ } else
7568
+ print("You must run this command from a blueprints project");
7569
+ }
7498
7570
  }, functionsGroup = {
7499
7571
  name: "functions",
7500
7572
  signature: "[COMMAND]",
@@ -7503,13 +7575,23 @@ Examples
7503
7575
  hideFromHelp: !0
7504
7576
  }, helpText$a = `
7505
7577
  Options
7506
- --id <id> The ID of the function to retrieve logs for
7578
+ --name <name> The name of the function to retrieve logs for
7579
+ --limit <limit> The number of log entries to retrieve
7580
+ --json If set return json
7507
7581
 
7508
7582
  Examples
7509
7583
  # Retrieve logs for Sanity Function abcd1234
7510
- sanity functions logs --id abcd1234
7584
+ sanity functions logs --name echo
7585
+
7586
+ # Retrieve the last two log entries for Sanity Function abcd1234
7587
+ sanity functions logs --name echo --limit 2
7588
+
7589
+ # Retrieve logs for Sanity Function abcd1234 in json format
7590
+ sanity functions logs --name echo --json
7511
7591
  `, defaultFlags$2 = {
7512
- id: void 0
7592
+ name: "",
7593
+ limit: 50,
7594
+ json: !1
7513
7595
  }, logsFunctionsCommand = {
7514
7596
  name: "logs",
7515
7597
  group: "functions",
@@ -7522,35 +7604,72 @@ Examples
7522
7604
  requireUser: !0,
7523
7605
  requireProject: !1
7524
7606
  });
7525
- if (flags.id) {
7526
- const token2 = client2.config().token;
7527
- if (token2) {
7528
- const { logsAction } = await import("@sanity/runtime-cli"), result = await logsAction(flags.id, token2);
7529
- print(JSON.stringify(result, null, 2));
7607
+ if (flags.name === "") {
7608
+ print("You must provide a function name");
7609
+ return;
7610
+ }
7611
+ const token2 = client2.config().token, { blueprintsActions, utils: utils2 } = await import("@sanity/runtime-cli"), { deployedStack } = await blueprintsActions.blueprint.readBlueprintOnDisk({
7612
+ getStack: !0,
7613
+ token: token2
7614
+ });
7615
+ if (!deployedStack) {
7616
+ print("Stack not found");
7617
+ return;
7618
+ }
7619
+ const projectId = blueprintsActions.blueprint.readConfigFile()?.projectId, { externalId } = utils2.findFunctions.findFunctionByName(
7620
+ deployedStack,
7621
+ flags.name
7622
+ );
7623
+ if (token2 && projectId) {
7624
+ const { functionsActions } = await import("@sanity/runtime-cli"), { ok, error: error2, logs, total } = await functionsActions.logs.logs(
7625
+ externalId,
7626
+ { limit: flags.limit },
7627
+ { token: token2, projectId }
7628
+ );
7629
+ if (!ok) {
7630
+ print(`Error: ${error2 || "Unknown error"}`);
7631
+ return;
7632
+ }
7633
+ const filteredLogs = logs.filter(
7634
+ (entry) => entry.level && entry.message
7635
+ );
7636
+ if (filteredLogs.length === 0) {
7637
+ print(`No logs found for function ${flags.name}`);
7638
+ return;
7639
+ }
7640
+ if (flags.json)
7641
+ print(JSON.stringify(filteredLogs, null, 2));
7642
+ else {
7643
+ print(`Found ${total} log entries for function ${flags.name}`), logs.length < total && print(`Here are the last ${filteredLogs.length} entries`), print(`
7644
+ `);
7645
+ for (const log of filteredLogs) {
7646
+ const { time, level, message } = log, date = new Date(time);
7647
+ print(`${date.toLocaleDateString()} ${date.toLocaleTimeString()} ${level} ${message}`);
7648
+ }
7530
7649
  }
7531
7650
  } else
7532
- print("You must provide a function ID");
7651
+ print("You must run this command from a blueprints project");
7533
7652
  }
7534
7653
  }, helpText$9 = `
7535
7654
  Options
7536
7655
  --data <data> Data to send to the function
7537
7656
  --file <file> Read data from file and send to the function
7538
- --path <path> Path to your Sanity Function code
7657
+ --name <name> The name of your Sanity Function
7539
7658
  --timeout <timeout> Execution timeout value in seconds
7540
7659
 
7541
7660
  Examples
7542
7661
  # Test function passing event data on command line
7543
- sanity functions test --path ./test.ts --data '{ "id": 1 }'
7662
+ sanity functions test --name echo --data '{ "id": 1 }'
7544
7663
 
7545
7664
  # Test function passing event data via a file
7546
- sanity functions test -path ./test.js --file 'payload.json'
7665
+ sanity functions test -name echo --file 'payload.json'
7547
7666
 
7548
7667
  # Test function passing event data on command line and cap execution time to 60 seconds
7549
- sanity functions test -path ./test.ts --data '{ "id": 1 }' --timeout 60
7668
+ sanity functions test -name echo --data '{ "id": 1 }' --timeout 60
7550
7669
  `, defaultFlags$1 = {
7551
7670
  data: void 0,
7552
7671
  file: void 0,
7553
- path: void 0,
7672
+ name: "",
7554
7673
  timeout: 5
7555
7674
  // seconds
7556
7675
  }, testFunctionsCommand = {
@@ -7562,15 +7681,20 @@ Examples
7562
7681
  hideFromHelp: !0,
7563
7682
  async action(args, context) {
7564
7683
  const { output } = context, { print } = output, flags = { ...defaultFlags$1, ...args.extOptions };
7565
- if (flags.path) {
7566
- const { testAction } = await import("@sanity/runtime-cli"), { json: json2, logs, error: error2 } = await testAction(flags.path, {
7567
- data: flags.data,
7568
- file: flags.file,
7569
- timeout: flags.timeout
7570
- });
7571
- error2 ? print(error2.toString()) : (print("Logs:"), print(logs), print("Response:"), print(JSON.stringify(json2, null, 2)));
7572
- } else
7573
- print("You must provide a path to the Sanity Function code");
7684
+ if (flags.name === "") {
7685
+ print("You must provide a function name");
7686
+ return;
7687
+ }
7688
+ const { blueprintsActions, functionsActions, utils: utils2 } = await import("@sanity/runtime-cli"), { parsedBlueprint } = await blueprintsActions.blueprint.readBlueprintOnDisk({
7689
+ getStack: !1
7690
+ }), src2 = utils2.findFunctions.getFunctionSource(parsedBlueprint, flags.name);
7691
+ src2 || print(`Error: Function ${flags.name} has no source code`);
7692
+ const { json: json2, logs, error: error2 } = await functionsActions.test.testAction(src2, {
7693
+ data: flags.data,
7694
+ file: flags.file,
7695
+ timeout: flags.timeout
7696
+ });
7697
+ error2 ? print(error2.toString()) : (print("Logs:"), print(logs), print("Response:"), print(JSON.stringify(json2, null, 2)));
7574
7698
  }
7575
7699
  };
7576
7700
  var leven$1 = { exports: {} }, hasRequiredLeven;
@@ -53998,7 +54122,8 @@ const baseCommands = [
53998
54122
  functionsGroup,
53999
54123
  devFunctionsCommand,
54000
54124
  logsFunctionsCommand,
54001
- testFunctionsCommand
54125
+ testFunctionsCommand,
54126
+ envFunctionsCommand
54002
54127
  ], copyProperty = (to, from2, property, ignoreNonConfigurable) => {
54003
54128
  if (property === "length" || property === "prototype" || property === "arguments" || property === "caller")
54004
54129
  return;