@terraforge/core 0.0.22 → 0.0.24

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.d.mts CHANGED
@@ -355,7 +355,14 @@ declare class WorkSpace {
355
355
  /**
356
356
  * Refresh the state of the resources & data-sources inside your app.
357
357
  */
358
- refresh(app: App): Promise<void>;
358
+ refresh(app: App, options?: ProcedureOptions): Promise<{
359
+ operations: {
360
+ urn: URN;
361
+ operation: "delete" | "update";
362
+ commit(): void;
363
+ }[] | undefined;
364
+ commit: () => Promise<void>;
365
+ }>;
359
366
  /**
360
367
  * Get the status of all resources in the app by comparing current config with state file.
361
368
  */
package/dist/index.mjs CHANGED
@@ -1114,35 +1114,47 @@ const hydrate = async (app, opt) => {
1114
1114
  const refresh = async (app, opt) => {
1115
1115
  const appState = await opt.backend.state.get(app.urn);
1116
1116
  const queue = createConcurrencyQueue(opt.concurrency ?? 10);
1117
- if (appState) {
1118
- await Promise.all(Object.values(appState.stacks).map((stackState) => {
1119
- return Promise.all(Object.values(stackState.nodes).map((nodeState) => {
1117
+ let filteredStacks = [];
1118
+ if (opt.filters && opt.filters.length > 0) filteredStacks = Object.entries(appState?.stacks ?? {}).filter(([stackName]) => {
1119
+ return opt.filters.includes(stackName);
1120
+ }).map(([_, state]) => state);
1121
+ if (appState && filteredStacks.length > 0) return {
1122
+ operations: (await Promise.all(filteredStacks.map((stackState) => {
1123
+ return Promise.all(Object.entries(stackState.nodes).map(([_urn, nodeState]) => {
1124
+ const urn = _urn;
1120
1125
  return queue(async () => {
1121
1126
  const provider = findProvider(opt.providers, nodeState.provider);
1122
- if (nodeState.tag === "data") {
1123
- const result = await provider.getData?.({
1124
- type: nodeState.type,
1125
- state: nodeState.output
1126
- });
1127
- if (result && !compareState(result.state, nodeState.output)) {
1128
- nodeState.output = result.state;
1129
- nodeState.input = result.state;
1127
+ let result;
1128
+ if (nodeState.tag === "data") result = await provider.getData?.({
1129
+ type: nodeState.type,
1130
+ state: nodeState.output
1131
+ });
1132
+ else result = await provider.getResource({
1133
+ type: nodeState.type,
1134
+ state: nodeState.output
1135
+ });
1136
+ if (!result) return {
1137
+ urn,
1138
+ operation: "delete",
1139
+ commit() {
1140
+ delete stackState.nodes[urn];
1130
1141
  }
1131
- } else if (nodeState.tag === "resource") {
1132
- const result = await provider.getResource({
1133
- type: nodeState.type,
1134
- state: nodeState.output
1135
- });
1136
- if (result && !compareState(result.state, nodeState.output)) {
1137
- nodeState.output = result.state;
1142
+ };
1143
+ else if (!compareState(result.state, nodeState.output)) return {
1144
+ urn,
1145
+ operation: "update",
1146
+ commit() {
1138
1147
  nodeState.input = result.state;
1148
+ nodeState.output = result.state;
1139
1149
  }
1140
- }
1150
+ };
1141
1151
  });
1142
1152
  }));
1143
- }));
1144
- await opt.backend.state.update(app.urn, appState);
1145
- }
1153
+ }))).flat().filter((op) => !!op),
1154
+ async commit() {
1155
+ await opt.backend.state.update(app.urn, appState);
1156
+ }
1157
+ };
1146
1158
  };
1147
1159
 
1148
1160
  //#endregion
@@ -1296,14 +1308,42 @@ var WorkSpace = class {
1296
1308
  /**
1297
1309
  * Refresh the state of the resources & data-sources inside your app.
1298
1310
  */
1299
- refresh(app) {
1300
- return lockApp(this.props.backend.lock, app, async () => {
1301
- try {
1302
- await refresh(app, this.props);
1303
- } finally {
1311
+ async refresh(app, options = {}) {
1312
+ let releaseLock;
1313
+ try {
1314
+ releaseLock = await this.props.backend.lock.lock(app.urn);
1315
+ } catch (error) {
1316
+ throw new Error(`Already in progress: ${app.urn}`);
1317
+ }
1318
+ const releaseExit = onExit(async () => {
1319
+ await this.destroyProviders();
1320
+ await releaseLock();
1321
+ });
1322
+ try {
1323
+ const result = await refresh(app, {
1324
+ ...this.props,
1325
+ ...options
1326
+ });
1327
+ if (!result) {
1304
1328
  await this.destroyProviders();
1329
+ await releaseLock();
1330
+ releaseExit();
1305
1331
  }
1306
- });
1332
+ return {
1333
+ operations: result?.operations,
1334
+ commit: async () => {
1335
+ await result?.commit();
1336
+ await this.destroyProviders();
1337
+ await releaseLock();
1338
+ releaseExit();
1339
+ }
1340
+ };
1341
+ } catch (error) {
1342
+ await this.destroyProviders();
1343
+ await releaseLock();
1344
+ releaseExit();
1345
+ throw error;
1346
+ }
1307
1347
  }
1308
1348
  /**
1309
1349
  * Get the status of all resources in the app by comparing current config with state file.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terraforge/core",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",