@openfn/project 0.3.1 → 0.4.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.
package/dist/index.d.ts CHANGED
@@ -110,4 +110,16 @@ declare class Workspace {
110
110
  declare function yamlToJson(y: string): any;
111
111
  declare function jsonToYaml(json: string | JSONObject): string;
112
112
 
113
- export { Workspace, Project as default, jsonToYaml, yamlToJson };
113
+ type GenerateWorkflowOptions = {
114
+ name: string;
115
+ uuidSeed: number;
116
+ openfnUuid: boolean;
117
+ };
118
+ /**
119
+ * Generate a Workflow from a simple text based representation
120
+ * The def array contains strings of pairs of nodes
121
+ * eg, ['a-b', 'b-c']
122
+ */
123
+ declare function generateWorkflow(def: string[], options?: Partial<GenerateWorkflowOptions>): Workflow;
124
+
125
+ export { Workspace, Project as default, generateWorkflow as generate, jsonToYaml, yamlToJson };
package/dist/index.js CHANGED
@@ -943,6 +943,7 @@ function merge(source, target, options) {
943
943
  }
944
944
 
945
945
  // src/Project.ts
946
+ var maybeCreateWorkflow = (wf) => wf instanceof Workflow_default ? wf : new Workflow_default(wf);
946
947
  var setConfigDefaults = (config = {}) => ({
947
948
  workflowRoot: config.workflowRoot ?? "workflows",
948
949
  formats: {
@@ -1007,7 +1008,7 @@ var Project = class {
1007
1008
  this.description = data.description;
1008
1009
  this.openfn = data.openfn;
1009
1010
  this.options = data.options;
1010
- this.workflows = data.workflows?.map((w) => new Workflow_default(w)) ?? [];
1011
+ this.workflows = data.workflows?.map(maybeCreateWorkflow) ?? [];
1011
1012
  this.collections = data.collections;
1012
1013
  this.credentials = data.credentials;
1013
1014
  this.meta = data.meta;
@@ -1111,11 +1112,75 @@ var Workspace = class {
1111
1112
  }
1112
1113
  };
1113
1114
 
1115
+ // src/gen/workflow-generator.ts
1116
+ import { randomUUID as randomUUID2 } from "node:crypto";
1117
+
1118
+ // src/util/slugify.ts
1119
+ function slugify2(text) {
1120
+ return text.replace(/\W/g, " ").trim().replace(/\s+/g, "-").toLowerCase();
1121
+ }
1122
+
1123
+ // src/gen/workflow-generator.ts
1124
+ function gen(def, name = "workflow", uuidSeed, openfnUuid) {
1125
+ const ids = /* @__PURE__ */ new Map();
1126
+ const nodes = {};
1127
+ for (const conn of def) {
1128
+ const [from, to] = conn.split("-");
1129
+ if (nodes[from]) {
1130
+ if (to) {
1131
+ if (!nodes[from].next?.[to]) {
1132
+ nodes[from].next ??= {};
1133
+ nodes[from].next[to] = edge(`${from}-${to}`);
1134
+ }
1135
+ }
1136
+ } else {
1137
+ let props;
1138
+ if (to) {
1139
+ props = { next: { [to]: edge(`${from}-${to}`) } };
1140
+ }
1141
+ nodes[from] = node(from, props);
1142
+ }
1143
+ if (to && !nodes[to]) {
1144
+ nodes[to] = node(to);
1145
+ }
1146
+ }
1147
+ return {
1148
+ name,
1149
+ id: slugify2(name),
1150
+ steps: Object.values(nodes),
1151
+ ...openfnUuid ? { openfn: { uuid: randomUUID2() } } : {}
1152
+ };
1153
+ function node(id, props = {}) {
1154
+ return {
1155
+ id,
1156
+ ...props,
1157
+ openfn: { uuid: uuid(id) }
1158
+ };
1159
+ }
1160
+ function edge(id, props = {}) {
1161
+ return {
1162
+ ...props,
1163
+ openfn: { uuid: uuid(id) }
1164
+ };
1165
+ }
1166
+ function uuid(id) {
1167
+ const muuid = !isNaN(uuidSeed) ? ++uuidSeed : randomUUID2();
1168
+ ids.set(id, muuid);
1169
+ return muuid;
1170
+ }
1171
+ }
1172
+ function generateWorkflow(def, options = {}) {
1173
+ const { name, uuidSeed, openfnUuid } = options;
1174
+ const wf = gen(def, name, uuidSeed, openfnUuid);
1175
+ return new Workflow_default(wf);
1176
+ }
1177
+
1114
1178
  // src/index.ts
1115
1179
  var src_default = Project;
1116
1180
  export {
1117
1181
  Workspace,
1118
1182
  src_default as default,
1183
+ generateWorkflow as generate,
1119
1184
  jsonToYaml,
1120
1185
  yamlToJson
1121
1186
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/project",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Read, serialize, replicate and sync OpenFn projects",
5
5
  "type": "module",
6
6
  "exports": {