@openfn/deploy
Advanced tools
Comparing version 0.2.6 to 0.2.7
@@ -49,2 +49,6 @@ import { Logger } from '@openfn/logger'; | ||
delete?: boolean; | ||
project_id?: string; | ||
inserted_at?: string; | ||
updated_at?: string; | ||
deleted_at?: string; | ||
} | ||
@@ -64,2 +68,3 @@ interface ProjectState { | ||
name: string; | ||
project_id?: string; | ||
jobs: Concrete<Job>[]; | ||
@@ -91,2 +96,4 @@ triggers: Concrete<Trigger>[]; | ||
declare function mergeSpecIntoState(oldState: ProjectState, spec: ProjectSpec): ProjectState; | ||
declare function getStateFromProjectPayload(project: ProjectPayload): ProjectState; | ||
declare function mergeProjectPayloadIntoState(state: ProjectState, project: ProjectPayload): ProjectState; | ||
@@ -112,2 +119,2 @@ declare function getProject(config: DeployConfig, projectId: string): Promise<{ | ||
export { DeployConfig, DeployError, ProjectState, deploy, getConfig, getProject, getSpec, getState, mergeSpecIntoState, validateConfig }; | ||
export { DeployConfig, DeployError, ProjectState, deploy, getConfig, getProject, getSpec, getState, getStateFromProjectPayload, mergeProjectPayloadIntoState, mergeSpecIntoState, validateConfig }; |
@@ -99,2 +99,20 @@ // src/index.ts | ||
); | ||
var hyphenate = (str) => { | ||
return str.replace(/\s+/g, "-"); | ||
}; | ||
var reduceByKey = (key, arr, callback = (x) => x) => { | ||
return arr.reduce((acc, obj) => { | ||
const k = hyphenate(obj[key]); | ||
acc[k] = callback({ ...obj }); | ||
return acc; | ||
}, {}); | ||
}; | ||
var assignIfTruthy = (obj, incoming) => { | ||
Object.entries(incoming).forEach(([key, value]) => { | ||
if (!isNaN(value) || value) { | ||
obj[key] = value; | ||
} | ||
}); | ||
return obj; | ||
}; | ||
function splitZip(l, r) { | ||
@@ -221,9 +239,13 @@ return concatKeys(l, r).map((key) => { | ||
function convertToStateEdge(jobs2, triggers2, specEdge2, id) { | ||
const edge = { | ||
id, | ||
condition: specEdge2.condition ?? null, | ||
source_job_id: (specEdge2.source_job && jobs2[specEdge2.source_job].id) ?? null, | ||
source_trigger_id: (specEdge2.source_trigger && triggers2[specEdge2.source_trigger].id) ?? null, | ||
target_job_id: (specEdge2.target_job && jobs2[specEdge2.target_job].id) ?? "" | ||
}; | ||
const edge = assignIfTruthy( | ||
{ | ||
id, | ||
condition: specEdge2.condition ?? null, | ||
target_job_id: jobs2[specEdge2.target_job ?? -1]?.id ?? "" | ||
}, | ||
{ | ||
source_job_id: jobs2[specEdge2.source_job ?? -1]?.id, | ||
source_trigger_id: triggers2[specEdge2.source_trigger ?? -1]?.id | ||
} | ||
); | ||
return edge; | ||
@@ -291,10 +313,41 @@ } | ||
); | ||
return { | ||
const projectState = { | ||
...oldState, | ||
id: oldState.id || crypto.randomUUID(), | ||
name: spec.name, | ||
description: spec.description, | ||
workflows: nextWorkflows | ||
}; | ||
if (spec.description) | ||
projectState.description = spec.description; | ||
return projectState; | ||
} | ||
function getStateFromProjectPayload(project) { | ||
const workflows = reduceByKey("name", project.workflows, (wf) => { | ||
const { triggers, jobs, edges, ...workflowData } = wf; | ||
const stateWorkflow = { | ||
...workflowData | ||
}; | ||
stateWorkflow.triggers = reduceByKey("type", wf.triggers); | ||
stateWorkflow.jobs = reduceByKey("name", wf.jobs); | ||
stateWorkflow.edges = wf.edges.reduce((obj, edge) => { | ||
let sourceName; | ||
if (edge.source_trigger_id) { | ||
const t = wf.triggers.find((t2) => t2.id === edge.source_trigger_id); | ||
sourceName = t.type; | ||
} else { | ||
const job = wf.jobs.find((e) => e.id === edge.source_job_id); | ||
sourceName = job.name; | ||
} | ||
const target = wf.jobs.find((j) => j.id === edge.target_job_id); | ||
const name = hyphenate(`${sourceName}->${hyphenate(target.name)}`); | ||
obj[name] = edge; | ||
return obj; | ||
}, {}); | ||
return stateWorkflow; | ||
}); | ||
return { | ||
...project, | ||
workflows | ||
}; | ||
} | ||
function mergeProjectPayloadIntoState(state, project) { | ||
@@ -323,2 +376,3 @@ const nextWorkflows = Object.fromEntries( | ||
{ | ||
...nextWorkflow, | ||
id, | ||
@@ -354,3 +408,2 @@ name, | ||
).map((workflow) => { | ||
console.log(workflow); | ||
return { | ||
@@ -370,4 +423,6 @@ ...workflow, | ||
// src/client.ts | ||
var getLightningUrl = (config, path = "") => new URL(`/api/provision/${path}`, config.endpoint); | ||
async function getProject(config, projectId) { | ||
const url = new URL(config.endpoint + `/${projectId}`); | ||
const url = getLightningUrl(config, projectId); | ||
console.log(`Checking ${url} for existing project.`); | ||
try { | ||
@@ -381,2 +436,3 @@ const response = await fetch(url, { | ||
if (response.status === 404) { | ||
console.log("No existing project found."); | ||
return { data: null }; | ||
@@ -391,2 +447,3 @@ } | ||
} | ||
console.log("Project found."); | ||
return response.json(); | ||
@@ -400,3 +457,3 @@ } catch (error) { | ||
try { | ||
const url = new URL(config.endpoint); | ||
const url = getLightningUrl(config); | ||
const response = await fetch(url, { | ||
@@ -588,4 +645,6 @@ method: "POST", | ||
getState, | ||
getStateFromProjectPayload, | ||
mergeProjectPayloadIntoState, | ||
mergeSpecIntoState, | ||
validateConfig | ||
}; |
{ | ||
"name": "@openfn/deploy", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Deploy projects to Lightning instances", | ||
@@ -5,0 +5,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23686
741