@lowdefy/build 6.0.0 → 6.1.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.
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { type, serializer } from '@lowdefy/helpers';
16
- import getCronEnvironmentNames from '../utils/getCronEnvironmentNames.js';
16
+ import getEnvironmentNames from '../utils/getEnvironmentNames.js';
17
17
  async function writeEndpoint({ endpoint, context }) {
18
18
  await context.writeBuildArtifact(`api/${endpoint.endpointId}.json`, serializer.serializeToString(endpoint ?? {}));
19
19
  }
@@ -21,18 +21,23 @@ async function writeEndpoint({ endpoint, context }) {
21
21
  // generate the `crons` array in config.json. The runtime reads schedules off the endpoint artifact
22
22
  // directly, so it does not depend on this file.
23
23
  //
24
- // With config.cron.environments declared there is one entry per enabled environment and schedule:
25
- // `environment` names it and `forward` is true for environments the production deployment forwards
26
- // to (those with a url) — Vercel fires crons only on production, so every environment's schedules
27
- // must be registered there.
28
- function getSchedules({ endpoint, environment }) {
24
+ // With config.environments declared there is one entry per registered environment and schedule:
25
+ // `environment` names it and `forward` is true for environments this deployment forwards to.
26
+ // Vercel fires crons only on production, so:
27
+ // - the current environment registers its own schedules;
28
+ // - an environment with a cron.secret is forwarded to, so its own crons never fire it; any other
29
+ // current environment is the one Vercel fires crons on and also registers every forwarded
30
+ // environment's schedules;
31
+ // - without a current environment only the `default` schedules are registered;
32
+ // - cron.enabled: false registers nothing for that environment.
33
+ function getSchedules({ endpoint, key }) {
29
34
  if (type.isArray(endpoint.schedules)) return endpoint.schedules;
30
- return endpoint.schedules?.[environment] ?? [];
35
+ return endpoint.schedules?.[key] ?? [];
31
36
  }
32
- function pushSchedules({ endpoint, environment, forward }) {
37
+ function toManifestEntries({ endpoint, key, environment, forward }) {
33
38
  return getSchedules({
34
39
  endpoint,
35
- environment
40
+ key
36
41
  }).map((schedule)=>({
37
42
  endpointId: endpoint.endpointId,
38
43
  cron: schedule.cron,
@@ -43,24 +48,59 @@ function pushSchedules({ endpoint, environment, forward }) {
43
48
  }
44
49
  }));
45
50
  }
51
+ function isCronEnabled(environment) {
52
+ return environment.cron?.enabled !== false;
53
+ }
54
+ function isForwardTarget(environment) {
55
+ return !type.isUndefined(environment.cron?.secret);
56
+ }
57
+ function getRegisteredEnvironments({ environments, current }) {
58
+ const registered = [];
59
+ if (isCronEnabled(environments[current])) {
60
+ registered.push({
61
+ name: current,
62
+ forward: false
63
+ });
64
+ }
65
+ if (isForwardTarget(environments[current])) return registered;
66
+ getEnvironmentNames(environments).forEach((name)=>{
67
+ const environment = environments[name];
68
+ if (name === current || !isForwardTarget(environment) || !isCronEnabled(environment)) return;
69
+ registered.push({
70
+ name,
71
+ forward: true
72
+ });
73
+ });
74
+ return registered;
75
+ }
46
76
  async function writeSchedulesManifest({ components, context }) {
47
- const cronEnvironments = components.config?.cron?.environments;
48
- const environmentNames = getCronEnvironmentNames(cronEnvironments);
77
+ const { environments, environment: current } = components.config ?? {};
78
+ const declared = getEnvironmentNames(environments).length > 0;
79
+ const registered = declared && !type.isUndefined(current) ? getRegisteredEnvironments({
80
+ environments,
81
+ current
82
+ }) : [];
49
83
  const schedules = [];
50
84
  (components.api ?? []).forEach((endpoint)=>{
51
- if (environmentNames.length === 0) {
52
- schedules.push(...pushSchedules({
85
+ if (!declared) {
86
+ schedules.push(...toManifestEntries({
53
87
  endpoint
54
88
  }));
55
89
  return;
56
90
  }
57
- environmentNames.forEach((environment)=>{
58
- const config = cronEnvironments[environment];
59
- if (config.enabled === false) return;
60
- schedules.push(...pushSchedules({
91
+ if (type.isUndefined(current)) {
92
+ schedules.push(...toManifestEntries({
61
93
  endpoint,
62
- environment,
63
- forward: !type.isUndefined(config.url)
94
+ key: 'default'
95
+ }));
96
+ return;
97
+ }
98
+ registered.forEach(({ name, forward })=>{
99
+ schedules.push(...toManifestEntries({
100
+ endpoint,
101
+ key: name,
102
+ environment: name,
103
+ forward
64
104
  }));
65
105
  });
66
106
  });