@terraforge/core 0.0.23 → 0.0.25

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, options?: ProcedureOptions): Promise<void>;
358
+ refresh(app: App, options?: ProcedureOptions): Promise<{
359
+ operations: {
360
+ urn: URN;
361
+ operation: "delete" | "update";
362
+ commit(): void;
363
+ }[];
364
+ commit: () => Promise<void>;
365
+ } | undefined>;
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
@@ -1119,33 +1119,45 @@ const refresh = async (app, opt) => {
1119
1119
  return opt.filters.includes(stackName);
1120
1120
  }).map(([_, state]) => state);
1121
1121
  if (appState && filteredStacks.length > 0) {
1122
- await Promise.all(filteredStacks.map((stackState) => {
1123
- return Promise.all(Object.values(stackState.nodes).map((nodeState) => {
1122
+ const filteredOperations = (await Promise.all(filteredStacks.map((stackState) => {
1123
+ return Promise.all(Object.entries(stackState.nodes).map(([_urn, nodeState]) => {
1124
+ const urn = _urn;
1124
1125
  return queue(async () => {
1125
1126
  const provider = findProvider(opt.providers, nodeState.provider);
1126
- if (nodeState.tag === "data") {
1127
- const result = await provider.getData?.({
1128
- type: nodeState.type,
1129
- state: nodeState.output
1130
- });
1131
- if (result && !compareState(result.state, nodeState.output)) {
1132
- nodeState.output = result.state;
1133
- 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];
1134
1141
  }
1135
- } else if (nodeState.tag === "resource") {
1136
- const result = await provider.getResource({
1137
- type: nodeState.type,
1138
- state: nodeState.output
1139
- });
1140
- if (result && !compareState(result.state, nodeState.output)) {
1141
- nodeState.output = result.state;
1142
+ };
1143
+ else if (!compareState(result.state, nodeState.output)) return {
1144
+ urn,
1145
+ operation: "update",
1146
+ commit() {
1142
1147
  nodeState.input = result.state;
1148
+ nodeState.output = result.state;
1143
1149
  }
1144
- }
1150
+ };
1145
1151
  });
1146
1152
  }));
1147
- }));
1148
- await opt.backend.state.update(app.urn, appState);
1153
+ }))).flat().filter((op) => !!op);
1154
+ if (filteredOperations.length === 0) return;
1155
+ return {
1156
+ operations: filteredOperations,
1157
+ async commit() {
1158
+ await opt.backend.state.update(app.urn, appState);
1159
+ }
1160
+ };
1149
1161
  }
1150
1162
  };
1151
1163
 
@@ -1300,17 +1312,43 @@ var WorkSpace = class {
1300
1312
  /**
1301
1313
  * Refresh the state of the resources & data-sources inside your app.
1302
1314
  */
1303
- refresh(app, options = {}) {
1304
- return lockApp(this.props.backend.lock, app, async () => {
1305
- try {
1306
- await refresh(app, {
1307
- ...this.props,
1308
- ...options
1309
- });
1310
- } finally {
1315
+ async refresh(app, options = {}) {
1316
+ let releaseLock;
1317
+ try {
1318
+ releaseLock = await this.props.backend.lock.lock(app.urn);
1319
+ } catch (error) {
1320
+ throw new Error(`Already in progress: ${app.urn}`);
1321
+ }
1322
+ const releaseExit = onExit(async () => {
1323
+ await this.destroyProviders();
1324
+ await releaseLock();
1325
+ });
1326
+ try {
1327
+ const result = await refresh(app, {
1328
+ ...this.props,
1329
+ ...options
1330
+ });
1331
+ if (!result) {
1311
1332
  await this.destroyProviders();
1333
+ await releaseLock();
1334
+ releaseExit();
1335
+ return;
1312
1336
  }
1313
- });
1337
+ return {
1338
+ operations: result.operations,
1339
+ commit: async () => {
1340
+ await result.commit();
1341
+ await this.destroyProviders();
1342
+ await releaseLock();
1343
+ releaseExit();
1344
+ }
1345
+ };
1346
+ } catch (error) {
1347
+ await this.destroyProviders();
1348
+ await releaseLock();
1349
+ releaseExit();
1350
+ throw error;
1351
+ }
1314
1352
  }
1315
1353
  /**
1316
1354
  * 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.23",
3
+ "version": "0.0.25",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",