Skip to content

Commit

Permalink
CLI: allow file paths for state and data
Browse files Browse the repository at this point in the history
  • Loading branch information
SatyamMattoo committed Jun 13, 2024
1 parent 40578c2 commit ca4a822
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
25 changes: 25 additions & 0 deletions packages/cli/src/util/load-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const loadPlan = async (
| 'adaptors'
| 'baseDir'
| 'expandAdaptors'
| 'statePath'
>,
logger: Logger
): Promise<ExecutionPlan> => {
Expand Down Expand Up @@ -203,6 +204,9 @@ const importExpressions = async (
typeof job.expression === 'string' && job.expression?.trim();
const configurationStr =
typeof job.configuration === 'string' && job.configuration?.trim();
const stateStr =
typeof job.state === 'string' && job.state?.trim();

if (expressionStr && isPath(expressionStr)) {
job.expression = await fetchFile(
job.id || `${idx}`,
Expand All @@ -220,6 +224,27 @@ const importExpressions = async (
);
job.configuration = JSON.parse(configString!);
}

if (stateStr && isPath(stateStr)) {
const stateString = await fetchFile(
job.id || `${idx}`,
rootDir,
stateStr,
log
);
job.state = JSON.parse(stateString!);
}

const state = job.state
if (typeof state === 'object' && state !== null) {
const keys = Object.keys(state);
for (const key of keys) {
if (typeof state[key] === 'string' && isPath(state[key])) {
const fileContent = await fetchFile(job.id || `${idx}`, rootDir, state[key], log);
state[key] = JSON.parse(fileContent);
}
}
}
}
};

Expand Down
46 changes: 39 additions & 7 deletions packages/cli/test/util/load-plan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test.serial('expression: load a plan from an expression.js', async (t) => {
plan: {},
};

const plan = await loadPlan(opts as Opts, logger);
const plan = await loadPlan(opts as unknown as Opts, logger);

t.truthy(plan);
t.deepEqual(plan.options, {});
Expand Down Expand Up @@ -118,7 +118,7 @@ test.serial('xplan: load a plan from workflow path', async (t) => {
plan: {},
};

const plan = await loadPlan(opts as Opts, logger);
const plan = await loadPlan(opts as unknown as Opts, logger);

t.truthy(plan);
t.deepEqual(plan, sampleXPlan);
Expand All @@ -143,7 +143,7 @@ test.serial('xplan: expand adaptors', async (t) => {
'test/wf.json': JSON.stringify(plan),
});

const result = await loadPlan(opts as Opts, logger);
const result = await loadPlan(opts as unknown as Opts, logger);
t.truthy(result);

const step = result.workflow.steps[0] as Job;
Expand All @@ -169,7 +169,7 @@ test.serial('xplan: do not expand adaptors', async (t) => {
'test/wf.json': JSON.stringify(plan),
});

const result = await loadPlan(opts as Opts, logger);
const result = await loadPlan(opts as unknown as Opts, logger);
t.truthy(result);

const step = result.workflow.steps[0] as Job;
Expand Down Expand Up @@ -197,7 +197,7 @@ test.serial('xplan: set timeout from CLI', async (t) => {
'test/wf.json': JSON.stringify(plan),
});

const { options } = await loadPlan(opts as Opts, logger);
const { options } = await loadPlan(opts as unknown as Opts, logger);
t.is(options.timeout, 666);
});

Expand All @@ -222,7 +222,7 @@ test.serial('xplan: set start from CLI', async (t) => {
'test/wf.json': JSON.stringify(plan),
});

const { options } = await loadPlan(opts as Opts, logger);
const { options } = await loadPlan(opts as unknown as Opts, logger);
t.is(options.start, 'b');
});

Expand Down Expand Up @@ -259,7 +259,7 @@ test.serial('old-workflow: load a plan from workflow path', async (t) => {
plan: {},
};

const plan = await loadPlan(opts as Opts, logger);
const plan = await loadPlan(opts as unknown as Opts, logger);

t.deepEqual(plan.options, {
start: 'a',
Expand All @@ -271,3 +271,35 @@ test.serial('old-workflow: load a plan from workflow path', async (t) => {
expression: 'x()',
});
});

test.serial('step: allow file paths for state and data', async (t) => {
const opts = {
workflowPath: 'test/wf.json',
plan: {},
};

const plan = createPlan([
{
id: 'a',
expression: '.',
state: './state.json',
},
]);

mock({
'test/data.json': JSON.stringify({ x: 1 }),
'test/state.json': JSON.stringify({
data: "./data.json"
}),
'test/wf.json': JSON.stringify(plan),
});
const result = await loadPlan(opts as unknown as Opts, logger);
t.truthy(result);

const step = result.workflow.steps[0] as Job;
t.deepEqual(step.state, {
data: {
x: 1
}
});
});

0 comments on commit ca4a822

Please sign in to comment.