@theholocron/cli 5.0.0-alpha.551 → 5.0.0-alpha.552

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/cli.mjs CHANGED
@@ -2851,7 +2851,8 @@ const ENVIRONMENT_LAYERS = [
2851
2851
  "local",
2852
2852
  "dev",
2853
2853
  "ci",
2854
- "prod"
2854
+ "prod",
2855
+ "preview"
2855
2856
  ];
2856
2857
  /** Whether `value` is exactly one of the layer words (case-sensitive: `Prod` is a literal Environment name). */
2857
2858
  function isEnvironmentLayer(value) {
@@ -10208,7 +10209,12 @@ function stageDeploy(input) {
10208
10209
  copyFileSync(join(packageDir, "api", "webhook.mjs"), join(outDir, "api", "webhook.mjs"));
10209
10210
  for (const entry of readdirSync(join(packageDir, "api")).filter((f) => f.endsWith(".mjs") && f !== "webhook.mjs")) copyFileSync(join(packageDir, "api", entry), join(outDir, "api", entry));
10210
10211
  copyFileSync(distIndex, join(outDir, "dist", "index.mjs"));
10211
- copyFileSync(join(packageDir, "vercel.json"), join(outDir, "vercel.json"));
10212
+ if (input.omitCrons) {
10213
+ const vercelConfig = JSON.parse(readFileSync(join(packageDir, "vercel.json"), "utf8"));
10214
+ delete vercelConfig.crons;
10215
+ writeFileSync(join(outDir, "vercel.json"), stringifyJson(vercelConfig));
10216
+ print(" left the crons out of vercel.json");
10217
+ } else copyFileSync(join(packageDir, "vercel.json"), join(outDir, "vercel.json"));
10212
10218
  const deployPkg = {
10213
10219
  name: pkg.name,
10214
10220
  version: pkg.version,
@@ -10308,6 +10314,62 @@ async function checkProjectDomain(deployment, project, domain) {
10308
10314
  message: `${deployment.providerName} project "${project}" does not serve ${domain} (it serves ${served.length ? served.join(", ") : "no domain"}); nothing was written`
10309
10315
  };
10310
10316
  }
10317
+ // src/commands/preview-instance/preview-instance.ts
10318
+ /**
10319
+ * The preview instance an app declares in its `deployment` options (`instances: { preview: { project, domain } }`), or
10320
+ * `undefined` when it declares none (spec `tech-sentinel-preview-instance`). The one reading of the declaration, shared by
10321
+ * `deploy`, `sync` and `setup`.
10322
+ *
10323
+ * @param app - The app's name, for the message.
10324
+ * @param options - The `deployment` provider's options from the app's config.
10325
+ * @throws {ConfigError} For an `instances.preview` that is not an object with a `project` name.
10326
+ */
10327
+ function previewInstance(app, options) {
10328
+ const instances = options.instances;
10329
+ const declared = instances && typeof instances === "object" ? instances.preview : void 0;
10330
+ if (declared === void 0) return void 0;
10331
+ if (!declared || typeof declared !== "object") throw new ConfigError$1(`${app}: \`deployment\` option \`instances.preview\` must be an object with a \`project\``);
10332
+ const { project, domain } = declared;
10333
+ if (typeof project !== "string" || project === "") throw new ConfigError$1(`${app}: \`instances.preview\` needs a \`project\` name`);
10334
+ return {
10335
+ project,
10336
+ ...typeof domain === "string" ? { domain } : {}
10337
+ };
10338
+ }
10339
+ // src/commands/app-instance/app-instance.ts
10340
+ /**
10341
+ * The project, domain and deployment target an app's `--env` target goes to. `production` is the project named like the
10342
+ * app, as its production target. `preview` is the instance the app declares in its `deployment` options
10343
+ * (`instances: { preview: { project, domain } }`): a project of its own, deployed as that project's production target
10344
+ * with the `crons` left out of its `vercel.json`, because the crons belong to production. An app that declares no preview
10345
+ * instance keeps the earlier meaning: a Vercel preview deployment of its one project.
10346
+ *
10347
+ * @param app - The app folder's name.
10348
+ * @param options - The `deployment` provider's options from the app's config.
10349
+ * @param target - The target `--env` named.
10350
+ * @throws {ConfigError} For an `instances.preview` that is not an object with a `project` name.
10351
+ */
10352
+ function appInstance(app, options, target) {
10353
+ const domain = typeof options.domain === "string" ? { domain: options.domain } : {};
10354
+ if (target === "production") return {
10355
+ project: app,
10356
+ ...domain,
10357
+ production: true,
10358
+ omitCrons: false
10359
+ };
10360
+ const declared = previewInstance(app, options);
10361
+ if (!declared) return {
10362
+ project: app,
10363
+ ...domain,
10364
+ production: false,
10365
+ omitCrons: false
10366
+ };
10367
+ return {
10368
+ ...declared,
10369
+ production: true,
10370
+ omitCrons: true
10371
+ };
10372
+ }
10311
10373
  // src/commands/app-plan/app-plan.ts
10312
10374
  /**
10313
10375
  * Reads what an app's own config declares: its `deployment` and `workers` providers and whether `repo.secrets.deploy`
@@ -10328,9 +10390,10 @@ function appPlan(config) {
10328
10390
  }
10329
10391
  // src/commands/app-deploy/app-deploy.ts
10330
10392
  /**
10331
- * `holocron deploy <app> --env <env>`: builds the app, stages it and deploys the staged files to the project that takes
10332
- * the app folder's name, once for each environment `--env` names (production, then preview). Before anything is written the
10333
- * project must serve the app's configured `domain`. An app with no `deployment` provider has nothing to deploy and is
10393
+ * `holocron deploy <app> --env <env>`: builds the app, stages it and deploys the staged files, once for each environment
10394
+ * `--env` names (production, then preview). Production goes to the project that takes the app folder's name; preview goes
10395
+ * to the instance the app declares (`appInstance()`: a project of its own, its crons left out), or to a preview deployment
10396
+ * of the same project when it declares none. Before anything is written every project must serve its configured `domain`. An app with no `deployment` provider has nothing to deploy and is
10334
10397
  * reported, not failed (Comlink's Worker is made by `setup notifications`; Council is installed by hand).
10335
10398
  *
10336
10399
  * @param input - The app, the environment, its loaded config and the seams tests replace.
@@ -10360,12 +10423,23 @@ async function runAppDeploy(input) {
10360
10423
  print(style.header(`Holocron deploy ${app.name} → ${env.targets.join(" + ")}${dryRun ? " (dry-run)" : ""}`));
10361
10424
  await loader.load();
10362
10425
  if (!loader.has("deployment")) return fail(`${app.name}: the deployment provider did not load`);
10363
- const domain = plan.deployment.options.domain;
10364
- const checked = await checkProjectDomain(loader.get("deployment"), app.name, typeof domain === "string" ? domain : void 0);
10365
- if (checked.status === "fail") return fail(checked.message);
10366
- print(` ${style.dim(checked.message)}`);
10426
+ let instances;
10427
+ try {
10428
+ instances = env.targets.map((target) => ({
10429
+ target,
10430
+ instance: appInstance(app.name, plan.deployment.options, target)
10431
+ }));
10432
+ } catch (err) {
10433
+ return fail(errorMessage(err));
10434
+ }
10435
+ for (const { instance } of instances) {
10436
+ const checked = await checkProjectDomain(loader.get("deployment"), instance.project, instance.domain);
10437
+ if (checked.status === "fail") return fail(checked.message);
10438
+ print(` ${style.dim(checked.message)}`);
10439
+ }
10367
10440
  if (dryRun) {
10368
- const message = `would: build and stage ${app.path}, then deploy it to ${app.name} (${env.targets.join(" + ")})`;
10441
+ const where = instances.map(({ target, instance }) => `${instance.project} (${target})`).join(" + ");
10442
+ const message = `would: build and stage ${app.path}, then deploy it to ${where}`;
10369
10443
  print(` ${style.dim(`… ${message}`)}`);
10370
10444
  return {
10371
10445
  status: "dry-run",
@@ -10374,18 +10448,28 @@ async function runAppDeploy(input) {
10374
10448
  }
10375
10449
  const built = input.build(app.dir);
10376
10450
  if (built.status === "fail") return fail(built.message);
10377
- const staged = input.stage({
10378
- packageDir: app.dir,
10379
- print
10380
- });
10381
- if (staged.status === "fail" || !staged.outDir) return fail(`stage: ${staged.message ?? "no payload was made"}`);
10382
- for (const target of env.targets) {
10451
+ const staged = /* @__PURE__ */ new Map();
10452
+ for (const { target, instance } of instances) {
10453
+ let dir = staged.get(instance.omitCrons);
10454
+ if (!dir) {
10455
+ const result = input.stage({
10456
+ packageDir: app.dir,
10457
+ print,
10458
+ ...instance.omitCrons ? {
10459
+ omitCrons: true,
10460
+ outDir: join(app.dir, ".vercel-deploy-preview")
10461
+ } : {}
10462
+ });
10463
+ if (result.status === "fail" || !result.outDir) return fail(`stage: ${result.message ?? "no payload was made"}`);
10464
+ dir = result.outDir;
10465
+ staged.set(instance.omitCrons, dir);
10466
+ }
10383
10467
  const report = await input.deployFiles({
10384
10468
  loaded,
10385
10469
  context,
10386
- projectId: app.name,
10387
- dir: staged.outDir,
10388
- ...target === "production" ? { target: "production" } : {},
10470
+ projectId: instance.project,
10471
+ dir,
10472
+ ...instance.production ? { target: "production" } : {},
10389
10473
  loader,
10390
10474
  print
10391
10475
  });
@@ -10402,7 +10486,8 @@ const APP_TARGETS = ["production", "preview"];
10402
10486
  /**
10403
10487
  * Reads the `--env` of `deploy <app>` and `sync <app>`: the standard environments of `@theholocron/env-utils`
10404
10488
  * (`environment.normalize()`, so `prod` is `production`), the flag repeated for more than one. An app is deployed to
10405
- * `production` and `preview`; both take their secrets from the `prod` vault layer. Any other environment is refused with
10489
+ * `production` and `preview`; production takes its secrets from the `prod` vault layer and preview from the `preview`
10490
+ * layer (`appLayer()`), with no fallback between them. Any other environment is refused with
10406
10491
  * the reason: `local`, `development` and `ci` are never synced or deployed to, and `test` has no deployment target.
10407
10492
  *
10408
10493
  * @param values - The `--env` (or `--environment`) values, one or several.
@@ -10418,10 +10503,7 @@ function parseAppEnv(values) {
10418
10503
  if (normalized !== "production" && normalized !== "preview") throw new ConfigError$1(`--env ${value} is not an environment an app is deployed or synced to (accepted: ${APP_TARGETS.join(", ")}); \`sync secrets --env ci\` fills GitHub Actions`);
10419
10504
  wanted.add(normalized);
10420
10505
  }
10421
- return {
10422
- layer: "prod",
10423
- targets: APP_TARGETS.filter((target) => wanted.has(target))
10424
- };
10506
+ return { targets: APP_TARGETS.filter((target) => wanted.has(target)) };
10425
10507
  }
10426
10508
  // src/commands/build-app/build-app.ts
10427
10509
  /**
@@ -19282,7 +19364,7 @@ async function readLayers(vault, slug, layers = ENVIRONMENT_LAYERS) {
19282
19364
  blind = true;
19283
19365
  }
19284
19366
  } catch (err) {
19285
- if (layer === "local") continue;
19367
+ if (layer === "local" || layer === "preview") continue;
19286
19368
  rows.push({
19287
19369
  id: `skip:${layer}`,
19288
19370
  title: `Environment "${environment}"`,
@@ -19832,7 +19914,7 @@ async function runDoctorSecrets(input) {
19832
19914
  }
19833
19915
  if (input.shared) {
19834
19916
  ran = true;
19835
- const layers = ENVIRONMENT_LAYERS.filter((layer) => wants(layer));
19917
+ const layers = ENVIRONMENT_LAYERS.filter((layer) => layer !== "preview" && wants(layer));
19836
19918
  if (layers.length < 2) rows.push({
19837
19919
  id: "shared:layers",
19838
19920
  title: "compare the layers",
@@ -24445,6 +24527,15 @@ async function runSetup(input) {
24445
24527
  print(formatStep(steps[steps.length - 1]));
24446
24528
  }
24447
24529
  if (previewCfg?.domain) await ensureCustomDomain(previewCfg.project, previewCfg.domain);
24530
+ const { options: deploymentOptions } = config.providers.deployment.tuple;
24531
+ const instance = previewInstance(config.name, deploymentOptions);
24532
+ if (instance) {
24533
+ steps.push(await runStep("deployment", `ensureProject ${instance.project}`, dryRun, async () => {
24534
+ await deploy.ensureProject({ name: instance.project });
24535
+ }));
24536
+ print(formatStep(steps[steps.length - 1]));
24537
+ if (instance.domain) await ensureCustomDomain(instance.project, instance.domain);
24538
+ }
24448
24539
  if (previewCfg?.domain && deploy.providerName === "cloudflare" && loader.has("dns")) {
24449
24540
  const dns = loader.get("dns");
24450
24541
  const wildcardDomain = `*.${previewCfg.domain}`;
@@ -38349,12 +38440,22 @@ const syncArgs = (y) => y.positional("steps", {
38349
38440
  global: false,
38350
38441
  describe: "Repo coords (\"owner/name\"). Defaults to plugin-specific resolution."
38351
38442
  });
38443
+ // src/commands/app-layer/app-layer.ts
38444
+ /**
38445
+ * The vault layer an app's secrets for a deployment target are read from: `production` reads `prod` and `preview` reads
38446
+ * its own `preview` layer, never `prod` (spec `tech-sentinel-preview-instance`), so a preview deployment is given only
38447
+ * what its own Environment holds.
38448
+ */
38449
+ function appLayer(target) {
38450
+ return target === "production" ? "prod" : "preview";
38451
+ }
38352
38452
  // src/commands/app-sync/app-sync.ts
38353
38453
  /**
38354
- * `holocron sync <app> --env <env>`: reads the app's vault Environment of the `prod` layer and sends the secrets
38355
- * `repo.secrets.deploy` names to the app's destinations, the way `sync secrets --project-id <app> --target production`
38356
- * or `--worker <app>` would: the Vercel project when the app has a `deployment` provider, the Worker script when it has
38357
- * a `workers` provider, both named after the app folder. Before anything is written the project must serve the app's
38454
+ * `holocron sync <app> --env <env>`: reads the app's vault Environment of each target's layer (`prod` for production,
38455
+ * `preview` for preview, never one for the other) and sends the secrets `repo.secrets.deploy` names for that layer to the
38456
+ * app's destinations, the way `sync secrets --project-id <app> --target <target>` or `--worker <app>` would: the Vercel
38457
+ * project when the app has a `deployment` provider, the Worker script when it has a `workers` provider, both named after
38458
+ * the app folder. Before anything is written the project must serve the app's
38358
38459
  * configured `domain`. An app with neither destination has nothing to sync and is reported, not failed.
38359
38460
  *
38360
38461
  * @param input - The app, the environment, its loaded config and the seams tests replace.
@@ -38383,41 +38484,57 @@ async function runAppSync(input) {
38383
38484
  const { loaded, context, loader } = await input.load();
38384
38485
  const destinations = [plan.deployment ? `${app.name} (project)` : "", plan.workers ? `${app.name} (Worker)` : ""];
38385
38486
  print(style.header(`Holocron sync ${app.name} → ${destinations.filter(Boolean).join(" + ")}${context.dryRun ? " (dry-run)" : ""}`));
38487
+ const instances = /* @__PURE__ */ new Map();
38386
38488
  if (plan.deployment) {
38387
38489
  await loader.load();
38388
38490
  if (!loader.has("deployment")) return fail(`${app.name}: the deployment provider did not load`);
38389
- const domain = plan.deployment.options.domain;
38390
- const checked = await checkProjectDomain(loader.get("deployment"), app.name, typeof domain === "string" ? domain : void 0);
38391
- if (checked.status === "fail") return fail(checked.message);
38392
- print(` ${style.dim(checked.message)}`);
38491
+ try {
38492
+ for (const target of env.targets) instances.set(target, appInstance(app.name, plan.deployment.options, target));
38493
+ } catch (err) {
38494
+ return fail(errorMessage(err));
38495
+ }
38496
+ for (const instance of instances.values()) {
38497
+ const checked = await checkProjectDomain(loader.get("deployment"), instance.project, instance.domain);
38498
+ if (checked.status === "fail") return fail(checked.message);
38499
+ print(` ${style.dim(checked.message)}`);
38500
+ }
38393
38501
  }
38394
- const environment = await input.resolveEnvironment(env.layer);
38395
38502
  const resolved = loaded.resolved;
38396
- const routes = secretRoutes(resolved.repo?.secrets?.sync, environment);
38397
- const variables = variableRoutes(resolved.repo?.variables?.sync, environment);
38398
- const variablesOnly = variables.length > 0 && routes.length === 0;
38399
- const filter = deployFilter(resolved.repo?.secrets?.deploy, environment);
38400
- const report = await input.syncSecrets({
38401
- loaded,
38402
- context,
38403
- loader,
38404
- environmentId: environment,
38405
- ...plan.deployment ? {
38406
- projectId: app.name,
38407
- targets: env.targets
38408
- } : {},
38409
- ...plan.workers ? { workerScript: app.name } : {},
38410
- ...filter ? { deployFilter: filter } : {},
38411
- ...routes.length ? { secretRoutes: routes } : variablesOnly ? { secretRoutes: [] } : {},
38412
- ...variables.length ? { variableRoutes: variables } : {}
38413
- });
38414
- if (report.summary.fail > 0) return {
38503
+ const failed = [];
38504
+ let written = 0;
38505
+ let skipped = 0;
38506
+ for (const target of env.targets) {
38507
+ const environment = await input.resolveEnvironment(appLayer(target));
38508
+ const instance = instances.get(target);
38509
+ const routes = secretRoutes(resolved.repo?.secrets?.sync, environment);
38510
+ const variables = variableRoutes(resolved.repo?.variables?.sync, environment);
38511
+ const variablesOnly = variables.length > 0 && routes.length === 0;
38512
+ const filter = deployFilter(resolved.repo?.secrets?.deploy, environment);
38513
+ const report = await input.syncSecrets({
38514
+ loaded,
38515
+ context,
38516
+ loader,
38517
+ environmentId: environment,
38518
+ ...instance ? {
38519
+ projectId: instance.project,
38520
+ targets: [instance.production ? "production" : target]
38521
+ } : {},
38522
+ ...plan.workers ? { workerScript: app.name } : {},
38523
+ ...filter ? { deployFilter: filter } : {},
38524
+ ...routes.length ? { secretRoutes: routes } : variablesOnly ? { secretRoutes: [] } : {},
38525
+ ...variables.length ? { variableRoutes: variables } : {}
38526
+ });
38527
+ written += report.summary.ok;
38528
+ skipped += report.summary.skip;
38529
+ if (report.summary.fail > 0) failed.push(`${target}: ${report.summary.fail} secret(s) did not sync`);
38530
+ }
38531
+ if (failed.length > 0) return {
38415
38532
  status: "fail",
38416
- message: `${app.name}: ${report.summary.fail} secret(s) did not sync`
38533
+ message: `${app.name}: ${failed.join("; ")}`
38417
38534
  };
38418
38535
  return {
38419
38536
  status: "ok",
38420
- message: `${app.name} synced (${report.summary.ok} written, ${report.summary.skip} skipped)`
38537
+ message: `${app.name} synced (${written} written, ${skipped} skipped)`
38421
38538
  };
38422
38539
  }
38423
38540
  // src/commands/sync/sync-app.ts