storyblok 4.11.0 → 4.12.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.
package/dist/index.mjs CHANGED
@@ -6146,11 +6146,11 @@ const generateProject = async (blueprint, projectName, targetPath = process.cwd(
6146
6146
  handleFileSystemError("read", error);
6147
6147
  }
6148
6148
  };
6149
- const createEnvFile = async (projectPath, accessToken, additionalVars) => {
6149
+ const createEnvFile = async (projectPath, storyblokVars, additionalVars) => {
6150
6150
  try {
6151
6151
  const envPath = path.join(projectPath, ".env");
6152
6152
  let envContent = `# Storyblok Configuration
6153
- STORYBLOK_DELIVERY_API_TOKEN=${accessToken}
6153
+ ${Object.entries(storyblokVars).map(([key, value]) => `${key}=${value}`).join("\n")}
6154
6154
  `;
6155
6155
  if (additionalVars && Object.keys(additionalVars).length > 0) ;
6156
6156
  await saveToFile(envPath, envContent);
@@ -6158,6 +6158,38 @@ STORYBLOK_DELIVERY_API_TOKEN=${accessToken}
6158
6158
  throw new Error(`Failed to create .env file: ${error.message}`);
6159
6159
  }
6160
6160
  };
6161
+ async function handleEnvFileCreation(resolvedPath, token, region) {
6162
+ const envVars = {};
6163
+ if (token) {
6164
+ envVars.STORYBLOK_DELIVERY_API_TOKEN = token;
6165
+ }
6166
+ if (region) {
6167
+ envVars.STORYBLOK_REGION = region;
6168
+ }
6169
+ if (Object.keys(envVars).length === 0) {
6170
+ konsola.info("No environment variables to write");
6171
+ return true;
6172
+ }
6173
+ try {
6174
+ await createEnvFile(resolvedPath, envVars);
6175
+ const writtenKeys = Object.keys(envVars).join(", ");
6176
+ konsola.ok(`Created .env file with: ${writtenKeys}`, true);
6177
+ return true;
6178
+ } catch (error) {
6179
+ konsola.warn(`Failed to create .env file: ${error.message}`);
6180
+ if (token) {
6181
+ konsola.info(
6182
+ `You can manually add STORYBLOK_DELIVERY_API_TOKEN to your .env file.`
6183
+ );
6184
+ }
6185
+ if (region) {
6186
+ konsola.info(
6187
+ `You can manually add STORYBLOK_REGION to your .env file.`
6188
+ );
6189
+ }
6190
+ return false;
6191
+ }
6192
+ }
6161
6193
  const generateSpaceUrl = (spaceId, region) => {
6162
6194
  const domain = appDomains[region];
6163
6195
  const utmParams = new URLSearchParams({
@@ -6225,22 +6257,18 @@ function showNextSteps(technologyTemplate, finalProjectPath) {
6225
6257
  `);
6226
6258
  konsola.info(`Or check the dedicated guide at: ${chalk.hex(colorPalette.PRIMARY)(`https://www.storyblok.com/docs/guides/${technologyTemplate}`)}`);
6227
6259
  }
6228
- async function handleEnvFileCreation(resolvedPath, token) {
6229
- try {
6230
- await createEnvFile(resolvedPath, token);
6231
- konsola.ok(`Created .env file with Storyblok access token`, true);
6232
- return true;
6233
- } catch (error) {
6234
- konsola.warn(`Failed to create .env file: ${error.message}`);
6235
- konsola.info(`You can manually add this token to your .env file: ${token}`);
6236
- return false;
6237
- }
6238
- }
6239
6260
  const program$3 = getProgram();
6240
- program$3.command(`${commands.CREATE} [project-path]`).alias("c").description(`Scaffold a new project using Storyblok`).option("-t, --template <template>", "technology starter template").option("-b, --blueprint <blueprint>", "[DEPRECATED] use --template instead").option("--skip-space", "skip space creation").option("--token <token>", "Storyblok access token (skip space creation and use this token)").action(async (projectPath, options) => {
6261
+ program$3.command(`${commands.CREATE} [project-path]`).alias("c").description(`Scaffold a new project using Storyblok`).option("-t, --template <template>", "technology starter template").option("-b, --blueprint <blueprint>", "[DEPRECATED] use --template instead").option("--skip-space", "skip space creation").option("--token <token>", "Storyblok access token (skip space creation and use this token)").option(
6262
+ "-r, --region <region>",
6263
+ `The region to apply to the generated project template (does not affect space creation).`
6264
+ ).action(async (projectPath, options) => {
6241
6265
  konsola.title(`${commands.CREATE}`, colorPalette.CREATE);
6242
6266
  const verbose = program$3.opts().verbose;
6243
6267
  const { template, blueprint, token } = options;
6268
+ if (options.region && !isRegion(options.region)) {
6269
+ handleError(new CommandError(`The provided region: ${options.region} is not valid. Please use one of the following values: ${Object.values(regions).join(" | ")}`));
6270
+ return;
6271
+ }
6244
6272
  let selectedTemplate = template;
6245
6273
  if (blueprint && !template) {
6246
6274
  konsola.warn(`The --blueprint flag is deprecated. Please use --template instead.`);
@@ -6254,6 +6282,10 @@ program$3.command(`${commands.CREATE} [project-path]`).alias("c").description(`S
6254
6282
  return;
6255
6283
  }
6256
6284
  const { password, region } = state;
6285
+ if (options.region && options.region !== region && !options.skipSpace && !token) {
6286
+ handleError(new CommandError(`Cannot create space in region "${options.region}". Your account is configured for region "${region}". Space creation must use your account's region.`));
6287
+ return;
6288
+ }
6257
6289
  mapiClient({
6258
6290
  token: {
6259
6291
  accessToken: password
@@ -6324,11 +6356,14 @@ program$3.command(`${commands.CREATE} [project-path]`).alias("c").description(`S
6324
6356
  let userData;
6325
6357
  let whereToCreateSpace = "personal";
6326
6358
  if (token) {
6327
- await handleEnvFileCreation(resolvedPath, token);
6359
+ await handleEnvFileCreation(resolvedPath, token, options.region || region);
6328
6360
  showNextSteps(technologyTemplate, finalProjectPath);
6329
6361
  return;
6330
6362
  }
6331
6363
  if (options.skipSpace) {
6364
+ if (options.region || region) {
6365
+ await handleEnvFileCreation(resolvedPath, void 0, options.region || region);
6366
+ }
6332
6367
  showNextSteps(technologyTemplate, finalProjectPath);
6333
6368
  return;
6334
6369
  }
@@ -6383,7 +6418,7 @@ program$3.command(`${commands.CREATE} [project-path]`).alias("c").description(`S
6383
6418
  createdSpace = await createSpace(spaceToCreate);
6384
6419
  spinnerSpace.succeed(`Space "${chalk.hex(colorPalette.PRIMARY)(toHumanReadable(projectName))}" created successfully`);
6385
6420
  if (createdSpace?.first_token) {
6386
- await handleEnvFileCreation(resolvedPath, createdSpace.first_token);
6421
+ await handleEnvFileCreation(resolvedPath, createdSpace.first_token, region);
6387
6422
  }
6388
6423
  if (createdSpace?.id) {
6389
6424
  try {