@terraforge/core 0.0.23 → 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, options?: ProcedureOptions): 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
@@ -1118,35 +1118,43 @@ const refresh = async (app, opt) => {
1118
1118
  if (opt.filters && opt.filters.length > 0) filteredStacks = Object.entries(appState?.stacks ?? {}).filter(([stackName]) => {
1119
1119
  return opt.filters.includes(stackName);
1120
1120
  }).map(([_, state]) => state);
1121
- if (appState && filteredStacks.length > 0) {
1122
- await Promise.all(filteredStacks.map((stackState) => {
1123
- return Promise.all(Object.values(stackState.nodes).map((nodeState) => {
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;
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);
1149
- }
1153
+ }))).flat().filter((op) => !!op),
1154
+ async commit() {
1155
+ await opt.backend.state.update(app.urn, appState);
1156
+ }
1157
+ };
1150
1158
  };
1151
1159
 
1152
1160
  //#endregion
@@ -1300,17 +1308,42 @@ var WorkSpace = class {
1300
1308
  /**
1301
1309
  * Refresh the state of the resources & data-sources inside your app.
1302
1310
  */
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 {
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) {
1311
1328
  await this.destroyProviders();
1329
+ await releaseLock();
1330
+ releaseExit();
1312
1331
  }
1313
- });
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
+ }
1314
1347
  }
1315
1348
  /**
1316
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.23",
3
+ "version": "0.0.24",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",